chickadee » math » relative-error

relative-error x rprocedure
x
number
r
number

Measures how close an approximation x is to the correct value r, relative to the magnitude of r.

This function usually computes (abs (/ (- x r) r)) using exact rationals, but handles non-rational reals such as +inf.0 specially, as well as r = 0.

> (relative-error 1/2 1/2)
0
> (relative-error 0.14285714285714285 1/7)
5.551115123125783e-17
> (relative-error +inf.0 +inf.0)
0.0
> (relative-error +inf.0 +nan.0)
+inf.0
> (relative-error 1e-20 0.0)
+inf.0
> (relative-error (- 1.0 (fp 4999999/5000000)) 1/5000000)
2.8755664516211255e-11

In the last two examples, relative error is high because the result is near zero. (Compare the same examples with absolute-error.) Because flonums are particularly dense near zero, this makes relative error better than absolute error for measuring the error in a flonum approximation. An even better one is error in ulps; see fpulp and fpulp-error.