> Then there are special cases: the Cray and Alpha
integer units, which
> don't offer divide at all. Instead, you're supposed to multiply by
> the reciprocal of the divisor.
On Fri, 25 Jun 2004, Michael Sokolov wrote:
Ahmm, but the reciprocal of an integer is not an
integer, so you are
stepping out of integer-land into floating point territory. Do you
"Most important thing to know about doing floating point arithmetic:
DON'T."
really want to use floating point to implement integer
division
(dividing an integer by an integer to get quotient and remainder)?
And how do you guarantee that the final integer result will be exact?
(I guess you convert the dividend to a float, get the reciprocal of
the divisor, which will be a float, do float multiplication, and integerise
the result, right? Are there enough bits of precision in a float to
guarantee an exact result for integer division done this way? I
doubt that, since AFAIK Alpha has usual 32-bit and 64-bit floats,
which have other things besides mantissa in those bits, but has
64-bit integers.) And how do you get the remainder? Floating division
(whether direct or via multiplication by the reciprocal) doesn't produce
a remainder at all.
REMAINDER = NUMERATOR - ((int)QUOTIENT * DENOMINATOR) ;
which adds an otherwise unnecessary integer multiply and integer
subtraction.