[Sbcl-help] Processing NaN

52 views
Skip to first unread message

Steven Nunez via Sbcl-help

unread,
Jun 17, 2021, 2:45:38 AM6/17/21
to SBCL Help
Hi All,

I'm doing a bit of numeric programming, and using float-features for the IEEE-754 stuff. Recently I encountered an error when using this cond statement:


   
  (declare (double-float x))
  (let (i (q (abs x)) z)
    (cond ((= x double-float-positive-infinity) double-float-positive-infinity)
          ((> x maxgamd) double-float-positive-infinity)
          ((and (minusp x) (= 0 (rem x 1))) double-float-nan)



The last line causes trouble for the SBCL compiler (2.0.10, MS Windows). Ignore the actual condition, that's to detect a negative integer when I've declared x as a double.

arithmetic error FLOATING-POINT-INVALID-OPERATION signalled
Operation was (> #<DOUBLE-FLOAT quiet NaN> #<DOUBLE-FLOAT quiet NaN>).
   [Condition of type FLOATING-POINT-INVALID-OPERATION]

It almost looks like the compiler is trying to do a comparison. double-float-nan is defined as a constant and I've confirmed it's properly loaded.

Any ideas?

Christophe Rhodes

unread,
Jun 17, 2021, 7:29:39 AM6/17/21
to Steven Nunez via Sbcl-help
Steven Nunez via Sbcl-help <sbcl...@lists.sourceforge.net> writes:

> arithmetic error FLOATING-POINT-INVALID-OPERATION signalled
> Operation was (> #<DOUBLE-FLOAT quiet NaN> #<DOUBLE-FLOAT quiet NaN>).
> [Condition of type FLOATING-POINT-INVALID-OPERATION]
> It almost looks like the compiler is trying to do a comparison. double-float-nan is defined as a constant and I've confirmed it's properly loaded.
> Any ideas?

The compiler is probably trying to do a comparison, attempting to do
type derivation or constant-folding or something similar. This is a
bug, but fixing it might involve auditing the current type derivers to
make sure that they are NaN-aware (and so might be a more substantial
piece of work than can be done quickly).

Some possible workarounds:

* around compiling, turn floating-point traps off:
(let ((modes (sb-int:get-floating-point-modes)))
(unwind-protect
(progn (set-floating-point-modes :traps nil) (compile-your-things))
(apply 'sb-int:set-floating-point-modes modes)))

* disguise the NaN constant from the compiler:
use `(load-time-value double-float-nan)` in place of the bare `double-float-nan`

Best wishes,

Christophe


_______________________________________________
Sbcl-help mailing list
Sbcl...@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sbcl-help

Marco Antoniotti

unread,
Jun 17, 2021, 7:36:54 AM6/17/21
to Steven Nunez, SBCL Help
Hi Steven

The last line is wrong in SBCL (and float-features)... depending on the degree of compliance with the "Language Independent Arithmetic" standards.

The standard mandates the signaling of a condition only for signaling NaNs.  For quiet NaNs the result should be, to the bes of mz knowledge, NIL.

Cheers

MA

_______________________________________________
Sbcl-help mailing list
Sbcl...@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sbcl-help


--
Marco Antoniotti, Associate Professor         tel. +39 - 02 64 48 79 01
DISCo, Università Milano Bicocca U14 2043 http://dcb.disco.unimib.it
Viale Sarca 336                                                       http://cdac2021.lakecomoschool.org
I-20126 Milan (MI) ITALY
Reply all
Reply to author
Forward
0 new messages