[issue42148] floating point representation issues

2020-10-25 Thread Eric V. Smith


Eric V. Smith  added the comment:

Thanks for double-checking the other languages, Steven.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42148] floating point representation issues

2020-10-25 Thread Steven D'Aprano


Steven D'Aprano  added the comment:

It looks like Python is correct and the other languages may be just truncating 
the output.


In the Lua interpreter:

> =277*0.1
27.7
> = 277*0.1 == 27.7
false


Perl:

$ perl -e "use feature qw(say); say 277*0.1"
27.7
$ perl -e "use feature qw(say); if (277*0.1 != 27.7) {say 'unequal'}"
unequal

In Ruby:

irb(main):001:0> 277*0.1
=> 27.7
irb(main):002:0> 277*0.1 == 27.7
=> false


Julia agrees with Python:

julia> 277*0.1
27.703

So does the Rhino javascript interpreter:

js> 277*0.1
27.703

I think Eric's instinct was correct.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42148] floating point representation issues

2020-10-25 Thread Steven D'Aprano


Steven D'Aprano  added the comment:

Eric I would normally agree with you but the only thing which gives me pause is 
the statement that this doesn't occur with C, Lua and Perl.

That alone doesn't mean much. Different interpreters can use different 
algorithms for printing floats, Python changed its algorithm in 3.1 and 
backported it to 2.7:

https://docs.python.org/3.1/whatsnew/3.1.html#other-language-changes

and now uses Gay's algorithm which will often give different, shorter, results 
than other algorithms. Other languages do other things, for example R is 
notorious for just terminating the float output after a few decimal places, so 
you have numbers which look identical but are unequal.

Andrea, do you still think this is a bug? Can you demonstrate some code in 
another language that shows the output you expect? (Preferably an interpreter 
rather than C.)

Most importantly, can you demonstrate that the other language's output is 
correct and Python's is wrong? In particular, are you sure that the output in 
those other languages is not just rounding the result to a certain number of 
decimal places?

If you can satisfy those, please re-open the ticket, otherwise I expect that 
Eric is correct.

--
nosy: +steven.daprano

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42148] floating point representation issues

2020-10-25 Thread Eric V. Smith


Eric V. Smith  added the comment:

This isn't a bug. It's due to the base 2 representation of floating point 
numbers. See for example: See 
https://docs.python.org/3/tutorial/floatingpoint.html

It's possible, depending on your use case, you might want to use the decimal 
module. But that has it's own issues that you should be aware of. For example, 
1/7 is not exactly representable in either floats or Decimals.

--
nosy: +eric.smith
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42148] floating point representation issues

2020-10-25 Thread Andrea Tuccia


New submission from Andrea Tuccia :

I'm noticing some floating point representation precision issues that occurs on 
all versions and platforms:

>>> 277*0.1
27.703
>>> 1.2-1.0
0.19996
>>> import numpy as np
>>> np.double(277*0.1)
27.703
>>> np.double(1.2-1.0)
0.19996
>>> np.longdouble(277*0.1)
27.702842
>>> np.longdouble(1.2-1.0)
0.19995559

Verified with python 2.7 to 3.8. On x86 (i386 and amd64) and ARM (32 and 64 
bits). It does not occur in C, LUA, Perl, ...

--
components: Interpreter Core, Library (Lib)
messages: 379589
nosy: atuccia
priority: normal
severity: normal
status: open
title: floating point representation issues
type: behavior

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com