Message315828
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. |
|
Date |
User |
Action |
Args |
2018-04-27 06:40:31 | steven.daprano | set | recipients:
+ steven.daprano, tim.peters, an0n.r00t32 |
2018-04-27 06:40:31 | steven.daprano | set | messageid: <1524811231.38.0.682650639539.issue33372@psf.upfronthosting.co.za> |
2018-04-27 06:40:31 | steven.daprano | link | issue33372 messages |
2018-04-27 06:40:30 | steven.daprano | create | |
|