[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: floor & inexact->exact



    From: Alan Bawden <bawden@parc.xerox.com>
                           ...If you -expected- an inexact quotient, then
    you probably should have written (INEXACT->EXACT (FLOOR (/ P Q))).

  From: Robert Hieb <hieb@iuvax.cs.indiana.edu>
  Probably not.  I don't see how one can be sure that inexact->exact will
  return an integer.  To be sure one must write
  (floor (inexact->exact (/ p q))).

Although FLOOR is guaranteed to return an integer, you can't be sure
that it will return an exact integer unless its argument is an exact
integer within the (constrained but nonetheless implementation-dependent)
range of exact integers.  There is also an implementation-dependent
range within which INEXACT->EXACT is guaranteed to implement "the natural
one-to-one correspondence between inexact and exact integers".  If you
know that (/ P Q) is in that latter range, then

    (INEXACT->EXACT (FLOOR (/ P Q)))                           [1]

is guaranteed to return an exact integer.  If you know that (/ P Q) is in
the former range, then

    (FLOOR (INEXACT->EXACT (/ P Q)))                           [2]

is guaranteed to return an exact integer.  So the expression to use depends
on what you know about your implementation, what you know about (/ P Q),
and perhaps, in situations where you might not know these things, whether
[1] you'd prefer to have an exact number that might not be an integer or
[2] an integer that might not be exact.

In most implementations, the range within which INEXACT->EXACT reliably
maps inexact integers to exact integers is the same as the range in which
FLOOR is guaranteed to return an exact integer.  Expression [1], however,
is less likely to violate an implementation restriction, because
INEXACT->EXACT might barf in a typical fixnum/flonum implementation if
its argument isn't an integer.  That's why I recommend [1] over [2].

As Alan Bawden explained, you might also choose [1] because you want
the FLOOR instead of the CEILING, but if this matters greatly to you
then perhaps you should consider writing code that reports an error
whenever (/ P Q) is inexact.

Peace, Will