Re: Floating point Infinities and NaN's?
Hi all,I wonder if any of you know the preferred manner of testing for floating point infinities or NaN's in Common Lisp?
So far, what I've come up with is to enshroud the computation with a HANDLER-CASE, to trap the invalid operations, and then for those operations which "succeed" to test for an infinity by looking to see whether its reciprocal is zero...
(in my case, I'm also looking to be sure the result is a real-value, not a complex value)
-----------------------------------
;; test for infinities
(defun infinitep (v)
(and (not (zerop v))
(zerop (/ v))))
;; careful evaluation with tests for infinities and non-real results
(defun real-eval-with-nans (fn &rest args)
(handler-case
(let ((v (apply fn args)))
(if (or (complexp v)
(infinitep v))
:nan
v))
(error (err)
(declare (ignore err))
:nan)))
;; catchall predicate for both NaN's and infinities
(defun nanp (v)
(or (eq v :nan)
(infinitep v)))
-------------------------------------
I find it interesting that Common Lisp will generate infinite results, and consider them as numbers, but does not appear to offer any method for testing for such results, or for classifying numbers as per the IEEE FP Standard.
I do see that NaN's are never actually generated since the operations that would produce them are trapped as invalid, or else produce complex results. So the NaN's are my own invention here, I guess. But infinities are not.
David McClain
Chief Technical Officer
Refined Audiometrics Laboratory
4391 N. Camino Ferreo
Tucson, AZ 85750
email: dbm@refined-audiometrics.com
phone: 1.520.390.3995
Skype: dbmcclain