Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

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.

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

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

01:29 We can’t use fractional exponents, so we’ll round it to the nearest whole number, which is 312. This also gives us infinity, so we have to keep trying smaller exponents.

01:41 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.

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