[plt-scheme] inexact->exact

From: John Clements (clements at ccs.neu.edu)
Date: Sat May 24 18:53:26 EDT 2003

On Friday, May 23, 2003, at 18:57 US/Eastern, Alex Peake wrote:

>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> Is there a faster way to convert an inexact integer (4.0) to an exact 
> integer (4)?
> (Context: I am using the result of floating point math to index into a 
> vector for a translation
> value)
> I tried inexact->exact and it is very slow. I can find no other 
> references in Help Desk (with my
> search guesses anyway).

I could very well be wrong, but I'd guess there's no built-in way to 
speed this up.  In the absence of static reasoning about the flow of 
values, there's no way that mzscheme can guarantee safety without 
performing run-time checks.

If this is a real performance issue for you (and it sounds like it is), 
your best bet is probably to write a tiny C extension that performs the 
cast for you unsafely.  To do the job right, what you'd really like is 
to write a small tool that can prove statically that the values that 
flow into this extension are always of a certain type (presumably a 
double or float). In fact, while you're at it, you might as well chop 
out another safety check by doing the vector lookup (you still need a 
bounds check, of course) while in the C code.

hope this helps,

john clements

Posted on the users mailing list.