chickadee » math » fpulp-error

fpulp-error x rprocedure
x
flonum
r
number

Returns the absolute number of ulps difference between x and r.

For non-rational arguments such as +nan.0, fpulp-error returns 0.0 if (eqv? x r); otherwise it returns +inf.0.

A flonum function with maximum error 0.5 ulps exhibits only rounding error; it is correct. A flonum function with maximum error no greater than a few ulps is accurate. Most moderately complicated flonum functions, when implemented directly, seem to have over a hundred thousand ulps maximum error.

Examples:

> (fpulp-error 0.5 1/2)
0.0
> (fpulp-error 0.14285714285714285 1/7)
0.2857142857142857
> (fpulp-error +inf.0 +inf.0)
0.0
> (fpulp-error +inf.0 +nan.0)
+inf.0
> (fpulp-error 1e-20 0.0)
+inf.0
> (fpulp-error (- 1.0 (fl 4999999/5000000)) 1/5000000)
217271.6580864

he last example subtracts two nearby flonums, the second of which had already been rounded, resulting in horrendous error. This is an example of ''catastrophic cancellation'. Avoid subtracting nearby flonums whenever possible. [1]

See relative-error for a similar way to measure approximation error when the approximation is not necessarily represented by a flonum.

[1] You can make an exception if the result is to be exponentiated. If x has small absolute-error, then (exp x) has small relative-error and small fpulp-error