Single Percision Float on Chez-Scheme impl

A part of my Racket program requires calculating single-precision float number that follows IEEE 754. However my Racket tells me (single-flonum-available?) ; => #f. How can I bypass this? I imagine I can have such api (make-f32) (f32+ a b), etc.

The best you can do is to use flonum functions and wrap arguments and operator functions in an equation in flsingle calls. It's a pain.

> (require racket/flonum)
> (flsingle (fl+ (flsingle 1.0) (flsingle 2.5)))
3.5

You can box them into single-element f32vectors to make sure they're stored as singles.

> (require ffi/vector)
> (define val (make-f32-vector 1))
> (f32vector-set! val 0 (flsingle (fl+ (flsingle 1.0) (flsingle 2.5))))
> (f32vector-ref val 0)
3.5

The lack of single-precision flonums and to a lesser extent extflonums in Racket CS is frustrating sometimes when trying to do numerical work.

1 Like