Reputation: 79
In my program I'm trying to find the smallest number that python can give me. When I kept dividing a number by 2, I got 5 x 10^-324 (5e-324). I thought I could divide this by the biggest number I can use in python. I tried to get the biggest number in python by doing this:
z = 1
while True:
try:
z = z+1
except OverflowError:
z = z-1
break
Here is my full code:
from os import system
x = 76556758478567587
while True:
x = x/2
if x/2 == 0.0:
break
print("Smallest number I can compute:", x)
print()
z = 1
while True:
try:
z = z+1
except OverflowError:
z = z-1
break
print(str(x) + " divided by " + str(z) + " is...")
z = x/z
print(z)
system("pause >nul")
Every time I run this it does nothing. I suddenly recognize it's still trying to solve the problem so I open task manager and Python was eating up my CPU like a pack of wolves eating a dead cow.
I know the smallest number in python would be negative but I want to get the the smallest number above zero.
Upvotes: 1
Views: 2246
Reputation: 48077
You may use sys.float_info
to get the maximum/minimum representable finite float as:
>>> import sys
>>> sys.float_info.min
2.2250738585072014e-308
>>> sys.float_info.max
1.7976931348623157e+308
Python uses double-precision floats, which can hold values from about 10 to the -308 to 10 to the 308 power. Below is the experiment from the python prompt:
# for maximum
>>> 1e308
1e+308
>>> 1e+309
inf <-- Infinite
You may even get numbers smaller than 1e-308
via denormals
, but there is a significant performance hit to this and such numbers are represented with a loss of precision. I found that Python is able to handle 1e-323 but underflows on 1e-324 and returns 0.0 as the value.
# for minimum
>>> 1e-323
1e-323
>>> 1e-324
0.0
You can get denormalized minimum as sys.float_info.min * sys.float_info.epsilon
, which comes as 5e-324
.
As per the document:
sys.float_info.max is maximum representable finite float
sys.float_info.min is minimum positive normalized float
To get more information check: sys.float_info
:
sys.float_info is a struct sequence holding information about the float type. It contains low level information about the precision and internal representation. The values correspond to the various floating-point constants defined in the standard header file float.h for the ‘C’ programming language; see section 5.2.4.2.2 of the 1999 ISO/IEC C standard [C99], ‘Characteristics of floating types’, for details.
Upvotes: 3
Reputation: 104712
The smallest floating point number representable in Python is 5e-324
, or 2.0**-1075
.
The other two answers have each given you half of what you need to find this number. It can be found by multiplying sys.float_info.min
(2.2250738585072014e-308
, the smallest normalized float) by sys.float_info.epsilon
(2.220446049250313e-16
, the smallest proportion you can modify a number by while still getting a distinct value).
Upvotes: 1
Reputation: 17500
The key word that you're apparently missing is "epsilon." If you search on Python epsilon value then it returns a links to StackOverflow: Value for epsilon in Python near the top of the results in Google.
Obviously it helps if you understand how the term is associated with this concept.
Upvotes: 0