Lisp HUG Maillist Archive

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
web: http://www.refined-audiometrics.com
Skype: dbmcclain


Re: Floating point Infinities and NaN's?


David McClain <dbm@refined-audiometrics.com> writes:
> I wonder if any of you know the preferred manner of testing for
> floating point infinities or NaN's in Common Lisp?

What I generally do is either use eql checking or use /= (as you have
done in another mail):

  (defun nan-1? (x)
    (eql x 1D+-0))

  (defun nan-2? (x)
    (/= x x))

  (nan-1? 7)     => NIL
  (nan-1? 1d+-0) => T
  (nan-2? 7)     => NIL
  (nan-2? 1d+-0) => T

The nan-1? test assumes you know the data types you're using.

Cheers,
Chris Dean


Re: Floating point Infinities and NaN's?

David,

I've only been following your discussion from afar but you might want to look at the GBBopen source as I'm pretty sure that there are portable versions of at least some of what you're looking for. (see http://www.gbbopen.org/)

regards,

On Aug 5, 2007, at 1:55 PM, David McClain wrote:

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
web: http://www.refined-audiometrics.com
Skype: dbmcclain



--
Gary Warren King, metabang.com 
Cell: (413) 885 9127
Fax: (206) 338-4052
gwkkwg on Skype * garethsan on AIM




Updated at: 2020-12-10 08:45 UTC