This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Rounding/Large Int representation error with multiplication and division
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eglass, eric.smith, steven.daprano
Priority: normal Keywords:

Created on 2016-08-08 10:57 by eglass, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (9)
msg272160 - (view) Author: Ethan Glass (eglass) Date: 2016-08-08 10:57
Python represents large integers as floating-points, which may or may not be the cause or part of the cause of division of said large integers evaluating to a floating-point number usually within a 1-integer range of the correct result.
msg272163 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2016-08-08 11:21
Actually, Python represents large integers exactly. Can you provide a specific example of a problem you're seeing?
msg272167 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2016-08-08 14:26
Your description is hard to understand, and doesn't give enough detail, but I *think* I can guess what you might be referring to.

If you start with a really big integer, and divide, you get a float. But really big floats cannot represent every number exactly (they only have a limited precision). For example:

py> n = 12345678901234567
py> print(n, n/1)
12345678901234567 1.2345678901234568e+16

Converting n to an int (you don't need to use division, calling float() will do the same thing) rounds to the nearest possible 64-bit floating point value, which happens to be one larger.

If this is the error you are talking about, there's nothing that can be done about it. It is fundamental to the way floating point numbers work.

If you mean something different, please explain.
msg272168 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2016-08-08 14:27
> Converting n to an int

Oops. Obviously I meant converting n *from* an int *to* a float.
msg272229 - (view) Author: Ethan Glass (eglass) Date: 2016-08-09 10:04
I actually started with a very small integer, said integer being 14. I then multiplied it by two large integers(10^100 or higher).I no longer have access to the two large integers, as they were never stored. When I divided the product by the two large integers, the quotient is returned as 13, 14, or 15.  I have done more testing and found that the correct quotient of 14 is almost always returned, but 13 or 15 are returned occasionally. I am not trying to convert anything to/from a float to/from an integer. I typed a similar equation into the python shell and found that if I only divide by one of the factors, the return is a floating point that appears something like this:
6.754791118045615e+200
This is displayed in scientific notation as a float multiplied by 10^200. I do not know if this is how the computer processes and stored the integer or just how it is displayed,but if it is in fact how the integer is stored, it would theoretically be subject to rounding errors.
msg272235 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2016-08-09 11:32
Since your result is 6.754791118045615e+200, it's definitely not stored as an integer.

If you would show us exactly how you calculated this value, we can explain where the conversion to floating point is taking place. 

In the meantime, I'm going to close this issue, since it's not an problem with python integers.
msg272297 - (view) Author: Ethan Glass (eglass) Date: 2016-08-10 03:40
As I said in a previous post, the numbers used are not stored. I ran the algorithm again in the shell with the following result.
>>>43328029062024980302163323673775445496132394617695779999836837704406332963931535527253995277986388432*8

346624232496199842417306589390203563969059156941566239998694701635250663711452284218031962223891107456
>>>346624232496199842417306589390203563969059156941566239998694701635250663711452284218031962223891107456/8

4.332802906202498e+100
msg272300 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2016-08-10 04:23
Thanks for giving an example.

You misunderstand how the "/" operator works in python 3.x: the result is always a float. If you use "//", you'll get an integer:

>>> 346624232496199842417306589390203563969059156941566239998694701635250663711452284218031962223891107456 // 8
43328029062024980302163323673775445496132394617695779999836837704406332963931535527253995277986388432
msg272323 - (view) Author: Ethan Glass (eglass) Date: 2016-08-10 09:44
Thank you so much!
History
Date User Action Args
2022-04-11 14:58:34adminsetgithub: 71895
2016-08-10 09:44:39eglasssetmessages: + msg272323
2016-08-10 04:24:44eric.smithsettitle: Roudning/Large Int representation error with multiplication and division -> Rounding/Large Int representation error with multiplication and division
2016-08-10 04:23:18eric.smithsetstatus: open -> closed

messages: + msg272300
2016-08-10 03:40:37eglasssetstatus: closed -> open

messages: + msg272297
2016-08-09 11:32:14eric.smithsetstatus: open -> closed
resolution: not a bug
messages: + msg272235

stage: resolved
2016-08-09 10:04:13eglasssetmessages: + msg272229
2016-08-08 14:27:17steven.dapranosetmessages: + msg272168
2016-08-08 14:26:22steven.dapranosetnosy: + steven.daprano
messages: + msg272167
2016-08-08 11:21:30eric.smithsetnosy: + eric.smith
messages: + msg272163
components: + Interpreter Core, - ctypes
2016-08-08 10:57:19eglasscreate