[racket] Fun with denormalized floating point numbers

From: Michael Wilber (mwilber at uccs.edu)
Date: Wed Aug 1 11:48:25 EDT 2012

So here's something fun to look out for in your own programs.

On my slower 32-bit machine, very small numbers are much slower than
slightly less small numbers.

> (time-it (expt 2 -1000))
126 - 235
> (time-it (expt 2 -1050))
1187 - 2071

On my faster 64-bit machine, the performance difference is an order of
magnitude:

> (time-it (expt 2 -1000))
55 - 57
> (time-it (expt 2 -1050))
777 - 831

This also happens in C, Java, Python, Javascript, and presumably any
language that uses IEEE754 floating point numbers.

Careful with those small numbers!


;; fun-with-denormalized-floats.rkt ;;;;;;;;;;;;;;;;;;;;;;;;
#lang racket

(provide (all-defined-out))

(define (test value)
  ;; Perform 100,000 floating point multiplies.
  ;; (Only side effect is time)
  (let loop ([count 100000] [x value])
    (when (count . > . 0)
      (loop (sub1 count)
            (* value 0.999999999999)))))


(define (time-it start-value)
  ;; Run (test start-value) 100 times and print
  ;; 5th and 95th percentile
  (define unsorted-times
    (for/list ([i (in-range 100)])
      (define-values (results cputime realtime gctime)
        (time-apply test (list start-value)))
      realtime))
  (define times (sort unsorted-times <))
  (define 5p (list-ref times 5))
  (define 95p (list-ref times 95))
  (printf "~a - ~a\n" 5p 95p))

Posted on the users mailing list.