[racket] Optimization; converting 'Float' to 'Fixnum' in Typed Racket

From: Vincent St-Amour (stamourv at ccs.neu.edu)
Date: Sat Feb 18 23:11:00 EST 2012

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'.


Posted on the users mailing list.