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
It is often useful to decompose an integer by factorization:
a = b * (a/b) + a % b
This works fine for a > 0 but gets messy when a is negative, since the math operators in C-like languages tend to either ignore the sign or truncate any fractional bits ("round to zero").
Fortran addresses the modulo issue by including both mod() for C-like modulus, and modulo() for the more integer-friendly handling.
mod(-7,5) = -|mod(7,5)| = -2
modulo(-7,5) = 3
But it is not so easy to compute floor division. To the best of my knowledge, this is the most efficient option.
e = (a - modulo(a,b)) / b
This is also common, though it unnecessarily brings floating point into the calculation.
e = floor(a / real(b))
I suppose these are fine, but they feel unintuitive to me.
I think an intrinsic could be useful here, in the same way that the mod/modulo distinction has been useful. And, perhaps, somewhere there is a CPU which does floor division, and the intrinsic could map directly onto this instruction? Ok, maybe not...
Anyway, it would be very little work to implement and would help readability, if only ever so slightly. Or is this just more intrinsic bloat?
I proposed the name idiv here but maybe not such a great suggestion since x86 idivchomps the fraction rounds to zero. But it feels like the best option.
The text was updated successfully, but these errors were encountered:
It is often useful to decompose an integer by factorization:
This works fine for
a
> 0 but gets messy whena
is negative, since the math operators in C-like languages tend to either ignore the sign or truncate any fractional bits ("round to zero").Fortran addresses the modulo issue by including both
mod()
for C-like modulus, andmodulo()
for the more integer-friendly handling.But it is not so easy to compute floor division. To the best of my knowledge, this is the most efficient option.
This is also common, though it unnecessarily brings floating point into the calculation.
I suppose these are fine, but they feel unintuitive to me.
I think an intrinsic could be useful here, in the same way that the
mod
/modulo
distinction has been useful. And, perhaps, somewhere there is a CPU which does floor division, and the intrinsic could map directly onto this instruction? Ok, maybe not...Anyway, it would be very little work to implement and would help readability, if only ever so slightly. Or is this just more intrinsic bloat?
I proposed the name
idiv
here but maybe not such a great suggestion since x86idiv
chomps the fractionrounds to zero. But it feels like the best option.The text was updated successfully, but these errors were encountered: