What happens when large integers are involved?

  • Thread starter Thread starter PeroK
  • Start date Start date
  • Tags Tags
    Integers Python
Click For Summary
SUMMARY

The discussion focuses on issues encountered when performing arithmetic operations on large integers in Python, particularly the confusion between the division operators '/' and '//'. The user initially faced incorrect results due to floating-point arithmetic, which is limited to 16 digits of precision. The conversation highlights that Python 3 can handle arbitrarily large integers, and suggests using the integer division operator '//' or the Fraction class from the fractions module to avoid rounding errors. Additionally, the divmod function is recommended for extracting integer quotients and remainders accurately.

PREREQUISITES
  • Understanding of Python 3 integer types and arithmetic operations
  • Familiarity with floating-point precision limitations
  • Knowledge of the differences between Python 2.x and 3.x division behavior
  • Basic understanding of libraries such as fractions and SageMath for arbitrary precision arithmetic
NEXT STEPS
  • Learn about Python 3 integer division using the '//' operator
  • Explore the Fraction class in the fractions module for exact arithmetic
  • Investigate the divmod built-in function for integer division and remainder
  • Research SageMath for performing arbitrary precision arithmetic in Python
USEFUL FOR

Programmers transitioning from Python 2.x to 3.x, data scientists handling large numerical computations, and developers needing to perform precise arithmetic operations on large integers.

PeroK
Science Advisor
Homework Helper
Insights Author
Gold Member
2025 Award
Messages
29,464
Reaction score
21,209
TL;DR
Arithmetic error handling large integer in Python
I've written a program to factorise large numbers (although not that large). The following arithmetic operation goes wrong:

x= int(912_321_155_211_368_155/(5))

The result is

182_464_231_042_273_632

Which is clearly not right (should end in 631). The maximum integer on the 64-bit version is supposed to be larger than that.

How do you know when integer arithmetic is getting too large and things start to go wrong?
 
Last edited:
Technology news on Phys.org
It looks like it is using 64 bit double precision floating point which will give you 16 digits. You might be able to force it to use 16 bit unsigned integer.
Also arrays of digits can be used.
 
  • Like
Likes   Reactions: PeroK
Baluncore said:
It looks like it is using 64 bit double precision floating point which will give you 16 digits. You might be able to force it to use 16 bit unsigned integer.
I'll assume the limit is 16 digits.

To go beyond that, do you have to hand-crank the arithmetic? Just program long division myself?
 
No, there is a long integer library you must find.
 
Baluncore said:
No, there is a long integer library you must find.
No, there is no long integer library in Python 3 because it should automatically upgrade 'ordinary' integers.
 
@PeroK what version do you use?
 
PeroK said:
x= int(912_321_155_211_368_155/(5))
Baluncore said:
It looks like it is using 64 bit double precision floating point which will give you 16 digits.
The / operator is a floating point operator. Integer division is // (since 2.something)
 
  • Like
Likes   Reactions: Wrichik Basu, FactChecker, PeroK and 1 other person
pbuk said:
The / operator is a floating point operator. Integer division is // (since 2.something)
That's fixed it. Thanks.
 
PeroK said:
I've written a program to factorise large numbers (although not that large). The following arithmetic operation goes wrong:

x= int(912_321_155_211_368_155/(5))

To avoid floating point rounding errors, either use integer division, i.e., the // operator, if you know the quotient is an exact integer, as has already been suggested, or use the Fraction class from the fractions module, which can represent fractions exactly without using floating point and risking rounding errors.

Another option to extract integer quotients is the divmod built-in function, which gives you an integer quotient and remainder.

PeroK said:
The result is

182_464_231_042_273_632

Which is clearly not right (should end in 631). The maximum integer on the 64-bit version is supposed to be larger than that.

The issue here has nothing to do with any maximum integer size. Python can represent arbitrarily large integers; it's not limited by the bit width of the underlying platform.
 
  • Like
Likes   Reactions: PeroK
  • #10
Baluncore said:
You might be able to force it to use 16 bit unsigned integer.
That wouldn't help -- the largest unsigned 16-bit integer is 65, 535.
 
  • Haha
Likes   Reactions: pbuk
  • #11
The / and // operators can cause some curious side effects when porting code from 2.7 python to 3.x python.

The / in python 2.x returns integer answers when the operands are both integers and return float answers when one or both operands are floats.

2 / 3 returns 0

5 / 2 returns 2

Programmers familiar with this behavior using python 2.x would tend to use / over //.

One common use for the integer / operator was when computing indices for data records in binary random store files.

I ran into this very bug as I converted a program from C to python 2.x which worked and then to python 3.x which didn't. It was tracked down to the / vs // issue.

https://stackoverflow.com/questions/183853/what-is-the-difference-between-and-when-used-for-division
 
  • #12
Sage (https://www.sagemath.org/) will do arbitrary precision arithmetic. It is an easy add-on to Python, and runs in Jupyter notebooks.

Code:
factor(12345678910111213141516171819202122232425262728293031323334353637383940)
2^2 * 5 * 3169 * 60757 * 579779 * 4362289433 * 79501124416220680469 * 15944694111943672435829023
 
  • Like
Likes   Reactions: PeroK

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
Replies
9
Views
3K
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 1 ·
Replies
1
Views
880
  • · Replies 2 ·
Replies
2
Views
3K
Replies
2
Views
2K
Replies
29
Views
5K
Replies
6
Views
6K