Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Find the Maximum Number (Solution)

00:00 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 "inf".

00:15 There’s also negative infinity, which you can obtain by placing a minus sign (-) in front of the positive infinity or by using the "-inf" argument.

00:28 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.

00:45 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.

01:07 Let’s try something in the middle between 250 and 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 250 plus 375 divided by 2. We can’t use fractional exponents, so we’ll round it to the nearest whole number, which is 312.

01:36 This also gives us infinity, so we have to keep trying smaller exponents. The next one should be 281.

01:47 Okay, that’s too small. Let’s move the other direction then.

01:57 We are getting closer now since the smallest exponents that we’re looking for must be somewhere between 297 and 312.

02:08 Oh, we’re getting really close now since we only have a few numbers left to try. Why don’t we check them by hand? 306, that’s still too small.

02:17 307, also too small. 308? Bingo. So the smallest exponent that gives us infinity is 308. That’s the answer. However, there’s a better way to find it.

02:33 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.

02:45 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.

03:10 So there’s no single correct answer to this exercise, as it may vary.

03:16 Okay, that wraps up this section. Next up, you’ll switch gears and get your hands dirty with number formatting.

Become a Member to join the conversation.