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.

Author steven.daprano
Recipients an0n.r00t32, steven.daprano, tim.peters
Date 2018-04-27.06:40:30
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1524811231.38.0.682650639539.issue33372@psf.upfronthosting.co.za>
In-reply-to
Content
I agree with Tim that this is likely to be the difference between Python 2 truncating division and Python 3 division.

For the record, I get the following results:

9997809507L Python 2.7
9997809307.0 Python 3.5
9997809307 R
9997809307 Javascript (Rhino)
9997809307 OpenXion

I tried it in Ruby 1.8 as well, and sometimes got a syntax error and sometimes 9999599931. I have no idea why the odd results.

I'm not mad enough to type the whole thing into my calculator, but I am mad enough to simplify it by hand, and I get:

-590072-2/10*100-200112-2/10*100-18-2/10*100-18-2/10*100-18-2/10*100-18-2/10*100-18-2/10*100-18-2/10*100-18-2/10*100-18-2/10*100+9998599835

Even Ruby gets the right answer (9997809507) for that :-)

In Python 2, 2/10 returns 0 unless you have run `from __future__ import division`, so the -2/10*100 terms all go to zero, and we get:

-590072-2/10*100-200112-2/10*100-18-2/10*100-18-2/10*100-18-2/10*100-18-2/10*100-18-2/10*100-18-2/10*100-18-2/10*100-18-2/10*100+9998599835

which evaluates to 9997809307.0 just as Python 2.7 says.

So Tim was right, this is entirely due to the difference between / as truncating division in Python 2 and true division in Python 3.

Oh, and one last thing... in Python 2, you can get the expected result by either using the future import above, or by adding a decimal point to the numbers being divided, turning them into floats and forcing Python to use floating point true division.
History
Date User Action Args
2018-04-27 06:40:31steven.dapranosetrecipients: + steven.daprano, tim.peters, an0n.r00t32
2018-04-27 06:40:31steven.dapranosetmessageid: <1524811231.38.0.682650639539.issue33372@psf.upfronthosting.co.za>
2018-04-27 06:40:31steven.dapranolinkissue33372 messages
2018-04-27 06:40:30steven.dapranocreate