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

From: Robby Findler (robby at eecs.northwestern.edu)
Date: Sat Feb 18 22:39:33 EST 2012

On Sat, Feb 18, 2012 at 9:35 PM, SF <sf.rom17 at gmail.com> wrote:
> On Sat, Feb 18, 2012 at 9:18 PM, Robby Findler wrote:
>> I'm not sure of the precise answer, but I don't think that all floats
>> (even the integers) have a corresponding fixnum.
> My C brain wonders why I can't just cast it to get the lower bits, I
> don't care about overflow.
> Using fixnum? does indeed work, just far too slowly.

I'm not sure what's really going on in your code, and this may not be
possible, but probably you want to arrange things so you don't have to
do that conversion inside any loops.

> 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?


Posted on the users mailing list.