You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Of course, what is "wrong" is a bit in the eye of the beholder:
78*6e-8 / 6e-8 == 78
# True
In any case, at least the divmod should give a remainder that is consistent with the fraction. I fear one either has to write a new inner loop or ensure the remainder is calculated using the fraction (or vice versa).
A possible cause of the problem may be a check on negative remainders done in https://github.com/numpy/numpy/blob/master/numpy/core/src/umath/loops.c.src#L1004: the remainder is increased by one unit, but the fraction would not be correspondingly decreased.
Consider a hypothetical floor_sub(78, 1e-15). Would you want it to give 78 because 78 - 1e-15 == 78 in double precision or would you want it to give the more mathematically correct answer 77?
@argriffing - I know one cannot always avoid precision/rounding errors; however, the remainder and floor division should be consistent with each other, in that if q, r = divmod(a, b), one should have q*b+r = a, at least approximately; this is violated in the numpy example I showed.
@mhvk Yes, I was responding to @pitrou's suggestion that Python's floor division is problematic. Sorry that wasn't clear in my comment! I agree that numpy has a divmod bug.
Activity
mhvk commentedon Jul 28, 2015
p.s. Bug initially found by @MatthewQuenneville
mhvk commentedon Jul 29, 2015
A bit further checking shows that
divmodjust callsfloor_divideandremainderseparately(https://github.com/numpy/numpy/blob/master/numpy/core/src/multiarray/number.c#L781), and that
floor_dividegives a different answer from python's version:Of course, what is "wrong" is a bit in the eye of the beholder:
In any case, at least the
divmodshould give a remainder that is consistent with the fraction. I fear one either has to write a new inner loop or ensure the remainder is calculated using the fraction (or vice versa).A possible cause of the problem may be a check on negative remainders done in
https://github.com/numpy/numpy/blob/master/numpy/core/src/umath/loops.c.src#L1004: the remainder is increased by one unit, but the fraction would not be correspondingly decreased.pitrou commentedon Sep 15, 2015
Given that:
It seems that Python's floor division is a bit problematic:
pitrou commentedon Sep 15, 2015
For reference, I reported a Python issue at http://bugs.python.org/issue25129
mhvk commentedon Sep 15, 2015
Not really, that could simply be a floating point rounding error, and the remainder is consistent with that.
All that matters is that one can add back, and this is fine for python:
In contrast, with a numpy array:
charris commentedon Sep 15, 2015
Looks to me like it depends on the details of the computation
charris commentedon Sep 15, 2015
nvm. Yes, it looks like numpy gets the remainder wrong.
mhvk commentedon Sep 15, 2015
I think the problem is that the two are not calculated together, so that the adjustment in the remainder is not propagated to the quotient.
pitrou commentedon Sep 15, 2015
Indeed, CPython internally has a unique divmod function that gets called by all three operations: divmod(), floor division and remainder.
argriffing commentedon Sep 15, 2015
Consider a hypothetical
floor_sub(78, 1e-15). Would you want it to give78because78 - 1e-15 == 78in double precision or would you want it to give the more mathematically correct answer77?mhvk commentedon Sep 15, 2015
@argriffing - I know one cannot always avoid precision/rounding errors; however, the remainder and floor division should be consistent with each other, in that if
q, r = divmod(a, b), one should haveq*b+r = a, at least approximately; this is violated in the numpy example I showed.argriffing commentedon Sep 15, 2015
@mhvk Yes, I was responding to @pitrou's suggestion that Python's floor division is problematic. Sorry that wasn't clear in my comment! I agree that numpy has a divmod bug.
anntzer commentedon Dec 30, 2015
Here's a case that's perhaps less contrieved, only using "non-small" numbers:
20 remaining items
charris commentedon Jan 15, 2016
I think we can get around the same path problem as long as division and
floorare repeatable. Which may not always be the case on x86, but...mhvk commentedon Jan 17, 2016
Happy to see this resolved; solution looks good!
charris commentedon Jan 17, 2016
@mhvk, I think it is actually better than Python at the moment. However, at some point it will hit the precision limits of the floats...
pitrou commentedon Feb 10, 2016
There seems to be a regression now: #7224
More precise choice of axes limits.
More precise choice of axes limits.
More precise choice of axes limits.
BUG: Make divmod behave better under roundoff error.
TST: skip broken test
TST: skip broken test
TST: skip broken test