From 6d627a3fd923a0265f0f409821d60808bbfa63d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Kochma=C5=84ski?= Date: Fri, 23 Dec 2016 08:55:27 +0100 Subject: [PATCH] tests: add approx= function --- src/tests/ecl-tests.lisp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/tests/ecl-tests.lisp b/src/tests/ecl-tests.lisp index 8082755f8..03957aecf 100644 --- a/src/tests/ecl-tests.lisp +++ b/src/tests/ecl-tests.lisp @@ -128,3 +128,17 @@ as a second value." (format ,stream ,string ,@args)) (multiple-value-prog1 (progn ,@body) (delete-file ,var))))) + + +;;; Approximate equality function +(defun approx= (x y &optional (eps (epsilon x))) + (<= (abs (/ (- x y) (max (abs x) 1))) eps)) + +(defun epsilon (number) + (etypecase number + (complex (* 2 (epsilon (realpart number)))) ;; crude + (short-float short-float-epsilon) + (single-float single-float-epsilon) + (double-float double-float-epsilon) + (long-float long-float-epsilon) + (rational 0)))