Repository navigation
Closes #2689: Value mismatch with numpy on int(arr) // -float(arr) - #4976
Merged
ajpotts merged 1 commit intoOct 8, 2025
Conversation
int(arr) // -float(arr)
1RyanK
force-pushed
the
2689-Value_mismatch_with_numpy_on_int_arr_div_by_neg_float_arr
branch
2 times, most recently
from
October 2, 2025 18:56
6f37657 to
84607e3
Compare
1RyanK
marked this pull request as ready for review
October 2, 2025 19:53
e-kayrakli
approved these changes
Oct 6, 2025
1RyanK
force-pushed
the
2689-Value_mismatch_with_numpy_on_int_arr_div_by_neg_float_arr
branch
2 times, most recently
from
October 6, 2025 15:19
ae55d71 to
07ff8ac
Compare
1RyanK
force-pushed
the
2689-Value_mismatch_with_numpy_on_int_arr_div_by_neg_float_arr
branch
from
October 8, 2025 13:11
2edae24 to
aa1141f
Compare
1RyanK
force-pushed
the
2689-Value_mismatch_with_numpy_on_int_arr_div_by_neg_float_arr
branch
from
October 8, 2025 13:12
aa1141f to
6284081
Compare
ajpotts
approved these changes
Oct 8, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
If you want to use integer division to do compute$a$ divided by $b$ , then we can break that down into
for some integer$q$ and (possibly non-integer) $r$ . When computing $q$ , computers can output "incorrect" numbers. This is because of floating point precision problems. For example,
1/3evaluates to0.3333333333333333in Python, but if you pry behind the scenes a little bit, you can do(1/3).as_integer_ratio()and get(6004799503160661, 18014398509481984), where6004799503160661 * 3is18014398509481983, which is one less than18014398509481984. This is perfectly fine if you decide to check1 // (1/3)because we are integer dividing by a number slightly less than the actual1/3.The problem arises when we try to do
(-1) // (1/3)because now the reality is that when we floor, we need to round down to-4(which might seem silly, but you weren't actually integer dividing by one third, you were integer dividing by whatever your computer says1/3is). However, if you want to do the integer division, the first step is to do the float division. It does the float division of-1by1/3and gets exactly-3.0. This is because the difference is so slim that it literally cannot store it in the floating point value.How do we correct this problem? C offers the
fmafunction, wherefma(x, y, z)isx * y + z. It computes with as much precision as possible and only fits it into a 64 bit real value at the very end. So we use that to computeand since$q$ is an integer, if $r$ and $b$ have different signs, the remainder term $\dfrac{r}{b}$ will be negative and we need to subtract one from $q$ , otherwise it is nonnegative and we can just return $q$ .
Closes #2689: Value mismatch with numpy on
int(arr) // -float(arr)