[issue32543] odd floor division behavior

2018-01-12 Thread Tim Peters

Tim Peters  added the comment:

This report appears to be the same:

https://bugs.python.org/issue27463

One other thing to note:  the Python docs are sometimes unclear about whether 
expressions are intended to be interpreted as Python code or as mathematical 
expressions.  In:

"""
floor division will be implemented in all the Python numeric types, and will 
have the semantics of:

a // b == floor(a/b)
"""

note that it did _not_ say `math.floor(a/b)`.  `floor(a/b)` here is intended to 
be read as a mathematical (infinitely precise) specification.  As can be 
inferred from the output Serhiy posted, after the Python

a = 0.9
b = 0.1

the infinitely precise value of `a/b` is a tiny bit less than 9, so the 
infinitely precise value of `floor(a/b)` is exactly 8.  Which is what the 
Python `a // b` returns.

Personally, I wish Python 3 had changed the meaning of `//` (and `divmod()` and 
`%` - the three are deeply related) for floats, but too late for that now :-)

--

___
Python tracker 

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



[issue32543] odd floor division behavior

2018-01-12 Thread Serhiy Storchaka

Serhiy Storchaka  added the comment:

This is the result of multiple roundings.

0.9 and 0.1 can't be represented exactly as floats. They are approximated by 
binary fractions.

>>> from fractions import Fraction
>>> Fraction(0.9)
Fraction(8106479329266893, 9007199254740992)
>>> Fraction(0.1)
Fraction(3602879701896397, 36028797018963968)

The result of the division of these fractions can't be represented as a float 
too.

>>> Fraction(0.9)/Fraction(0.1)
Fraction(32425917317067572, 3602879701896397)
>>> Fraction(0.9)/Fraction(0.1) - 9
Fraction(-1, 3602879701896397)
>>> float(Fraction(0.9)/Fraction(0.1) - 9)
-2.7755575615628914e-16
>>> 9 + float(Fraction(0.9)/Fraction(0.1) - 9)
9.0

It is slightly smaller than 9, but the nearest float value is 9.0. Thus the 
result of the division is rounded up to 9.0.

A similar issue already was opened several months ago. I don't remember the 
number.

--
nosy: +serhiy.storchaka
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



[issue32543] odd floor division behavior

2018-01-12 Thread Ammar Askar

Ammar Askar  added the comment:

Looks like floor division for floats call into the divmod function, which has 
the same behavior:

>>> 0.9 .__divmod__(0.1)
(8.0, 0.09998)

--
nosy: +ammar2

___
Python tracker 

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



[issue32543] odd floor division behavior

2018-01-12 Thread Steven D'Aprano

Steven D'Aprano  added the comment:

That does look at first glance like a bug in // to me. 0.9/0.1 is correctly 
rounded to 9.0 exactly, so flooring it should return 9 (as it does):

# Python 3.5 on Linux
py> 0.9/0.1 == 9
True
py> math.floor(0.9/0.1)
9

So I too would expect that 0.9//0.1 should evaluate as 9, not 8.

--
nosy: +mark.dickinson, steven.daprano, tim.peters

___
Python tracker 

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



[issue32543] odd floor division behavior

2018-01-12 Thread Nathan Goldbaum

New submission from Nathan Goldbaum :

According to PEP 238:


"floor division will be implemented in all the Python numeric types, and will 
have the semantics of:

a // b == floor(a/b)

except that the result type will be the common type into which a and b are 
coerced before the operation."

But consider the following cases in Python 3.6.3:

>>> 0.9//0.1
8.0
>>> 0.8//0.1
8.0
>>> import math
>>> math.floor(0.9/0.1)
9
>>> math.floor(0.8/0.1)
8

Am I missing something?

--
components: Interpreter Core
messages: 309873
nosy: Nathan.Goldbaum
priority: normal
severity: normal
status: open
title: odd floor division behavior
type: behavior
versions: Python 3.6

___
Python tracker 

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