Find the Maximum Number (Solution)
Infinity is a special value in the floating-point data type, which is greater than any other number. You can explicitly define infinity in Python by calling the built-in
float() function with a special string value as an argument, which says
Now, unlike integers, Python floats do have a limit or maximum value. If you try to create a floating-point number that exceeds that limit, then you’ll get infinity instead. Here, I’m using
2e500, which gives me infinity.
However, to find the smallest exponent that yields infinity, I can use the bisection algorithm by halving the exponent value at each step. So, since
500 is too much, I can try
250, which is half of the initial exponent. In this case, we’re getting a floating-point number that is clearly not infinity.
Let’s try something in the middle between
500. That would be
375. Okay, that’s too much. We can try a smaller exponent by finding the middle point between the last two exponent values, which would be
375 divided by
2. We can’t use fractional exponents, so we’ll round it to the nearest whole number, which is
This alternative approach is slightly more advanced, so don’t worry if you don’t completely understand it. You can import this standard library module called
sys, which provides access to system-specific parameters.
The parameter that we are interested in is called
float_info, and it shows a number of low-level details about the floating-point data type in Python, including
max_10_exp, which is equal to
308 that we found manually. Note that this number isn’t set in stone, as it depends on your operating system, hardware, architecture, Python release, and how the Python interpreter was compiled.
Become a Member to join the conversation.