[racket] converting to IEEE single precision

From: Neil Van Dyke (neil at neilvandyke.org)
Date: Sat Aug 18 19:21:50 EDT 2012

How can I convert a number to IEEE single precision?

I didn't see a good way in the documentation.  Obviously this is a bad way:

(define (number->single-precision x)
   (read (open-input-string (format "~Af0" x))))

(number->single-precision 2.0) ;==> 2.0f0

I want to do this because I am reimplementing an old C extension in pure 
Racket code, and want to exactly mimic the C extension's behavior.  The 
C extension happens to sometimes produce Racket IEEE single precision 
numbers like 2.0f0, but the pure Racket code will be working with 
non-single-precision numbers like 2.0.

Neil V.

Posted on the users mailing list.