| Paste number 76402: | Equal significant figures |
| Pasted by: | tmh |
| When: | 1 year, 6 months ago |
| Share: | Tweet this! | http://paste.lisp.org/+1MYA |
| Channel: | #lisp |
| Paste contents: |
;;; Equal at least to significant figures
;;;
;;; Would like to test whether 2 floating point numbers are equal in
;;; terms of significant figures.
;;; Step 1. Calculate the floating point error (delta) between the
;;; numbers
(defun err (exact approximate)
"Return the floating point error."
(abs (- 1.0 (/ approximate exact))))
;;; Step 2. Verify that error is less than some epsilon.
(defun float-equal (float1 float2 epsilon)
"Return true if the floating point error is less than some epsilon."
(< (err exact approximate) epsilon))
;;; Primary problem : The error is, in general, proportional to a
;;; constant with the exponent equal to the negative of the desired
;;; significant figures.
;;; 2 significant figures
SCRATCH> (err 1.1 1.04)
0.05454552
SCRATCH> (err 1.1 1.16)
0.054545403
SCRATCH> (err 11.0 10.4)
0.054545462
SCRATCH> (err 11.0 11.6)
0.05454552
;;; 3 significant figures
SCRATCH> (err 1.01 1.004)
0.005940616
SCRATCH> (err 1.01 1.016)
0.0059406757
SCRATCH> (err 10.1 10.04)
0.005940616
SCRATCH> (err 10.1 10.16)
0.0059405565
;;; Unfortunately, the constant is not constant.
;;; 2 significant figures
SCRATCH> (err 2.2 2.14)
0.027272701
SCRATCH> (err 2.2 2.26)
0.027272701
;;; 3 significant figures
SCRATCH> (err 2.02 2.014)
0.0029703379
SCRATCH> (err 2.02 2.026)
0.0029703379
;;; The acceptable error seems to vary from
;;; 0.25E-SIG < DELTA < 0.50e-SIG.
;;; Secondary problem : Using standard rounding rules, a 5 in the
;;; first digit after the last significant figure should cause the
;;; last significant figure to round up. Unfortunately, the floating
;;; point error with a 5 in the first digit after the last significant
;;; digit does not exceed the error.
SCRATCH> (err 1.01 1.015)
0.0049505234 ; Should be > 0.005
SCRATCH> (err 1.01 1.016)
0.0059406757
This paste has no annotations.