I can't seem to find any Common Lisp functions to do the following:
nextfloat(x) - returns the closest float in the system > x
prevfloat(x) - returns the closest float in the system < x
Can anyone suggest an accurate way to write these?
Thinking along the lines of:
(defun nextfloat (x)
(let ((x (float x 1d0)))
(multiple-value-bind (significand exponent sign)
(decode-float x)
(* sign (* (+ significand double-float-epsilon)
(expt (float-radix x) exponent))))))
But, is this approach accurate?
----------------------
Neil Pengelly
········@cyc.com
From: Raymond Toy
Subject: Re: looking for next and previous floating point functions.
Date:
Message-ID: <4nhg1rbqhe.fsf@rtp.ericsson.se>
Neil Pengelly <········@selma.cyc.com> writes:
> I can't seem to find any Common Lisp functions to do the following:
>
> nextfloat(x) - returns the closest float in the system > x
> prevfloat(x) - returns the closest float in the system < x
>
> Can anyone suggest an accurate way to write these?
>
> Thinking along the lines of:
>
> (defun nextfloat (x)
> (let ((x (float x 1d0)))
> (multiple-value-bind (significand exponent sign)
> (decode-float x)
> (* sign (* (+ significand double-float-epsilon)
> (expt (float-radix x) exponent))))))
>
> But, is this approach accurate?
Try integer-decode-float instead of decode-float, with suitable mods
to the above. Something like
(defun nextfloat (x)
(multiple-value-bind (significand exponent sign)
(integer-decode-float x)
(scale-float (* sign
(float (+ significand 1) x))
exponent)))
You may need to check for overflow here, if x is the FP number just
before "infinity".
Ray