At Sat, 18 Feb 2012 21:39:33 -0600,
Robby Findler wrote:
> > Still, good to
> > know. I didn't think of using Typed Racket like that.
> >
> > I should mention that this is the best method I've found so far:
> >
> > (: float->int (Float -> Integer))
> > (define (float->int x)
> >  (floor (inexact->exact (floor x))))   ; both floors are necessary
> 
> Why is the second floor necessary? Just for the type system or are
> there inexact integers that become exact non-integers?

Yes.

The inner `floor' produces a float that represents an integer. TR
doesn't distinguish them from other floats, so the result of
`inexact->exact' is of type `Exact-Rational'. The outer floor takes it
to the type `Integer'.

From the type system's perspective, the inner floor is not
necessary. However, leaving it out may cause `inexact->exact' to
return fractions in some cases, which would be bad performance-wise.

From Racket's point of view, the outer floor is not necessary, since
`inexact->exact' will always return integers, on which `floor' is the
identity.

Vincent

____________________
  Racket Users list:
  http://lists.racket-lang.org/users

Reply via email to