7.4 Floating-point numbers
Syntax:enabled-floating-point-traps
enabled-floating-point-traps
returns a list of the currently enabled conditions that cause interrupt traps on floating-point operations.
setf
to change the list of enabled conditions.
supported-floating-point-conditions
,with-floating-point-traps
Syntax:extreme-float-p
float
extreme-float-p
identifies extreme floating-point numbers. It returns one of the following values:nil
:minus-infinity
:plus-infinity
:not-a-number
Syntax:integer-decode-float
float
integer-decode-float
returns three values:(expt 2 52)
and(expt 2 53)
.integer-decode-float
multiplied by the radix raised to the power of the second result and then multiplied by the third result is exactly equal to the value of the argument.
> (float-precision 0.5) 53round-to-single-precision Function> (integer-decode-float 0.5) 8388608 -24 1
> (= * (expt 2 23)) T
> (let ((*print-base* 16)) (print (integer-decode-float 0.5)) (print (integer-decode-float 0.6)) (print (integer-decode-float (round-to-single-precision 0.6))) nil) 800000 13333333333333 99999A NIL
Syntax:round-to-single-precision
float
round-to-single-precision
rounds a double-precision number (64 bits) down to single-precision format (32 bits); it then reexpands the number to 64-bit size.
> pi 3.141592653589793supported-floating-point-conditions Constant> (float-precision pi) 53
> (round-to-single-precision pi) 3.1415927410125732
> (float-precision (round-to-single-precision pi)) 53
Syntax:supported-floating-point-conditions
supported-floating-point-conditions
is a list of the floating-point trapping conditions. These conditions conform to the IEEE standard for detecting floating-point exceptions.
floating-point-underflow
,floating-point-overflow
floating-point-inexact
floating-point-invalid-operation
floating-point-invalid-operation
represents the union of three lower-level conditions supported by the MC68881 floating-point coprocessor; see the on-line filewizards.doc
for more information.
division-by-zero
enabled-floating-point-traps
,with-floating-point-traps
Syntax:with-floating-point-traps
(enable-list disable-list) {form}*
with-floating-point-traps
enables or disables floating-point traps during the execution of the macro body.
> (with-floating-point-traps ('(floating-point-invalid-condition) supported-floating-point-conditions) ;; Find out how many bits of significance the denormalized ;; format really supports; turn off underflow and loss of ;; of precision traps. (loop for i from 1 when (zerop (/ least-positive-normalized-single-float (scale-float 1.0 i))) return (1- i))) 52> (with-floating-point-traps ('(floating-point-inexact) ()) (round-to-single-precision pi))
>>Error: A condition of type FLOATING-POINT-INEXACT occurred. EVAL: Required arg 0 (EXPRESSION): (WITH-FLOATING-POINT-TRAPS ((QUOTE #) NIL) (ROUND-TO-SINGLE-PRECISION PI)) :C 0: Use inexact result :A 1: Abort to Lisp Top Level -> :c Use inexact result 3.1415927
;; In the production mode of the compiler, ;; you can get unsafe code. > (compile (defun fast-asin (x) (declare (type (float (0.0) (1.0)) x) (optimize (speed 3) (safety 0))) (asin x))) ;;; Compiling function FAST-ASIN FAST-ASIN
> (with-floating-point-traps (() '(floating-point-OutOfDomain)) (fast-asin 100.0)) #<FLOAT :NOT-A-NUMBER>
> (fast-asin t) ;safety=0 fails to check floatp 5.386404458241357E-169
> ** #<FLOAT :NOT-A-NUMBER>
> (fast-asin *) #<FLOAT :NOT-A-NUMBER>
> (with-floating-point-traps ('(floating-point-NaN) '(floating-point-OutOfDomain)) (fast-asin (fast-asin 100.0)))
#<FLOAT :NOT-A-NUMBER>
enabled-floating-point-traps
,supported-floating-point-conditions
Generated with Harlequin WebMaker