[racket] Floating-point (was: RacketCon 2013 videos)

From: Konrad Hinsen (konrad.hinsen at fastmail.net)
Date: Mon Oct 14 02:15:57 EDT 2013

Neil Toronto writes:

 > > So how about Racket? Can a programmer rely on a given program producing
 > > the exact same result on all platforms, and with any Racket version?
 > 
 > In almost everything, PLT and Racket prioritize correctness over speed.

That sounds interesting. Why doesn't the Racket marketing department
do more advertising for this? It's pretty hard to find a language that
is both usable in real life and aims for correctness in float
operations.

 > More precisely, on every supported system whose FPU and C libraries' 
 > flonum functions (arithmetic, sqrt, exp, log, pow, trig) return 
 > correctly rounded results when given non-special values, every 
 > `racket/flonum' export should be perfectly IEEE 754-2008 and C99 

Nice!

 > If you want to test Racket's compliance on your system, run this:
 > 
 >    #lang racket
 >    (require math/utils)
 >    (test-floating-point 10000)
 > 
 > If it returns '(), your system is either compliant or close to it.

The bad news is that MacOS X is not very compliant. I get a list that's
far from empty:

'(((flexpt -1.4916681462400412e-154 -1.0) 1.0)
  ((flexpt 1.4916681462400412e-154 -1.0) 1.0)
  ((flexpt -1.3407807929942596e+154 -1.0) 1.0)
  ((flexpt 1.3407807929942596e+154 -1.0) 1.0)
  ((flsin -10.0) 1.0)
  ((flsin 10.0) 1.0)
  ...
  ((flatan 10.0) 1.0)
  ((fl2log 8.537625565374698e+146 -4.512338244767214e+130) 17179869183.907547)
  ((fl2log1p 2.0398056943905264e+255 5.292226095768373e+238)
   549755813887.9563))

It's always an error of 1.0 ulps, except for logarithms where the
error is much more important.

 > We know Racket is compliant because our automated testing machine DrDr 
 > runs the above test every time someone pushes a change to Racket.
 > I added that test with the math library, Matthew fixed a few errors it 
 > found, and it has passed every time since.

Sounds good, at least for the platforms that are regularly tested.
Comparing to MPFR is indeed a very nice approach.

 >   * Don't be surprised if trig functions on Windows or Mac OS X are a 
 > bit inaccurate (1-3 ulps instead of 0.5 ulps). Linux seems very solid.

Ah, I see. How about the log then? Is that a known problem as well?

 > Also, I just had an idea about how to automate at least half the 
 > debugging I talked about at RacketCon. I'm going to try it today.

Great!

Konrad.

Posted on the users mailing list.