au icon indicating copy to clipboard operation
au copied to clipboard

Create and document robust idiom for checking integer division

Open chiphogg opened this issue 2 months ago • 1 comments

It seems reasonable to expect that if a % b == ZERO, then a / unblock_int_div(b) is safe. However, this is not true in general. Consider https://godbolt.org/z/8vE3M8bf5, where the condition holds, but a / unblock_int_div(b) catastrophically truncates to zero, instead of the real solution of 3.

One takeaway for me is that unblock_int_div is a big hammer, and every usage of it deserves extra scrutiny. (Au is still far ahead of all other units libraries here, none of whom provide any protection against the dangers of integer quantity division.)

Another takeaway is that we don't have a crisp story for what to tell users to do instead. What it feels like we should do is convert the inputs to their common unit before performing the division. (This only makes sense if they have the same dimension, but the same is already true for a % b anyway.) Unfortunately, I don't know a concise and intent-forward way to express this right now.

Maybe we bring back integer_quotient, but for same-dimension types only?

I want to give this some thought, and open it up for discussion as well.

chiphogg avatar Nov 06 '25 15:11 chiphogg

More thoughts so far: https://godbolt.org/z/YPKd4e4EY.

divide_in_common_units feels better than integer_quotient.

chiphogg avatar Nov 06 '25 15:11 chiphogg