Steven D'Aprano <steve+pyt...@pearwood.info> added the comment:

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.

----------
nosy: +steven.daprano

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue33372>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to