Steel Bank Common Lisp

decode-float assumed not to error

Reported by John Fremlin on 2009-08-12
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
SBCL
Undecided
Unassigned

Bug Description

I acquired many #<DOUBLE-FLOAT quiet NaN> via CFFI.

> *f*
#<DOUBLE-FLOAT quiet NaN>

for some reason this simple check to see if a float is a NaN, i.e. if decode-float causes an error, is optimized away.

(defun safe-float-print (float)
  (princ (ignore-errors (decode-float float)))
  (princ float)
  (if (ignore-errors (decode-float float))
    (format t "GOOD")
    (format t "BAD")))

> (safe-float-print *f*)
NIL#<DOUBLE-FLOAT quiet NaN>GOOD

So it somehow assumes the ignore-errors will return true, even though it definitely doesn't.

> (lisp-implementation-version)
"1.0.29.11.debian"

Ubuntu AMD64

Changed in sbcl:
status: New → In Progress
Nikodemus Siivola (nikodemus) wrote :

Fixed in 1.0.30.44, assuming you (DECLARE (OPTIMIZE SAFETY)): error signaling for DECODE-FLOAT is guaranteed only for safe code.

For portable NaN testing I would suggest something like

(defun nan-p (x)
  (declare (float x))
  (/= x x))

instead -- or using implementation specifics like SB-EXT:FLOAT-NAN-P.

Changed in sbcl:
status: In Progress → Fix Committed

Thanks for your quick response.

Nikodemus Siivola <email address hidden> writes:

> Fixed in 1.0.30.44, assuming you (DECLARE (OPTIMIZE SAFETY)): error
> signaling for DECODE-FLOAT is guaranteed only for safe code.
>
> For portable NaN testing I would suggest something like
> (defun nan-p (x)
> (declare (float x))
> (/= x x))

This doesn't work on Allegro 8.1 for example.

> (let ((x EXCL:*NAN-DOUBLE*)) (/= x x))
NIL

> instead -- or using implementation specifics like SB-EXT:FLOAT-NAN-P.

If you move the ignore-errors form to its own function then it works
fine on SBCL before the fix.

(defun printable-float (float)
  (declare (optimize safety))
  (ignore-errors (decode-float float)))

Nikodemus Siivola (nikodemus) wrote :

FWIW I would argue that that is an Allegro bug -- assuming :IEEE-FLOATING-POINT is in *FEATURES* -- since

 (let ((x <nan>)) (= x x))

should never be true under IEEE FP arithmetic.

Changed in sbcl:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers