Skip to content

Closes #2689: Value mismatch with numpy on int(arr) // -float(arr) - #4976

Merged
ajpotts merged 1 commit into
Bears-R-Us:mainfrom
1RyanK:2689-Value_mismatch_with_numpy_on_int_arr_div_by_neg_float_arr
Oct 8, 2025
Merged

ajpotts merged 1 commit into
Bears-R-Us:mainfrom
1RyanK:2689-Value_mismatch_with_numpy_on_int_arr_div_by_neg_float_arr

Conversation

@1RyanK

@1RyanK 1RyanK commented Oct 1, 2025 •

Copy link
Copy Markdown
Contributor

If you want to use integer division to do compute $a$ divided by $b$, then we can break that down into

$$\dfrac{a}{b} = q + \dfrac{r}{b}$$

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/3 evaluates to 0.3333333333333333 in Python, but if you pry behind the scenes a little bit, you can do (1/3).as_integer_ratio() and get (6004799503160661, 18014398509481984), where 6004799503160661 * 3 is 18014398509481983, which is one less than 18014398509481984. This is perfectly fine if you decide to check 1 // (1/3) because we are integer dividing by a number slightly less than the actual 1/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 says 1/3 is). However, if you want to do the integer division, the first step is to do the float division. It does the float division of -1 by 1/3 and 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 fma function, where fma(x, y, z) is x * 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 compute

$$r = -qb + a$$

and 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)

@1RyanK 1RyanK changed the title Closes #2689: Value mismatch with numpy on int(arr) // -float(arr) Closes #2689: Value mismatch with numpy on int(arr) // -float(arr) Oct 2, 2025
@1RyanK
1RyanK force-pushed the 2689-Value_mismatch_with_numpy_on_int_arr_div_by_neg_float_arr branch 2 times, most recently from 6f37657 to 84607e3 Compare October 2, 2025 18:56
@1RyanK
1RyanK marked this pull request as ready for review October 2, 2025 19:53
Comment thread src/BinOp.chpl Resolved
@1RyanK
1RyanK force-pushed the 2689-Value_mismatch_with_numpy_on_int_arr_div_by_neg_float_arr branch 2 times, most recently from ae55d71 to 07ff8ac Compare October 6, 2025 15:19

@jaketrookman jaketrookman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

@1RyanK
1RyanK force-pushed the 2689-Value_mismatch_with_numpy_on_int_arr_div_by_neg_float_arr branch from 2edae24 to aa1141f Compare October 8, 2025 13:11
@1RyanK
1RyanK force-pushed the 2689-Value_mismatch_with_numpy_on_int_arr_div_by_neg_float_arr branch from aa1141f to 6284081 Compare October 8, 2025 13:12
@ajpotts
ajpotts enabled auto-merge October 8, 2025 14:39
@ajpotts
ajpotts added this pull request to the merge queue Oct 8, 2025
Merged via the queue into Bears-R-Us:main with commit a52037b Oct 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Value mismatch with numpy on int(arr) // -float(arr)

4 participants