1

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.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • Find the largest integer first, then take the reciprocal;) – Andras Deak -- Слава Україні Sep 28 '16 at 21:13
  • you first algorithm is log(N)-complex, your second one is N-complex. You should multiply by 2 until you reach overflow THEN work your way down. However I'm sensing that the exercise is pointless. – Jean-François Fabre Sep 28 '16 at 21:17
  • What? There is no maximum or minimum number in Python. Python will automagically convert your "int" to a "long" which is actually infinite size (realistically w.e. is allocated by the OS). There is a max 'int', but it's fairly meaningless. If that's not satisfying, check out https://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic or search for "big number library" for reference impls. – Eugene K Sep 28 '16 at 21:25

3 Answers3

3

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.

Community
  • 1
  • 1
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
  • The interesting thing is that it seems that you _can_ still divide `sys.float_info.min` ... And I'm not sure _why_. Maybe an IEEE guru can enlighten us? – mgilson Sep 28 '16 at 21:19
  • How do you suppose that 2.2250738585072014e-308 is the smallest available float when the OP has gotten to 5e-324? – TigerhawkT3 Sep 28 '16 at 21:19
  • Try: `sys.float_info.min*sys.float_info.epsilon` – brianpck Sep 28 '16 at 21:21
  • @mgilson isn't this about normalized and unnormalized floats? `2.2e-308` might be divided into `0.000000002e-308`, etc? – Andras Deak -- Слава Україні Sep 28 '16 at 21:24
  • @AndrasDeak -- Likely. I don't know the difference between a "normalized" and "unnormalized" float :-). You'd think I would have picked that up somewhere ... – mgilson Sep 28 '16 at 21:25
  • 1
    @mgilson and I don't even know the terminology, apparently they're called [denormal numbers](https://en.wikipedia.org/wiki/Denormal_number). – Andras Deak -- Слава Україні Sep 28 '16 at 21:27
  • In order to clarify the doubts here: `sys.float_info.max` is the number with the largest exponent and the full precision in use. As you go above it, you'll loose precision. Similar is for `.min` – Moinuddin Quadri Sep 28 '16 at 21:42
  • Python uses double-precision floats, which can hold values from about 10 to the -308 to 10 to the 308 power. Updated the answer with the experiment from the python prompt. Hope that will clarify your doubt – Moinuddin Quadri Sep 28 '16 at 21:47
  • This answer was most helpful because I tried the code and divided what I got with the maximum float. I got 0, this confirmed that I had found the smallest number (above 0) in python. Thanks. – AAAAAAAAA EEEEEEEEEEEEEEEEEEEE Oct 03 '16 at 22:45
1

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).

Blckknght
  • 100,903
  • 11
  • 120
  • 169
0

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.

Community
  • 1
  • 1
Jim Dennis
  • 17,054
  • 13
  • 68
  • 116