Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smallest positive float64 number

Tags:

I need to find a numpy.float64 value that is as close to zero as possible.

Numpy offers several constants that allow to do something similar:

  • np.finfo(np.float64).eps = 2.2204460492503131e-16
  • np.finfo(np.float64).tiny = 2.2250738585072014e-308

These are both reasonably small, but when I do this

>>> x = np.finfo(np.float64).tiny >>> x / 2 6.9533558078350043e-310 

the result is even smaller. When using an impromptu binary search I can get down to about 1e-323, before the value is rounded down to 0.0.

Is there a constant for this in numpy that I am missing? Alternatively, is there a right way to do this?

like image 880
m00am Avatar asked Jul 20 '16 09:07

m00am


People also ask

What is the difference between float and float64?

float is an alias for python float type. np. float32 and np. float64 are numpy specific 32 and 64-bit float types.

What is float64 in Python?

Python float values are represented as 64-bit double-precision values. 1.8 X 10308 is an approximate maximum value for any floating-point number. If it exceeds or exceeds the max value, Python returns an error with string inf (infinity).

How many digits can a float hold Python?

For example, 97.98, 32.3+e18, -32.54e100 all are floating point numbers. Python float values are represented as 64-bit double-precision values. The maximum value any floating-point number can be is approx 1.8 x 10308.


2 Answers

Use np.nextafter.

>>> import numpy as np >>> np.nextafter(0, 1) 4.9406564584124654e-324 >>> np.nextafter(np.float32(0), np.float32(1)) 1.4012985e-45 
like image 103
ev-br Avatar answered Sep 21 '22 02:09

ev-br


2^-1075 is the smallest positive float. 2^-1075 = 5.10^-324

like image 26
Simo Avatar answered Sep 22 '22 02:09

Simo