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: Division "/" error on Long Integers
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Mradul, docs@python, steven.daprano
Priority: normal Keywords:

Created on 2019-12-18 11:57 by Mradul, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
Tested values.png Mradul, 2019-12-18 11:57 Image shows the values that I've tested
Messages (3)
msg358622 - (view) Author: Mradul Tiwari (Mradul) * Date: 2019-12-18 11:57
I'm a Competitive programmer and got Wrong Answer because of this division issue, which I figured out post Contest. Please See the attached screenshot.

The "/" operator gives float division but for long integers, it's giving integer answer. I've tested it on several values but the result doesn't matched with expected answers. I've also googled a lot about this but can't get the explanation.

Please go to link "https://codeforces.com/contest/1266/submission/67106918"
and see the actual arise of problem on SEVERAL VALUES, in detail section, in TestCase 3 which have very large integers.

In my code at that link, the error arises inside the function get(x) at line "c=(x-i)/14"
msg358624 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2019-12-18 12:16
Please don't post unnecessary screen shots of text. They are impossible for us to copy your code, we have to re-type it from scratch, and retyping 17-digit numbers is prone to typos. Screenshots make it difficult or impossible for the blind and visually impaired, who may be using screen readers, to contribute. Please copy and paste the actual text of your code, the results and the expected values.

You say you tested the values with a calculator, but which calculator and how do you know it is accurate?

I can't work out the connection between the competition and your calculations. Clicking on the "Problem" link takes me to a problem about dice.
msg358625 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2019-12-18 12:28
Looking at your first example, you calculate:

   >>> 63945173192649609/13
   4918859476357662.0


which is correct when using floats (64-bit C doubles). 64 bits are not enough to be any more precise, and you would get the same result in C or any other language using the same precision.

Using Decimal with the default precision, you could get:

    >>> decimal.Decimal(63945173192649609)/13
    Decimal('4918859476357662.230769230769')

but the exact result is 4918859476357662.[230769] where the decimal part inside the square brackets [...] repeats forever.

So within the limitations of the C 64-bit double floating point format, the calculation is correct and there is no bug here.

Please see

https://docs.python.org/3/faq/design.html#why-are-floating-point-calculations-so-inaccurate

for more information.
History
Date User Action Args
2022-04-11 14:59:24adminsetgithub: 83267
2019-12-18 12:28:38steven.dapranosetstatus: open -> closed
resolution: not a bug
messages: + msg358625

stage: resolved
2019-12-18 12:16:54steven.dapranosetnosy: + steven.daprano
messages: + msg358624
2019-12-18 11:57:14Mradulcreate