[racket-dev] What are single flonums good for?
On Wed, Sep 12, 2012 at 11:24 AM, Vincent St-Amour <stamourv at ccs.neu.edu> wrote:
> Single-precision float support used to be enabled via a configure
> option, which meant that some Racket installations would support them,
> and some would not.
>
> Since zo files are meant to be portable, they could not contain
> single-precision floats. So, compilation would promote single literals
> to doubles.
>
> This meant that compilation could change the behavior of a program.
> Here's an example:
>
> stamourv at ahuntsic:small-float-test$ cat test2.rkt
> #lang racket
> (define (f x) (displayln (flonum? x)))
> (f (if (with-input-from-string "#t" read) 1.0f0 2.0f0))
> stamourv at ahuntsic:small-float-test$ racket test2.rkt
> #f
> stamourv at ahuntsic:small-float-test$ raco make test2.rkt
> stamourv at ahuntsic:small-float-test$ racket test2.rkt
> #t
> stamourv at ahuntsic:small-float-test$
>
> This example has to be a bit convoluted to defeat constant folding,
> which makes the problem disappear:
>
> stamourv at ahuntsic:small-float-test$ cat test.rkt
> #lang racket
> (flonum? 1.0f0)
> stamourv at ahuntsic:small-float-test$ racket test.rkt
> #f
> stamourv at ahuntsic:small-float-test$ raco make test.rkt
> stamourv at ahuntsic:small-float-test$ racket test.rkt
> #f
> stamourv at ahuntsic:small-float-test$ raco decompile test.rkt
> (begin
> (module test ....
> (#%apply-values |_print-values@(lib "racket/private/modbeg.rkt")| '#f)))
> stamourv at ahuntsic:small-float-test$
>
> This can make for pretty elusive bugs. This gets especially problematic
> when you add unsafe float operations to the mix, which can turn these
> issues into segfaults.
>
> The solution we picked was to support single-precision floats by default
> and to allow them in zos, which makes these discrepancies go away.
>
> I agree that having to handle single floats when reasoning about numbers
> complicates things, and it annoys me too. But I still think it's less
> problematic than what I describe above.
This rationale does not explain why we have single precision floats at all, tho.
Robby