In this tutorial, we will learn the different methods to print a large number in Python. And the contrast between printing integers or floats in those situations.
As you go through the article, feel free to watch my explainer video:
Problem Formulation: Float vs Integer Printing
Python by default prints an approximation in the case of float numbers. Most people do not need that much precision. This is to keep the number of digits manageable by displaying a rounded value instead:
>>> print(0.1) 0.1
If the numbers weren’t rounded this would be the result:
If you try to print the large float number, this would be the result using the
>>> print(0.1000000000000000055511151231257827021181583404541015625) 0.1
To tell you the truth, most programmers that don’t work with accounting or mathematical applications do not bother to understand why printing large floats or integer numbers is an issue in Python. They simply don’t have the need.
What is the maximum possible value of an integer in Python?
In the case of integers, those types of numbers don’t have any kind of limitation went printing large numbers. They can be as large value as memory is available.
Try running this:
y = 99999**1000 print(y)
The output should be a large integer number. And it should not have any problem displaying the results of this exponentiation.
If you increase the exponent, let say to 10000 value, you should get the results with a message
"Squeed text (255 lines).", on the IDLE Shell.
If you double click the message, it will follow up by a warning that expands it will make it slow or unresponsive. That is because the output consumes your PC memory.
How do you print large numbers in Python?
Is went we dwell with large float numbers, that is went problems start to arise. Example:
Traceback (most recent call last): File "<pyshell#3>", line 1, in <module> x=9.9999**1000 OverflowError: (34, 'Result too large')
print() function can’t show on the screen the results because the answer is too large.
The reality is that Python floats are neither of unlimited size and don’t have arbitrary precision by default, meaning: there is a size limit based on the system it is running (most common 32 or 64 bits). That restriction does not apply to integers or strings.
To know more about the technical limitations of the Python programming language read the Floating-Point Arithmetic: Issues and Limitations.
Let say the numbers you are working on are large, but not large enough to produce an overflow error. Printing the results should be easy? Right?
>>> x=9.9999**100 >>> print(x)
Well, it prints a summary because the number is still too high. The
print() function standard limitations can be circumvented using the following methods:
Method 1: Convert Floats to Integers
Python displays a rounded value to keep the number of digits manageable. It shows float up to 16 digits of precision.
One method to get all the numbers is to convert the results from floats to integers using the
On older versions of Python, the
long() function can be used to get identical results. Check PEP 237 for more information.
Method 2: Decimal Module
In cases like accounting and high-precision applications, jobs where printing the exact decimal representation is needed, use the decimal module.
Using the previous example:
>>> from decimal import Decimal >>> Decimal.from_float(x)
This module is designed to work in the same way as arithmetic is taught at school. It provides support for fast correctly-rounded decimal floating-point arithmetic, even for large numbers.
Method 3: Fractions
Another way to print large float numbers is supported by the fractions module which implements arithmetic based on rational numbers (so the numbers like 1/10 can be represented exactly).
>>> from fractions import Fraction >>> Fraction(x)
Also, Python has a method that delivers the exact value of float, as a fraction: The
In this tutorial, we learned three different ways to print large numbers:
- Convert floats to integers
- Decimal module
We hope this brief article helps you understand better the ways of printing large integers and floats numbers and the limitation of each.