Debugging Floating-Point Debugging Floating-Point Debugging - - PowerPoint PPT Presentation

debugging floating point debugging floating point
SMART_READER_LITE
LIVE PREVIEW

Debugging Floating-Point Debugging Floating-Point Debugging - - PowerPoint PPT Presentation

Debugging Floating-Point Debugging Floating-Point Debugging Floating-Point Math in Racket Math in Racket Math in Racket Neil Toronto RacketCon 2013 Racket Floating-Point Support Racket Floating-Point Support Racket Floating-Point Support


slide-1
SLIDE 1

Debugging Floating-Point Debugging Floating-Point Debugging Floating-Point Math in Racket Math in Racket Math in Racket

Neil Toronto RacketCon 2013

slide-2
SLIDE 2

Racket Floating-Point Support Racket Floating-Point Support Racket Floating-Point Support

  • Fast (JIT-ed) and compliant (IEEE 754 and C99)

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

slide-3
SLIDE 3

Racket Floating-Point Support Racket Floating-Point Support Racket Floating-Point Support

  • Fast (JIT-ed) and compliant (IEEE 754 and C99)
  • More flonum (i.e. 64-bit float) functions:

math/special-functions: gamma, beta, psi,

zeta, erf, etc.

math/distributions: Gamma, Normal, etc.

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

slide-4
SLIDE 4

Racket Floating-Point Support Racket Floating-Point Support Racket Floating-Point Support

  • Fast (JIT-ed) and compliant (IEEE 754 and C99)
  • More flonum (i.e. 64-bit float) functions:

math/special-functions: gamma, beta, psi,

zeta, erf, etc.

math/distributions: Gamma, Normal, etc.

  • Other floating-point modules:

racket/extflonum: basic 80-bit operations math/bigfloat: arbitrary-precision floats

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

slide-5
SLIDE 5

Racket Floating-Point Support Racket Floating-Point Support Racket Floating-Point Support

  • Fast (JIT-ed) and compliant (IEEE 754 and C99)
  • More flonum (i.e. 64-bit float) functions:

math/special-functions: gamma, beta, psi,

zeta, erf, etc.

math/distributions: Gamma, Normal, etc.

  • Other floating-point modules:

racket/extflonum: basic 80-bit operations math/bigfloat: arbitrary-precision floats

  • math/flonum: a bunch of weird things like fl,

flnext, +max.0, flonum->ordinal, fllog1p, flsqrt1pm1, flcospix

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

slide-6
SLIDE 6

You Could Have Invented Floating-Point You Could Have Invented Floating-Point You Could Have Invented Floating-Point

Need to represent

  • r

...

2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

slide-7
SLIDE 7

You Could Have Invented Floating-Point You Could Have Invented Floating-Point You Could Have Invented Floating-Point

Need to represent

  • r

...

(struct: float ([sign : (U -1 1)] [sig : Natural] [exp : Integer]))

2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

slide-8
SLIDE 8

You Could Have Invented Floating-Point You Could Have Invented Floating-Point You Could Have Invented Floating-Point

Need to represent

  • r

...

(struct: float ([sign : (U -1 1)] [sig : Natural] [exp : Integer])) (: float->real (float -> Real)) (define (float->real x) (match-define (float s n m) x) (* s n (expt 2 m)))

2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

slide-9
SLIDE 9

You Could Have Invented Floating-Point You Could Have Invented Floating-Point You Could Have Invented Floating-Point

Need to represent

  • r

...

(struct: float ([sign : (U -1 1)] [sig : Natural] [exp : Integer])) (: float->real (float -> Real)) (define (float->real x) (match-define (float s n m) x) (* s n (expt 2 m))) > (float->real (float -1 10 0))

  • 10

2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

slide-10
SLIDE 10

You Could Have Invented Floating-Point You Could Have Invented Floating-Point You Could Have Invented Floating-Point

Need to represent

  • r

...

(struct: float ([sign : (U -1 1)] [sig : Natural] [exp : Integer])) (: float->real (float -> Real)) (define (float->real x) (match-define (float s n m) x) (* s n (expt 2 m))) > (float->real (float -1 10 0))

  • 10

> (float->real (float -1 10 3))

  • 80

2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

slide-11
SLIDE 11

You Could Have Invented Floating-Point Multiplication You Could Have Invented Floating-Point Multiplication You Could Have Invented Floating-Point Multiplication

(struct: float ([sign : (U -1 1)] [sig : Natural] [exp : Integer]))

3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3

slide-12
SLIDE 12

You Could Have Invented Floating-Point Multiplication You Could Have Invented Floating-Point Multiplication You Could Have Invented Floating-Point Multiplication

(struct: float ([sign : (U -1 1)] [sig : Natural] [exp : Integer]))

3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3

slide-13
SLIDE 13

You Could Have Invented Floating-Point Multiplication You Could Have Invented Floating-Point Multiplication You Could Have Invented Floating-Point Multiplication

(struct: float ([sign : (U -1 1)] [sig : Natural] [exp : Integer])) (: float* (float float -> float)) (define (float* x1 x2) (match-define (float s1 n1 m1) x1) (match-define (float s2 n2 m2) x2) (float (* s1 s2) (* n1 n2) (+ m1 m2)))

3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3

slide-14
SLIDE 14

You Could Have Invented Floating-Point Multiplication You Could Have Invented Floating-Point Multiplication You Could Have Invented Floating-Point Multiplication

(struct: float ([sign : (U -1 1)] [sig : Natural] [exp : Integer])) (: float* (float float -> float)) (define (float* x1 x2) (match-define (float s1 n1 m1) x1) (match-define (float s2 n2 m2) x2) (float (* s1 s2) (* n1 n2) (+ m1 m2))) > (float->real (float* (float -1 10 0) (float -1 10 3))) 800

3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3

slide-15
SLIDE 15

Finite Approximation Finite Approximation Finite Approximation

  • Actual flonum fields are fixed-size, requiring

Rounding least significant bit after operations Representations for overflow (i.e. +inf.0 and

  • inf.0) and underflow (i.e. +0.0 and -0.0)

4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4

slide-16
SLIDE 16

Finite Approximation Finite Approximation Finite Approximation

  • Actual flonum fields are fixed-size, requiring

Rounding least significant bit after operations Representations for overflow (i.e. +inf.0 and

  • inf.0) and underflow (i.e. +0.0 and -0.0)
  • Consequence: a natural well-order over flonums

> (flnext 0.0) ; from math/flonum 4.9406564584125e-324

4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4

slide-17
SLIDE 17

Finite Approximation Finite Approximation Finite Approximation

  • Actual flonum fields are fixed-size, requiring

Rounding least significant bit after operations Representations for overflow (i.e. +inf.0 and

  • inf.0) and underflow (i.e. +0.0 and -0.0)
  • Consequence: a natural well-order over flonums

> (flnext 0.0) ; from math/flonum 4.9406564584125e-324 > (list (flonum->ordinal 0.0) (flonum->ordinal +max.0)) '(0 9218868437227405311)

4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4

slide-18
SLIDE 18

Finite Approximation Finite Approximation Finite Approximation

  • Actual flonum fields are fixed-size, requiring

Rounding least significant bit after operations Representations for overflow (i.e. +inf.0 and

  • inf.0) and underflow (i.e. +0.0 and -0.0)
  • Consequence: a natural well-order over flonums

> (flnext 0.0) ; from math/flonum 4.9406564584125e-324 > (list (flonum->ordinal 0.0) (flonum->ordinal +max.0)) '(0 9218868437227405311) > (flonums-between 0.0 1.0) 4607182418800017408 ; 4.6 *quintillion*

4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4

slide-19
SLIDE 19

Finite Approximation Finite Approximation Finite Approximation

  • Actual flonum fields are fixed-size, requiring

Rounding least significant bit after operations Representations for overflow (i.e. +inf.0 and

  • inf.0) and underflow (i.e. +0.0 and -0.0)
  • Consequence: a natural well-order over flonums

> (flnext 0.0) ; from math/flonum 4.9406564584125e-324 > (list (flonum->ordinal 0.0) (flonum->ordinal +max.0)) '(0 9218868437227405311) > (flonums-between 0.0 1.0) 4607182418800017408 ; 4.6 *quintillion*

  • Consequence: most flonum functions aren’t exact

4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4

slide-20
SLIDE 20

Correctness Means Minimizing Error Correctness Means Minimizing Error Correctness Means Minimizing Error

  • A flonum’s unit in last place (ulp) is the distance

between it and the next flonum

> (flulp #i355/113) ; from math/flonum 4.440892098500626e-16

5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5

slide-21
SLIDE 21

Correctness Means Minimizing Error Correctness Means Minimizing Error Correctness Means Minimizing Error

  • A flonum’s unit in last place (ulp) is the distance

between it and the next flonum

> (flulp #i355/113) ; from math/flonum 4.440892098500626e-16

  • Error is distance from the true value, in ulps

> #i355/113 pi 3.1415929203539825 3.141592653589793

5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5

slide-22
SLIDE 22

Correctness Means Minimizing Error Correctness Means Minimizing Error Correctness Means Minimizing Error

  • A flonum’s unit in last place (ulp) is the distance

between it and the next flonum

> (flulp #i355/113) ; from math/flonum 4.440892098500626e-16

  • Error is distance from the true value, in ulps

> #i355/113 pi 3.1415929203539825 3.141592653589793 > (flulp-error #i355/113 pi) 600699552.0 ; 600.7 million ulps

5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5

slide-23
SLIDE 23

Correctness Means Minimizing Error Correctness Means Minimizing Error Correctness Means Minimizing Error

  • A flonum’s unit in last place (ulp) is the distance

between it and the next flonum

> (flulp #i355/113) ; from math/flonum 4.440892098500626e-16

  • Error is distance from the true value, in ulps

> #i355/113 pi 3.1415929203539825 3.141592653589793 > (flulp-error #i355/113 pi) 600699552.0 ; 600.7 million ulps

  • A flonum function is correctly rounded* when its
  • utputs’ maximum error is no more than 0.5 ulps

5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5

slide-24
SLIDE 24

Correctness Means Minimizing Error Correctness Means Minimizing Error Correctness Means Minimizing Error

  • A flonum’s unit in last place (ulp) is the distance

between it and the next flonum

> (flulp #i355/113) ; from math/flonum 4.440892098500626e-16

  • Error is distance from the true value, in ulps

> #i355/113 pi 3.1415929203539825 3.141592653589793 > (flulp-error #i355/113 pi) 600699552.0 ; 600.7 million ulps

  • A flonum function is correctly rounded* when its
  • utputs’ maximum error is no more than 0.5 ulps

* assuming inputs are exact; i.e. no guarantees are given for arguments with error

5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5

slide-25
SLIDE 25

Correctness Example: Subtraction Correctness Example: Subtraction Correctness Example: Subtraction

> (plot3d (contour-intervals3d (λ (x y) (let ([x (fl x)] [y (fl y)]) (define z* (- (inexact->exact x) (inexact->exact y))) (flulp-error (fl- x y) z*)))

  • 1 1 -1 1))

6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6

slide-26
SLIDE 26

Correctness Example: Subtraction Correctness Example: Subtraction Correctness Example: Subtraction

> (plot3d (contour-intervals3d (λ (x y) (let ([x (fl x)] [y (fl y)]) (define z* (- (inexact->exact x) (inexact->exact y))) (flulp-error (fl- x y) z*)))

  • 1 1 -1 1))

.1 .1 .1 .1 .1 .1 .1 .1 .1 .2 .2 .2 .2 .2 .2 .2 .2 .2 .3 .3 .3 .3 .3 .3 .3 .3 .3 .4 .4 .4 .4 .4 .4 .4 .4 .4 .5 .5 .5 .5 .5 .5 .5 .5 .5 1 1 1 1 1 1 1 1 1 .5 .5 .5 .5 .5 .5 .5 .5 .5

  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1

1 1 1 1 1 1 1 1 1 .5 .5 .5 .5 .5 .5 .5 .5 .5

  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1

x axis x axis x axis x axis x axis x axis x axis x axis x axis y axis y axis y axis y axis y axis y axis y axis y axis y axis

6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6

slide-27
SLIDE 27

Correctness Example: Logarithm Correctness Example: Logarithm Correctness Example: Logarithm

> (require math/bigfloat) ; default sig. size: 128 bits > (plot (function (λ (x) (let ([x (fl x)]) (define z* (bigfloat->real (bflog (bf x)))) (flulp-error (fllog x) z*))) 0 10))

7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7

slide-28
SLIDE 28

Correctness Example: Logarithm Correctness Example: Logarithm Correctness Example: Logarithm

> (require math/bigfloat) ; default sig. size: 128 bits > (plot (function (λ (x) (let ([x (fl x)]) (define z* (bigfloat->real (bflog (bf x)))) (flulp-error (fllog x) z*))) 0 10))

x axis x axis x axis x axis x axis x axis x axis x axis x axis y axis y axis y axis y axis y axis y axis y axis y axis y axis 2 2 2 2 2 2 2 2 2 4 4 4 4 4 4 4 4 4 6 6 6 6 6 6 6 6 6 8 8 8 8 8 8 8 8 8 10 10 10 10 10 10 10 10 10 .1 .1 .1 .1 .1 .1 .1 .1 .1 .2 .2 .2 .2 .2 .2 .2 .2 .2 .3 .3 .3 .3 .3 .3 .3 .3 .3 .4 .4 .4 .4 .4 .4 .4 .4 .4

7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7

slide-29
SLIDE 29

Correctness is Noncompositional Correctness is Noncompositional Correctness is Noncompositional

> (plot (function (λ (x) (define z* (bigfloat->real (bflog (bf x)))) (flulp-error (fllog (fl x)) z*))) 0 10)

8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8

slide-30
SLIDE 30

Correctness is Noncompositional Correctness is Noncompositional Correctness is Noncompositional

> (plot (function (λ (x) (define z* (bigfloat->real (bflog (bf x)))) (flulp-error (fllog (fl x)) z*))) 0 10)

x axis x axis x axis x axis x axis x axis x axis x axis x axis y axis y axis y axis y axis y axis y axis y axis y axis y axis 2 2 2 2 2 2 2 2 2 4 4 4 4 4 4 4 4 4 6 6 6 6 6 6 6 6 6 8 8 8 8 8 8 8 8 8 10 10 10 10 10 10 10 10 10 50 50 50 50 50 50 50 50 50 100 100 100 100 100 100 100 100 100 150 150 150 150 150 150 150 150 150 200 200 200 200 200 200 200 200 200 250 250 250 250 250 250 250 250 250

8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8

slide-31
SLIDE 31

Debugging: Geometric Inverse CDF Debugging: Geometric Inverse CDF Debugging: Geometric Inverse CDF

Implement for

9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9

slide-32
SLIDE 32

Debugging: Geometric Inverse CDF Debugging: Geometric Inverse CDF Debugging: Geometric Inverse CDF

Implement for

  • First stab:

(define (geom p u) (fl/ (fllog u) (fllog (fl- 1.0 p))))

9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9

slide-33
SLIDE 33

Debugging: Geometric Inverse CDF Debugging: Geometric Inverse CDF Debugging: Geometric Inverse CDF

Implement for

  • First stab:

(define (geom p u) (fl/ (fllog u) (fllog (fl- 1.0 p))))

  • Reference implementation:

(define (geom* p u) (let ([p (bf p)] [u (bf u)]) (bigfloat->real (bf/ (bflog u) (bflog (bf- 1.bf p))))))

9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9

slide-34
SLIDE 34

Debugging: Geometric Inverse CDF Debugging: Geometric Inverse CDF Debugging: Geometric Inverse CDF

  • Error plot for geom for p <= 1:

2 2 2 2 2 2 2 2 2 4 4 4 4 4 4 4 4 4 6 6 6 6 6 6 6 6 6 1 1 1 1 1 1 1 1 1 .8 .8 .8 .8 .8 .8 .8 .8 .8 .6 .6 .6 .6 .6 .6 .6 .6 .6 .4 .4 .4 .4 .4 .4 .4 .4 .4 .2 .2 .2 .2 .2 .2 .2 .2 .2 1 1 1 1 1 1 1 1 1 .8 .8 .8 .8 .8 .8 .8 .8 .8 .6 .6 .6 .6 .6 .6 .6 .6 .6 .4 .4 .4 .4 .4 .4 .4 .4 .4 .2 .2 .2 .2 .2 .2 .2 .2 .2 p p p p p p p p p u u u u u u u u u ulps error ulps error ulps error ulps error ulps error ulps error ulps error ulps error ulps error

10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10

slide-35
SLIDE 35

Debugging: Geometric Inverse CDF Debugging: Geometric Inverse CDF Debugging: Geometric Inverse CDF

  • Error plot for geom for p <= 0.1:

50 50 50 50 50 50 50 50 50 100 100 100 100 100 100 100 100 100 150 150 150 150 150 150 150 150 150 .1 .1 .1 .1 .1 .1 .1 .1 .1 .08 .08 .08 .08 .08 .08 .08 .08 .08 .06 .06 .06 .06 .06 .06 .06 .06 .06 .04 .04 .04 .04 .04 .04 .04 .04 .04 .02 .02 .02 .02 .02 .02 .02 .02 .02 1 1 1 1 1 1 1 1 1 .8 .8 .8 .8 .8 .8 .8 .8 .8 .6 .6 .6 .6 .6 .6 .6 .6 .6 .4 .4 .4 .4 .4 .4 .4 .4 .4 .2 .2 .2 .2 .2 .2 .2 .2 .2 p p p p p p p p p u u u u u u u u u ulps error ulps error ulps error ulps error ulps error ulps error ulps error ulps error ulps error

10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10

slide-36
SLIDE 36

Debugging: Geometric Inverse CDF Debugging: Geometric Inverse CDF Debugging: Geometric Inverse CDF

  • Error plot for geom for p <= 1e-05:

250000 250000 250000 250000 250000 250000 250000 250000 250000 5×10⁵ 5×10⁵ 5×10⁵ 5×10⁵ 5×10⁵ 5×10⁵ 5×10⁵ 5×10⁵ 5×10⁵ 750000 750000 750000 750000 750000 750000 750000 750000 750000 1×10⁶ 1×10⁶ 1×10⁶ 1×10⁶ 1×10⁶ 1×10⁶ 1×10⁶ 1×10⁶ 1×10⁶ .00001 .00001 .00001 .00001 .00001 .00001 .00001 .00001 .00001 8×10⁻⁶ 8×10⁻⁶ 8×10⁻⁶ 8×10⁻⁶ 8×10⁻⁶ 8×10⁻⁶ 8×10⁻⁶ 8×10⁻⁶ 8×10⁻⁶ 6×10⁻⁶ 6×10⁻⁶ 6×10⁻⁶ 6×10⁻⁶ 6×10⁻⁶ 6×10⁻⁶ 6×10⁻⁶ 6×10⁻⁶ 6×10⁻⁶ 4×10⁻⁶ 4×10⁻⁶ 4×10⁻⁶ 4×10⁻⁶ 4×10⁻⁶ 4×10⁻⁶ 4×10⁻⁶ 4×10⁻⁶ 4×10⁻⁶ 2×10⁻⁶ 2×10⁻⁶ 2×10⁻⁶ 2×10⁻⁶ 2×10⁻⁶ 2×10⁻⁶ 2×10⁻⁶ 2×10⁻⁶ 2×10⁻⁶ 1 1 1 1 1 1 1 1 1 .8 .8 .8 .8 .8 .8 .8 .8 .8 .6 .6 .6 .6 .6 .6 .6 .6 .6 .4 .4 .4 .4 .4 .4 .4 .4 .4 .2 .2 .2 .2 .2 .2 .2 .2 .2 p p p p p p p p p u u u u u u u u u ulps error ulps error ulps error ulps error ulps error ulps error ulps error ulps error ulps error

10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10

slide-37
SLIDE 37

Debugging: Geometric Inverse CDF Debugging: Geometric Inverse CDF Debugging: Geometric Inverse CDF

  • Error plot for geom for p <= 1e-10:

2×10⁸ 2×10⁸ 2×10⁸ 2×10⁸ 2×10⁸ 2×10⁸ 2×10⁸ 2×10⁸ 2×10⁸ 4×10⁸ 4×10⁸ 4×10⁸ 4×10⁸ 4×10⁸ 4×10⁸ 4×10⁸ 4×10⁸ 4×10⁸ 6×10⁸ 6×10⁸ 6×10⁸ 6×10⁸ 6×10⁸ 6×10⁸ 6×10⁸ 6×10⁸ 6×10⁸ 1×10⁻¹⁰ 1×10⁻¹⁰ 1×10⁻¹⁰ 1×10⁻¹⁰ 1×10⁻¹⁰ 1×10⁻¹⁰ 1×10⁻¹⁰ 1×10⁻¹⁰ 1×10⁻¹⁰ 8×10⁻¹¹ 8×10⁻¹¹ 8×10⁻¹¹ 8×10⁻¹¹ 8×10⁻¹¹ 8×10⁻¹¹ 8×10⁻¹¹ 8×10⁻¹¹ 8×10⁻¹¹ 6×10⁻¹¹ 6×10⁻¹¹ 6×10⁻¹¹ 6×10⁻¹¹ 6×10⁻¹¹ 6×10⁻¹¹ 6×10⁻¹¹ 6×10⁻¹¹ 6×10⁻¹¹ 4×10⁻¹¹ 4×10⁻¹¹ 4×10⁻¹¹ 4×10⁻¹¹ 4×10⁻¹¹ 4×10⁻¹¹ 4×10⁻¹¹ 4×10⁻¹¹ 4×10⁻¹¹ 2×10⁻¹¹ 2×10⁻¹¹ 2×10⁻¹¹ 2×10⁻¹¹ 2×10⁻¹¹ 2×10⁻¹¹ 2×10⁻¹¹ 2×10⁻¹¹ 2×10⁻¹¹ 1 1 1 1 1 1 1 1 1 .8 .8 .8 .8 .8 .8 .8 .8 .8 .6 .6 .6 .6 .6 .6 .6 .6 .6 .4 .4 .4 .4 .4 .4 .4 .4 .4 .2 .2 .2 .2 .2 .2 .2 .2 .2 p p p p p p p p p u u u u u u u u u ulps error ulps error ulps error ulps error ulps error ulps error ulps error ulps error ulps error

10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10

slide-38
SLIDE 38

Argh! Argh! Argh!

  • Q. Is this normal???
  • A. Yes. Most straightforward flonum function

implementations have low error most places and high error (often unbounded) in others.

11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11

slide-39
SLIDE 39

Argh! Argh! Argh!

  • Q. Is this normal???
  • A. Yes. Most straightforward flonum function

implementations have low error most places and high error (often unbounded) in others. The good news: You can usually fix them using just flonum ops.

11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11

slide-40
SLIDE 40

Argh! Argh! Argh!

  • Q. Is this normal???
  • A. Yes. Most straightforward flonum function

implementations have low error most places and high error (often unbounded) in others. The good news: You can usually fix them using just flonum ops.

  • Q. How do I fix them???
  • A. Most functions—not implementations, but

functions themselves—have ill-conditioned places where they turn low input error into high output

  • error. Avoid these badlands.

11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11

slide-41
SLIDE 41

The Floating-Point Priest Says... The Floating-Point Priest Says... The Floating-Point Priest Says...

“The condition number of a function is the absolute value of the ratio of its derivative and its value, multiplied by the blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah.”

12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12

slide-42
SLIDE 42

The Badlands: Subtraction The Badlands: Subtraction The Badlands: Subtraction

> (plot3d (contour-intervals3d (λ (x y) (define z* (- x y)) (flulp-error (fl- (fl x) (fl y)) z*))

  • 1 1 -1 1))

13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13

slide-43
SLIDE 43

The Badlands: Subtraction The Badlands: Subtraction The Badlands: Subtraction

> (plot3d (contour-intervals3d (λ (x y) (define z* (- x y)) (flulp-error (fl- (fl x) (fl y)) z*))

  • 1 1 -1 1))

2 2 2 2 2 2 2 2 2 4 4 4 4 4 4 4 4 4 6 6 6 6 6 6 6 6 6 8 8 8 8 8 8 8 8 8 1 1 1 1 1 1 1 1 1 .5 .5 .5 .5 .5 .5 .5 .5 .5

  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1

1 1 1 1 1 1 1 1 1 .5 .5 .5 .5 .5 .5 .5 .5 .5

  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1

x a x i s x a x i s x a x i s x a x i s x a x i s x a x i s x a x i s x a x i s x a x i s y axis y axis y axis y axis y axis y axis y axis y axis y axis

13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13

slide-44
SLIDE 44

The Badlands: Logarithm The Badlands: Logarithm The Badlands: Logarithm

> (plot (function (λ (x) (define z* (bigfloat->real (bflog (bf x)))) (flulp-error (fllog (fl x)) z*))) 0 10)

14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14

slide-45
SLIDE 45

The Badlands: Logarithm The Badlands: Logarithm The Badlands: Logarithm

> (plot (function (λ (x) (define z* (bigfloat->real (bflog (bf x)))) (flulp-error (fllog (fl x)) z*))) 0 10)

x axis x axis x axis x axis x axis x axis x axis x axis x axis y axis y axis y axis y axis y axis y axis y axis y axis y axis 2 2 2 2 2 2 2 2 2 4 4 4 4 4 4 4 4 4 6 6 6 6 6 6 6 6 6 8 8 8 8 8 8 8 8 8 10 10 10 10 10 10 10 10 10 50 50 50 50 50 50 50 50 50 100 100 100 100 100 100 100 100 100 150 150 150 150 150 150 150 150 150 200 200 200 200 200 200 200 200 200 250 250 250 250 250 250 250 250 250

14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14

slide-46
SLIDE 46

The Badlands: Division The Badlands: Division The Badlands: Division

.25 .25 .25 .25 .25 .25 .25 .25 .25 .5 .5 .5 .5 .5 .5 .5 .5 .5 .75 .75 .75 .75 .75 .75 .75 .75 .75 1 1 1 1 1 1 1 1 1 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1 1 1 1 1 1 1 1 1 .5 .5 .5 .5 .5 .5 .5 .5 .5

  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1

1 1 1 1 1 1 1 1 1 .5 .5 .5 .5 .5 .5 .5 .5 .5

  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1

x a x i s x a x i s x a x i s x a x i s x a x i s x a x i s x a x i s x a x i s x a x i s y axis y axis y axis y axis y axis y axis y axis y axis y axis

15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15

slide-47
SLIDE 47

The Badlands: Division The Badlands: Division The Badlands: Division

.25 .25 .25 .25 .25 .25 .25 .25 .25 .5 .5 .5 .5 .5 .5 .5 .5 .5 .75 .75 .75 .75 .75 .75 .75 .75 .75 1 1 1 1 1 1 1 1 1 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1 1 1 1 1 1 1 1 1 .5 .5 .5 .5 .5 .5 .5 .5 .5

  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1

1 1 1 1 1 1 1 1 1 .5 .5 .5 .5 .5 .5 .5 .5 .5

  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1

x a x i s x a x i s x a x i s x a x i s x a x i s x a x i s x a x i s x a x i s x a x i s y axis y axis y axis y axis y axis y axis y axis y axis y axis

  • No badlands: except for flonum rounding error,

division error doesn’t depend on inputs

15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15

slide-48
SLIDE 48

The Badlands: Division The Badlands: Division The Badlands: Division

.25 .25 .25 .25 .25 .25 .25 .25 .25 .5 .5 .5 .5 .5 .5 .5 .5 .5 .75 .75 .75 .75 .75 .75 .75 .75 .75 1 1 1 1 1 1 1 1 1 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1 1 1 1 1 1 1 1 1 .5 .5 .5 .5 .5 .5 .5 .5 .5

  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1

1 1 1 1 1 1 1 1 1 .5 .5 .5 .5 .5 .5 .5 .5 .5

  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1

x a x i s x a x i s x a x i s x a x i s x a x i s x a x i s x a x i s x a x i s x a x i s y axis y axis y axis y axis y axis y axis y axis y axis y axis

  • No badlands: except for flonum rounding error,

division error doesn’t depend on inputs

  • Multiplication error is the same way

15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15

slide-49
SLIDE 49

Informal Error Analysis Informal Error Analysis Informal Error Analysis

  • Recursively reason about the body of geom:

(define (geom p u) (fl/ (fllog u) (fllog (fl- 1.0 p))))

16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16

slide-50
SLIDE 50

Informal Error Analysis Informal Error Analysis Informal Error Analysis

  • Recursively reason about the body of geom:

(define (geom p u) (fl/ (fllog u) (fllog (fl- 1.0 p))))

  • Can’t do anything about fl/ except make sure its

arguments are as accurate as possible

16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16

slide-51
SLIDE 51

Informal Error Analysis Informal Error Analysis Informal Error Analysis

  • Recursively reason about the body of geom:

(define (geom p u) (fl/ (fllog u) (fllog (fl- 1.0 p))))

  • Can’t do anything about fl/ except make sure its

arguments are as accurate as possible

  • If u is exact, (fllog u) has <= 0.5 ulps error

16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16

slide-52
SLIDE 52

Informal Error Analysis Informal Error Analysis Informal Error Analysis

  • Recursively reason about the body of geom:

(define (geom p u) (fl/ (fllog u) (fllog (fl- 1.0 p))))

  • Can’t do anything about fl/ except make sure its

arguments are as accurate as possible

  • If u is exact, (fllog u) has <= 0.5 ulps error
  • If p is exact, (fl- 1.0 p) has <= 0.5 ulps error

16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16

slide-53
SLIDE 53

Informal Error Analysis Informal Error Analysis Informal Error Analysis

  • Recursively reason about the body of geom:

(define (geom p u) (fl/ (fllog u) (fllog (fl- 1.0 p))))

  • Can’t do anything about fl/ except make sure its

arguments are as accurate as possible

  • If u is exact, (fllog u) has <= 0.5 ulps error
  • If p is exact, (fl- 1.0 p) has <= 0.5 ulps error
  • If p is exact and near 0.0...

16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16

slide-54
SLIDE 54

Informal Error Analysis Informal Error Analysis Informal Error Analysis

  • Recursively reason about the body of geom:

(define (geom p u) (fl/ (fllog u) (fllog (fl- 1.0 p))))

  • Can’t do anything about fl/ except make sure its

arguments are as accurate as possible

  • If u is exact, (fllog u) has <= 0.5 ulps error
  • If p is exact, (fl- 1.0 p) has <= 0.5 ulps error
  • If p is exact and near 0.0...

Then (fl- 1.0 p) is inexact and near 1.0...

16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16

slide-55
SLIDE 55

Informal Error Analysis Informal Error Analysis Informal Error Analysis

  • Recursively reason about the body of geom:

(define (geom p u) (fl/ (fllog u) (fllog (fl- 1.0 p))))

  • Can’t do anything about fl/ except make sure its

arguments are as accurate as possible

  • If u is exact, (fllog u) has <= 0.5 ulps error
  • If p is exact, (fl- 1.0 p) has <= 0.5 ulps error
  • If p is exact and near 0.0...

Then (fl- 1.0 p) is inexact and near 1.0... So (fllog (fl- 1.0 p)) may have high error

16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16

slide-56
SLIDE 56

Informal Error Analysis Informal Error Analysis Informal Error Analysis

  • Recursively reason about the body of geom:

(define (geom p u) (fl/ (fllog u) (fllog (fl- 1.0 p))))

  • Can’t do anything about fl/ except make sure its

arguments are as accurate as possible

  • If u is exact, (fllog u) has <= 0.5 ulps error
  • If p is exact, (fl- 1.0 p) has <= 0.5 ulps error
  • If p is exact and near 0.0...

Then (fl- 1.0 p) is inexact and near 1.0... So (fllog (fl- 1.0 p)) may have high error

  • Let’s check math/flonum for another incantation...

16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16

slide-57
SLIDE 57

log(1+x) log(1+x) log(1+x)

  • Looks interesting: fllog1p

x x x x x x x x x log(1+x) log(1+x) log(1+x) log(1+x) log(1+x) log(1+x) log(1+x) log(1+x) log(1+x) 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 5 5 5 5 5 5 5 5 5 7.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2

2 2 2 2 2 2 2 2 2

17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17

slide-58
SLIDE 58

log(1+x) log(1+x) log(1+x)

  • Looks interesting: fllog1p

x x x x x x x x x log(1+x) log(1+x) log(1+x) log(1+x) log(1+x) log(1+x) log(1+x) log(1+x) log(1+x) 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 5 5 5 5 5 5 5 5 5 7.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2

2 2 2 2 2 2 2 2 2

  • We can use it almost directly—mathematically,

17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17

slide-59
SLIDE 59

Debugging: Geometric Inverse CDF (Second Stab) Debugging: Geometric Inverse CDF (Second Stab) Debugging: Geometric Inverse CDF (Second Stab)

(define (geom p u) (fl/ (fllog u) (fllog1p (- p))))

18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18

slide-60
SLIDE 60

Debugging: Geometric Inverse CDF (Second Stab) Debugging: Geometric Inverse CDF (Second Stab) Debugging: Geometric Inverse CDF (Second Stab)

(define (geom p u) (fl/ (fllog u) (fllog1p (- p))))

  • Error plot for geom for p <= 1:

.5 .5 .5 .5 .5 .5 .5 .5 .5 1 1 1 1 1 1 1 1 1 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1 1 1 1 1 1 1 1 1 .8 .8 .8 .8 .8 .8 .8 .8 .8 .6 .6 .6 .6 .6 .6 .6 .6 .6 .4 .4 .4 .4 .4 .4 .4 .4 .4 .2 .2 .2 .2 .2 .2 .2 .2 .2 1 1 1 1 1 1 1 1 1 .8 .8 .8 .8 .8 .8 .8 .8 .8 .6 .6 .6 .6 .6 .6 .6 .6 .6 .4 .4 .4 .4 .4 .4 .4 .4 .4 .2 .2 .2 .2 .2 .2 .2 .2 .2 p p p p p p p p p u u u u u u u u u ulps error ulps error ulps error ulps error ulps error ulps error ulps error ulps error ulps error

18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18

slide-61
SLIDE 61

Debugging: Geometric Inverse CDF (Second Stab) Debugging: Geometric Inverse CDF (Second Stab) Debugging: Geometric Inverse CDF (Second Stab)

(define (geom p u) (fl/ (fllog u) (fllog1p (- p))))

  • Error plot for geom for p <= 0.1:

.5 .5 .5 .5 .5 .5 .5 .5 .5 1 1 1 1 1 1 1 1 1 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 2 2 2 2 2 2 2 2 2 .1 .1 .1 .1 .1 .1 .1 .1 .1 .08 .08 .08 .08 .08 .08 .08 .08 .08 .06 .06 .06 .06 .06 .06 .06 .06 .06 .04 .04 .04 .04 .04 .04 .04 .04 .04 .02 .02 .02 .02 .02 .02 .02 .02 .02 1 1 1 1 1 1 1 1 1 .8 .8 .8 .8 .8 .8 .8 .8 .8 .6 .6 .6 .6 .6 .6 .6 .6 .6 .4 .4 .4 .4 .4 .4 .4 .4 .4 .2 .2 .2 .2 .2 .2 .2 .2 .2 p p p p p p p p p u u u u u u u u u ulps error ulps error ulps error ulps error ulps error ulps error ulps error ulps error ulps error

18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18

slide-62
SLIDE 62

Debugging: Geometric Inverse CDF (Second Stab) Debugging: Geometric Inverse CDF (Second Stab) Debugging: Geometric Inverse CDF (Second Stab)

(define (geom p u) (fl/ (fllog u) (fllog1p (- p))))

  • Error plot for geom for p <= 1e-05:

.5 .5 .5 .5 .5 .5 .5 .5 .5 1 1 1 1 1 1 1 1 1 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 2 2 2 2 2 2 2 2 2 .00001 .00001 .00001 .00001 .00001 .00001 .00001 .00001 .00001 8×10⁻⁶ 8×10⁻⁶ 8×10⁻⁶ 8×10⁻⁶ 8×10⁻⁶ 8×10⁻⁶ 8×10⁻⁶ 8×10⁻⁶ 8×10⁻⁶ 6×10⁻⁶ 6×10⁻⁶ 6×10⁻⁶ 6×10⁻⁶ 6×10⁻⁶ 6×10⁻⁶ 6×10⁻⁶ 6×10⁻⁶ 6×10⁻⁶ 4×10⁻⁶ 4×10⁻⁶ 4×10⁻⁶ 4×10⁻⁶ 4×10⁻⁶ 4×10⁻⁶ 4×10⁻⁶ 4×10⁻⁶ 4×10⁻⁶ 2×10⁻⁶ 2×10⁻⁶ 2×10⁻⁶ 2×10⁻⁶ 2×10⁻⁶ 2×10⁻⁶ 2×10⁻⁶ 2×10⁻⁶ 2×10⁻⁶ 1 1 1 1 1 1 1 1 1 .8 .8 .8 .8 .8 .8 .8 .8 .8 .6 .6 .6 .6 .6 .6 .6 .6 .6 .4 .4 .4 .4 .4 .4 .4 .4 .4 .2 .2 .2 .2 .2 .2 .2 .2 .2 p p p p p p p p p u u u u u u u u u ulps error ulps error ulps error ulps error ulps error ulps error ulps error ulps error ulps error

18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18

slide-63
SLIDE 63

Debugging: Geometric Inverse CDF (Second Stab) Debugging: Geometric Inverse CDF (Second Stab) Debugging: Geometric Inverse CDF (Second Stab)

(define (geom p u) (fl/ (fllog u) (fllog1p (- p))))

  • Error plot for geom for p <= 1e-10:

.5 .5 .5 .5 .5 .5 .5 .5 .5 1 1 1 1 1 1 1 1 1 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1×10⁻¹⁰ 1×10⁻¹⁰ 1×10⁻¹⁰ 1×10⁻¹⁰ 1×10⁻¹⁰ 1×10⁻¹⁰ 1×10⁻¹⁰ 1×10⁻¹⁰ 1×10⁻¹⁰ 8×10⁻¹¹ 8×10⁻¹¹ 8×10⁻¹¹ 8×10⁻¹¹ 8×10⁻¹¹ 8×10⁻¹¹ 8×10⁻¹¹ 8×10⁻¹¹ 8×10⁻¹¹ 6×10⁻¹¹ 6×10⁻¹¹ 6×10⁻¹¹ 6×10⁻¹¹ 6×10⁻¹¹ 6×10⁻¹¹ 6×10⁻¹¹ 6×10⁻¹¹ 6×10⁻¹¹ 4×10⁻¹¹ 4×10⁻¹¹ 4×10⁻¹¹ 4×10⁻¹¹ 4×10⁻¹¹ 4×10⁻¹¹ 4×10⁻¹¹ 4×10⁻¹¹ 4×10⁻¹¹ 2×10⁻¹¹ 2×10⁻¹¹ 2×10⁻¹¹ 2×10⁻¹¹ 2×10⁻¹¹ 2×10⁻¹¹ 2×10⁻¹¹ 2×10⁻¹¹ 2×10⁻¹¹ 1 1 1 1 1 1 1 1 1 .8 .8 .8 .8 .8 .8 .8 .8 .8 .6 .6 .6 .6 .6 .6 .6 .6 .6 .4 .4 .4 .4 .4 .4 .4 .4 .4 .2 .2 .2 .2 .2 .2 .2 .2 .2 p p p p p p p p p u u u u u u u u u ulps error ulps error ulps error ulps error ulps error ulps error ulps error ulps error ulps error

18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18

slide-64
SLIDE 64

Argh It Is Not Perfect! Argh It Is Not Perfect! Argh It Is Not Perfect!

  • But < 3 ulps error is very accurate

19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19

slide-65
SLIDE 65

Argh It Is Not Perfect! Argh It Is Not Perfect! Argh It Is Not Perfect!

  • But < 3 ulps error is very accurate
  • Does geom have badlands?

19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19

slide-66
SLIDE 66

Argh It Is Not Perfect! Argh It Is Not Perfect! Argh It Is Not Perfect!

  • But < 3 ulps error is very accurate
  • Does geom have badlands?

2 2 2 2 2 2 2 2 2 4 4 4 4 4 4 4 4 4 6 6 6 6 6 6 6 6 6 8 8 8 8 8 8 8 8 8 1 1 1 1 1 1 1 1 1 .8 .8 .8 .8 .8 .8 .8 .8 .8 .6 .6 .6 .6 .6 .6 .6 .6 .6 .4 .4 .4 .4 .4 .4 .4 .4 .4 .2 .2 .2 .2 .2 .2 .2 .2 .2 1 1 1 1 1 1 1 1 1 .8 .8 .8 .8 .8 .8 .8 .8 .8 .6 .6 .6 .6 .6 .6 .6 .6 .6 .4 .4 .4 .4 .4 .4 .4 .4 .4 .2 .2 .2 .2 .2 .2 .2 .2 .2 p p p p p p p p p u u u u u u u u u ulps error ulps error ulps error ulps error ulps error ulps error ulps error ulps error ulps error

19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19

slide-67
SLIDE 67

Argh It Is Not Perfect! Argh It Is Not Perfect! Argh It Is Not Perfect!

  • But < 3 ulps error is very accurate
  • Does geom have badlands?

2 2 2 2 2 2 2 2 2 4 4 4 4 4 4 4 4 4 6 6 6 6 6 6 6 6 6 8 8 8 8 8 8 8 8 8 1 1 1 1 1 1 1 1 1 .8 .8 .8 .8 .8 .8 .8 .8 .8 .6 .6 .6 .6 .6 .6 .6 .6 .6 .4 .4 .4 .4 .4 .4 .4 .4 .4 .2 .2 .2 .2 .2 .2 .2 .2 .2 1 1 1 1 1 1 1 1 1 .8 .8 .8 .8 .8 .8 .8 .8 .8 .6 .6 .6 .6 .6 .6 .6 .6 .6 .4 .4 .4 .4 .4 .4 .4 .4 .4 .2 .2 .2 .2 .2 .2 .2 .2 .2 p p p p p p p p p u u u u u u u u u ulps error ulps error ulps error ulps error ulps error ulps error ulps error ulps error ulps error

  • This is a property of the function, so we can’t do

anything about it

19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19

slide-68
SLIDE 68

Debugging Summary Debugging Summary Debugging Summary

  • Make direct and reference implementations

20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

slide-69
SLIDE 69

Debugging Summary Debugging Summary Debugging Summary

  • Make direct and reference implementations
  • Repeat:

Plot error, identify high-error regions Find badlands inputs, replace computations

20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

slide-70
SLIDE 70

Debugging Summary Debugging Summary Debugging Summary

  • Make direct and reference implementations
  • Repeat:

Plot error, identify high-error regions Find badlands inputs, replace computations

  • Avoid:

Subtracting nearby values Taking logs of values near 1.0 Other badlands (most zero crossings, exponential growth)

20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

slide-71
SLIDE 71

Debugging Summary Debugging Summary Debugging Summary

  • Make direct and reference implementations
  • Repeat:

Plot error, identify high-error regions Find badlands inputs, replace computations

  • Avoid:

Subtracting nearby values Taking logs of values near 1.0 Other badlands (most zero crossings, exponential growth)

  • Move multiplication and division outward

20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

slide-72
SLIDE 72

What If I Need Moar Bits? What If I Need Moar Bits? What If I Need Moar Bits?

  • racket/extflonum: 80-bit extended flonums

Requires (extflonum-available?) = #t 64-bit significand, 15-bit exponent

(extfl->exact (extflexp (real->extfl 1/7)))

21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21

slide-73
SLIDE 73

What If I Need Moar Bits? What If I Need Moar Bits? What If I Need Moar Bits?

  • racket/extflonum: 80-bit extended flonums

Requires (extflonum-available?) = #t 64-bit significand, 15-bit exponent

(extfl->exact (extflexp (real->extfl 1/7)))

  • double-double flonums: sum of two

nonoverlapping flonums represent a number Requires correctly rounded arithmetic ~105-bit significand, 11-bit exponent

(let*-values ([(x2 x1) (fl2 1/7)] [(y2 y1) (fl2exp x2 x1)]) (fl2->real y2 y1))

21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21

slide-74
SLIDE 74

What If I Need Moar Bits? What If I Need Moar Bits? What If I Need Moar Bits?

  • math/bigfloat: arbitrary precision floats

Requires MPFR (Racket ships with libraries) Any size significand, 32-bit exponent

22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22

slide-75
SLIDE 75

What If I Need Moar Bits? What If I Need Moar Bits? What If I Need Moar Bits?

  • math/bigfloat: arbitrary precision floats

Requires MPFR (Racket ships with libraries) Any size significand, 32-bit exponent

  • Exact rationals

22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22

slide-76
SLIDE 76

What If I Need Moar Bits? What If I Need Moar Bits? What If I Need Moar Bits?

  • math/bigfloat: arbitrary precision floats

Requires MPFR (Racket ships with libraries) Any size significand, 32-bit exponent

  • Exact rationals
  • AN IMPORTANT DATA POINT:

22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22

slide-77
SLIDE 77

What If I Need Moar Bits? What If I Need Moar Bits? What If I Need Moar Bits?

  • math/bigfloat: arbitrary precision floats

Requires MPFR (Racket ships with libraries) Any size significand, 32-bit exponent

  • Exact rationals
  • AN IMPORTANT DATA POINT:

It takes 1074-bit bigfloats to fix geom It takes 1074-bit bigfloats to fix geom It takes 1074-bit bigfloats to fix geom using (bflog (bf- 1.bf p)) using (bflog (bf- 1.bf p)) using (bflog (bf- 1.bf p))

22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22

slide-78
SLIDE 78

What If I Need Moar Bits? What If I Need Moar Bits? What If I Need Moar Bits?

  • math/bigfloat: arbitrary precision floats

Requires MPFR (Racket ships with libraries) Any size significand, 32-bit exponent

  • Exact rationals
  • AN IMPORTANT DATA POINT:

It takes 1074-bit bigfloats to fix geom It takes 1074-bit bigfloats to fix geom It takes 1074-bit bigfloats to fix geom using (bflog (bf- 1.bf p)) using (bflog (bf- 1.bf p)) using (bflog (bf- 1.bf p))

  • Conclusion: “moar bits” is not a general solution

22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22

slide-79
SLIDE 79

The Badlands: Square Oot The Badlands: Square Oot The Badlands: Square Oot

> (plot (function (λ (x) (define z* (bigfloat->real (bfsqrt (bf x)))) (flulp-error (flsqrt (fl x)) z*)) 0 1e-320))

x axis x axis x axis x axis x axis x axis x axis x axis x axis y axis y axis y axis y axis y axis y axis y axis y axis y axis 2×10⁻³²¹ 2×10⁻³²¹ 2×10⁻³²¹ 2×10⁻³²¹ 2×10⁻³²¹ 2×10⁻³²¹ 2×10⁻³²¹ 2×10⁻³²¹ 2×10⁻³²¹ 4×10⁻³²¹ 4×10⁻³²¹ 4×10⁻³²¹ 4×10⁻³²¹ 4×10⁻³²¹ 4×10⁻³²¹ 4×10⁻³²¹ 4×10⁻³²¹ 4×10⁻³²¹ 6×10⁻³²¹ 6×10⁻³²¹ 6×10⁻³²¹ 6×10⁻³²¹ 6×10⁻³²¹ 6×10⁻³²¹ 6×10⁻³²¹ 6×10⁻³²¹ 6×10⁻³²¹ 8×10⁻³²¹ 8×10⁻³²¹ 8×10⁻³²¹ 8×10⁻³²¹ 8×10⁻³²¹ 8×10⁻³²¹ 8×10⁻³²¹ 8×10⁻³²¹ 8×10⁻³²¹ 1×10¹³ 1×10¹³ 1×10¹³ 1×10¹³ 1×10¹³ 1×10¹³ 1×10¹³ 1×10¹³ 1×10¹³ 2×10¹³ 2×10¹³ 2×10¹³ 2×10¹³ 2×10¹³ 2×10¹³ 2×10¹³ 2×10¹³ 2×10¹³ 3×10¹³ 3×10¹³ 3×10¹³ 3×10¹³ 3×10¹³ 3×10¹³ 3×10¹³ 3×10¹³ 3×10¹³ 4×10¹³ 4×10¹³ 4×10¹³ 4×10¹³ 4×10¹³ 4×10¹³ 4×10¹³ 4×10¹³ 4×10¹³ 5×10¹³ 5×10¹³ 5×10¹³ 5×10¹³ 5×10¹³ 5×10¹³ 5×10¹³ 5×10¹³ 5×10¹³

slide-80
SLIDE 80

The Badlands: Sine The Badlands: Sine The Badlands: Sine

> (plot (function (λ (x) (define z* (bigfloat->real (bfsin (bf x)))) (flulp-error (flsin (fl x)) z*)) 0 (* 2 pi)))

x axis x axis x axis x axis x axis x axis x axis x axis x axis y axis y axis y axis y axis y axis y axis y axis y axis y axis 2 2 2 2 2 2 2 2 2 4 4 4 4 4 4 4 4 4 6 6 6 6 6 6 6 6 6 100 100 100 100 100 100 100 100 100 200 200 200 200 200 200 200 200 200 300 300 300 300 300 300 300 300 300

slide-81
SLIDE 81

The Badlands: Cosine The Badlands: Cosine The Badlands: Cosine

> (plot (function (λ (x) (define z* (bigfloat->real (bfcos (bf x)))) (flulp-error (flcos (fl x)) z*)) 0 (* 2 pi)))

x axis x axis x axis x axis x axis x axis x axis x axis x axis y axis y axis y axis y axis y axis y axis y axis y axis y axis 2 2 2 2 2 2 2 2 2 4 4 4 4 4 4 4 4 4 6 6 6 6 6 6 6 6 6 200 200 200 200 200 200 200 200 200 400 400 400 400 400 400 400 400 400 600 600 600 600 600 600 600 600 600 800 800 800 800 800 800 800 800 800

slide-82
SLIDE 82

The Badlands: Tangent The Badlands: Tangent The Badlands: Tangent

> (plot (function (λ (x) (define z* (bigfloat->real (bftan (bf x)))) (flulp-error (fltan (fl x)) z*)) 0 (* 2 pi)))

x axis x axis x axis x axis x axis x axis x axis x axis x axis y axis y axis y axis y axis y axis y axis y axis y axis y axis 2 2 2 2 2 2 2 2 2 4 4 4 4 4 4 4 4 4 6 6 6 6 6 6 6 6 6 200 200 200 200 200 200 200 200 200 400 400 400 400 400 400 400 400 400 600 600 600 600 600 600 600 600 600

slide-83
SLIDE 83

The Badlands: Arcsine The Badlands: Arcsine The Badlands: Arcsine

> (plot (function (λ (x) (define z* (bigfloat->real (bfasin (bf x)))) (flulp-error (flasin (fl x)) z*))

  • 1 1))

x axis x axis x axis x axis x axis x axis x axis x axis x axis y axis y axis y axis y axis y axis y axis y axis y axis y axis

  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • 1
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5
  • .5

.5 .5 .5 .5 .5 .5 .5 .5 .5 1 1 1 1 1 1 1 1 1 .25 .25 .25 .25 .25 .25 .25 .25 .25 .5 .5 .5 .5 .5 .5 .5 .5 .5 .75 .75 .75 .75 .75 .75 .75 .75 .75 1 1 1 1 1 1 1 1 1 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25

slide-84
SLIDE 84

The Badlands: Arccosine The Badlands: Arccosine The Badlands: Arccosine

> (plot (function (λ (x) (define z* (bigfloat->real (bfacos (bf x)))) (flulp-error (flacos (fl x)) z*)) 0.8 1))

x axis x axis x axis x axis x axis x axis x axis x axis x axis y axis y axis y axis y axis y axis y axis y axis y axis y axis .85 .85 .85 .85 .85 .85 .85 .85 .85 .9 .9 .9 .9 .9 .9 .9 .9 .9 .95 .95 .95 .95 .95 .95 .95 .95 .95 1 1 1 1 1 1 1 1 1 50 50 50 50 50 50 50 50 50 100 100 100 100 100 100 100 100 100 150 150 150 150 150 150 150 150 150 200 200 200 200 200 200 200 200 200

slide-85
SLIDE 85

The Badlands: Arctangent The Badlands: Arctangent The Badlands: Arctangent

> (plot (function (λ (x) (define z* (bigfloat->real (bfatan (bf x)))) (flulp-error (flatan (fl x)) z*))

  • 10 10))

x axis x axis x axis x axis x axis x axis x axis x axis x axis y axis y axis y axis y axis y axis y axis y axis y axis y axis

  • 10
  • 10
  • 10
  • 10
  • 10
  • 10
  • 10
  • 10
  • 10
  • 5
  • 5
  • 5
  • 5
  • 5
  • 5
  • 5
  • 5
  • 5

5 5 5 5 5 5 5 5 5 10 10 10 10 10 10 10 10 10 .2 .2 .2 .2 .2 .2 .2 .2 .2 .4 .4 .4 .4 .4 .4 .4 .4 .4 .6 .6 .6 .6 .6 .6 .6 .6 .6

slide-86
SLIDE 86

The Badlands: Exponential The Badlands: Exponential The Badlands: Exponential

> (plot (function (λ (x) (define z* (bigfloat->real (bfexp (bf x)))) (flulp-error (flexp (fl x)) z*))

  • 100 100))

x axis x axis x axis x axis x axis x axis x axis x axis x axis y axis y axis y axis y axis y axis y axis y axis y axis y axis

  • 100
  • 100
  • 100
  • 100
  • 100
  • 100
  • 100
  • 100
  • 100
  • 50
  • 50
  • 50
  • 50
  • 50
  • 50
  • 50
  • 50
  • 50

50 50 50 50 50 50 50 50 50 100 100 100 100 100 100 100 100 100 10 10 10 10 10 10 10 10 10 20 20 20 20 20 20 20 20 20 30 30 30 30 30 30 30 30 30 40 40 40 40 40 40 40 40 40 50 50 50 50 50 50 50 50 50

slide-87
SLIDE 87

The Badlands: Exponential With Base The Badlands: Exponential With Base The Badlands: Exponential With Base

> (plot3d (contour-intervals3d (λ (x y) (define z* (bigfloat->real (bfexpt (bf x) (bf y)))) (flulp-error (flexpt (fl x) (fl y)) z*)) 0 101 -101 101))

50 50 50 50 50 50 50 50 50 100 100 100 100 100 100 100 100 100 150 150 150 150 150 150 150 150 150 200 200 200 200 200 200 200 200 200 250 250 250 250 250 250 250 250 250 100 100 100 100 100 100 100 100 100 75 75 75 75 75 75 75 75 75 50 50 50 50 50 50 50 50 50 25 25 25 25 25 25 25 25 25 100 100 100 100 100 100 100 100 100 50 50 50 50 50 50 50 50 50

  • 50
  • 50
  • 50
  • 50
  • 50
  • 50
  • 50
  • 50
  • 50
  • 100
  • 100
  • 100
  • 100
  • 100
  • 100
  • 100
  • 100
  • 100

x axis x axis x axis x axis x axis x axis x axis x axis x axis y a x i s y a x i s y a x i s y a x i s y a x i s y a x i s y a x i s y a x i s y a x i s

slide-88
SLIDE 88

Condition Number Condition Number Condition Number

(: condition ((Flonum -> Flonum) (Flonum -> Flonum)

  • > (Flonum -> Flonum)))

(define ((condition f df) x) (abs (/ (* x (df x)) (f x)))) (: condition2d ((Flonum Flonum -> Flonum) (Flonum Flonum -> (Values Flonum Flonum))

  • > (Flonum Flonum -> Flonum)))

(define ((condition2d f df) x y) (define-values (dx dy) (df x y)) (define z (f x y)) (max (abs (/ (* x dx) z)) (abs (/ (* y dy) z))))