r8583: add naagum's epsilon function
authorKevin M. Rosenberg <kevin@rosenberg.net>
Sun, 1 Feb 2004 06:43:21 +0000 (06:43 +0000)
committerKevin M. Rosenberg <kevin@rosenberg.net>
Sun, 1 Feb 2004 06:43:21 +0000 (06:43 +0000)
math.lisp
package.lisp

index e9118443fdec24c716cf21898b42615ce5973692..fcbab3c4baa2e922b0eba049af0bd04079a19739 100644 (file)
--- a/math.lisp
+++ b/math.lisp
 
 (defun fixnum-width ()
   (nth-value 0 (truncate (+ (/ (log (1+ most-positive-fixnum)) (log 2)) .5))))
+
+(defun scaled-epsilon (float &optional (operation '+))
+  "Return the smallest number that would return a value different from
+  FLOAT if OPERATION were applied to FLOAT and this number.  OPERATION
+  should be either + or -, and defauls to +."
+  (multiple-value-bind (significand exponent)
+      (decode-float float)
+    (multiple-value-bind (1.0-significand 1.0-exponent)
+       (decode-float (float 1.0 float))
+      (if (and (eq operation '-)
+              (= significand 1.0-significand))
+         (scale-float (typecase float
+                        (short-float short-float-negative-epsilon)
+                        (single-float single-float-negative-epsilon)
+                        (double-float double-float-negative-epsilon)
+                        (long-float long-float-negative-epsilon))
+                      (- exponent 1.0-exponent))
+       (scale-float (typecase float
+                      (short-float short-float-epsilon)
+                      (single-float single-float-epsilon)
+                      (double-float double-float-epsilon)
+                      (long-float long-float-epsilon))
+                    (- exponent 1.0-exponent))))))
index 1ba962f275b36cfafb25a75b0d0705a4a34b45c4..62bf63791a82721db851bae6df08948c3dabbd7a 100644 (file)
    #:ensure-integer
    #:histogram
    #:fixnum-width
+   #:scaled-epsilon
    
    ;; macros.lisp
    #:time-iterations