What happens when large integers are involved?

In summary, the program's intended result is incorrect because it's using a 64-bit number to do integer arithmetic which exceeds the limit of 16 digits.
  • #1
PeroK
Science Advisor
Homework Helper
Insights Author
Gold Member
2023 Award
27,901
19,395
TL;DR Summary
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
  • #2
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 PeroK
  • #3
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?
 
  • #4
No, there is a long integer library you must find.
 
  • #5
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.
 
  • #6
@PeroK what version do you use?
 
  • #7
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 Wrichik Basu, FactChecker, PeroK and 1 other person
  • #8
pbuk said:
The / operator is a floating point operator. Integer division is // (since 2.something)
That's fixed it. Thanks.
 
  • #9
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 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 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 PeroK

FAQ: What happens when large integers are involved?

What are large integers?

Large integers are numbers that are larger than the typical range of numbers that can be easily represented and processed by a computer. They are typically greater than 2^32 or 4,294,967,296.

How do computers handle large integers?

Computers handle large integers by using specialized algorithms and data structures that can store and process numbers with a higher precision than regular integers. This can include using multiple processors, breaking the number into smaller parts, or using special libraries designed for handling large integers.

What happens when large integers are added or multiplied together?

When large integers are added or multiplied together, the computer may need to use a larger data type or a special algorithm to store and process the result. This can result in longer processing times and increased memory usage.

Can large integers cause errors in computer calculations?

Yes, large integers can cause errors in computer calculations if they exceed the maximum value that can be stored in a particular data type. This can result in overflow errors or incorrect results.

How can I avoid errors when working with large integers?

To avoid errors when working with large integers, it is important to use the appropriate data type and to regularly check for overflow errors in the code. Additionally, using specialized libraries or breaking the number into smaller parts can also help in handling large integers more accurately.

Back
Top