Debian Codegraph @

src/tests/normal-tests/ieee-fp.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;; Author: Daniel Kochmański
5 ;;;; Created: 2016-08-12
6 ;;;; Contains: IEEE floating point tests
7 ;;;;
8
9 (in-package :cl-test)
10
11 (suite 'ieee-fp)
12
13 (defmacro without-fpe-traps (&body body)
14 `(let ((bits (si:trap-fpe 'cl:last t)))
15 (unwind-protect
16 (progn
17 (si:trap-fpe t nil)
18 ,@body)
19 (si:trap-fpe bits t))))
20
21 (defconstant +number-subtypes-and-values+
22 '((fixnum 3)
23 (bignum #.(- most-negative-fixnum 13))
24 (ratio 3/5)
25 (single-float 1.23f0)
26 (double-float 4.56d0)
27 (long-float 7.89l0)
28 ((complex fixnum) #C(2 3))
29 ((complex integer) #.(complex 2 (+ most-positive-fixnum 3)))
30 ((complex rational) #C(5/7 -4))
31 ((complex single-float) #C(0.98f0 77.7f0))
32 ((complex double-float) #C(0.87d0 88.8d0))
33 ((complex long-float) #C(0.76l0 99.9l0))))
34
35 (defmacro for-all-number-subtypes ((variable &optional (type number) default-value) &body body)
36 (when (not (subtypep type 'number))
37 (error "~a must be a subtype of number" type))
38 `(progn ,@(loop :for (subtype value) :in +number-subtypes-and-values+
39 :if (subtypep subtype type)
40 :collect `(let ((,variable
41 ,(if default-value
42 `(coerce ,default-value ',subtype)
43 value)))
44 ,@body))))
45
46 (defmacro for-all-NaNs (variable &body body)
47 `(loop :for ,variable :in *ieee-fp.nan* :do
48 ,@body))
49
50 (defmacro for-all-infinities (minus-infinity-var plus-infinity-var &body body)
51 `(loop :for ,minus-infinity-var :in (remove-if-not #'minusp *ieee-fp.inf*) :do
52 (loop :for ,plus-infinity-var :in (remove-if-not #'plusp *ieee-fp.inf*) :do
53 ,@body)))
54
55 (defun type-contagion (&rest numbers)
56 (labels ((type-of-proper (number)
57 (etypecase number
58 (single-float 'single-float)
59 (double-float 'double-float)
60 (long-float 'long-float)
61 (ratio 'ratio)
62 (integer 'integer)
63 (complex `(complex ,(type-of-proper (realpart number))))))
64 (type-contagion1 (t1 &optional t2)
65 (cond ((not t2) t1)
66 ((subtypep t1 'float)
67 (cond ((subtypep t2 'float)
68 (cond ((or (eql t1 'long-float)
69 (eql t2 'long-float))
70 'long-float)
71 ((or (eql t1 'double-float)
72 (eql t2 'double-float))
73 'double-float)
74 ((or (eql t1 'single-float)
75 (eql t2 'single-float))
76 'single-float)))
77 ((subtypep t2 'rational)
78 t1)
79 ((subtypep t2 'complex)
80 `(complex ,(type-contagion1 t1 (second t2))))))
81 ((subtypep t1 'ratio)
82 (cond ((subtypep t2 'float) t2)
83 ((subtypep t2 'rational) 'ratio)
84 ((subtypep t2 'complex)
85 `(complex ,(type-contagion1 t1 (second t2))))))
86 ((subtypep t1 'integer)
87 (cond ((or (subtypep t2 'float) (subtypep t2 'ratio)) t2)
88 ((subtypep t2 'integer) 'integer)
89 ((subtypep t2 'complex)
90 `(complex ,(type-contagion1 t1 (second t2))))))
91 ((subtypep t1 'complex)
92 (cond ((or (subtypep t2 'float) (subtypep t2 'rational))
93 `(complex ,(type-contagion (second t1) t2)))
94 ((subtypep t2 'complex)
95 `(complex ,(type-contagion1 (second t1) (second t2)))))))))
96 (reduce #'type-contagion1 (mapcar #'type-of-proper numbers))))
97
98 (defmacro type-and-value-check ((function &rest args) &rest values)
99 "Arguments ((function &rest args) &rest values)
100 Check that function applied to args returns numbers that are
101 mathematically equal to values and have the correct type according to
102 Common Lisp type contagion rules."
103 (if (eql function 'funcall)
104 (setf function (first args)
105 args (rest args))
106 (setf function `(quote ,function)))
107 `(is (every #'eql
108 (multiple-value-list (funcall ,function ,@args))
109 (mapcar #'(lambda (v)
110 (coerce v (type-contagion ,@args)))
111 (list ,@values)))))
112
113 ;;; Constructing complex floats with infinities is difficult, because
114 ;;; any finite real number x plus infinite imaginary number leads to
115 ;;; an complex number with NaN real part due to a very literal
116 ;;; application of calculation rules:
117 ;;; x + I*∞ = x + (0 + I*1)*∞ = x + NaN + I*∞ = NaN + I*∞
118 ;;; Therefore, when comparing the output of numeric functions
119 ;;; returning complex infinities to expected results, we use the
120 ;;; following function
121 (defmacro complex-equality (equality-comparison form realpart imagpart)
122 `(progn
123 (is (funcall ,equality-comparison (realpart ,form) ,realpart))
124 (is (funcall ,equality-comparison (imagpart ,form) ,imagpart))))
125
126
127
128 (test ieee-fp.0001.infinity-eql
129 (without-fpe-traps
130 (let ((sfni ext:single-float-negative-infinity)
131 (sfpi ext:single-float-positive-infinity)
132 (dfni ext:double-float-negative-infinity)
133 (dfpi ext:double-float-positive-infinity))
134 (is (eql sfni (- sfpi)))
135 (is (eql dfni (- dfpi)))
136 (is (not (eql sfni (- dfpi))))
137 (is (= sfni (- dfpi))))))
138
139 (test ieee-fp.0002.printing
140 (let ((nums (list ext:single-float-negative-infinity
141 ext:single-float-positive-infinity
142 ext:double-float-negative-infinity
143 ext:double-float-positive-infinity
144 (si:nan)))
145 (*standard-output* (make-string-output-stream)))
146 (dolist (i nums)
147 (finishes
148 (let ((*print-readably* t)
149 (*read-eval* t))
150 (print nums)))
151 (finishes
152 (let ((*print-readably* nil)
153 (*read-eval* nil))
154 (print nums)))
155 (signals print-not-readable
156 (let ((*print-readably* t)
157 (*read-eval* nil))
158 (print nums))))))
159
160 (test ieee-fp.0003.predicates
161 (is (ext:float-infinity-p ext:single-float-negative-infinity))
162 (is (ext:float-infinity-p ext:single-float-positive-infinity))
163 (is (ext:float-nan-p (si:nan))))
164
165 (test ieee-fp.0004.nan-comparison
166 (for-all-number-subtypes (x real)
167 (for-all-NaNs NaN
168 (is (not (< NaN x)))
169 (is (not (< x NaN)))
170 (is (not (<= NaN x)))
171 (is (not (<= x NaN)))
172 (is (not (> NaN x)))
173 (is (not (> x NaN)))
174 (is (not (>= NaN x)))
175 (is (not (>= x NaN))))))
176
177 (test ieee-fp.0005.infinity-comparison
178 (for-all-number-subtypes (x real)
179 (for-all-infinities -infinity +infinity
180 (progn (is (< -infinity x))
181 (is (not (< x -infinity)))
182 (is (< x +infinity))
183 (is (not (< +infinity x))))
184 (progn (is (<= -infinity x))
185 (is (not (<= x -infinity)))
186 (is (<= x +infinity))
187 (is (not (<= +infinity x))))
188 (progn (is (not (> -infinity x)))
189 (is (> x -infinity))
190 (is (not (> x +infinity)))
191 (is (> +infinity x)))
192 (progn (is (not (>= -infinity x)))
193 (is (>= x -infinity))
194 (is (not (>= x +infinity)))
195 (is (>= +infinity x))))))
196
197
198
199 ;;; We treat NaNs as missing data (like fmin/fmax C functions)
200 (test ieee-fp.0006.NaN-min/max
201 (for-all-NaNs NaN
202 (is (eql (min NaN) NaN))
203 (is (eql (max NaN) NaN))
204 (is (eql (min NaN NaN) NaN))
205 (is (eql (max NaN NaN) NaN))
206 (for-all-number-subtypes (x real)
207 (let* ((y (* 2 x))
208 (min (min x y))
209 (max (max x y)))
210 (is (= (min x NaN) x))
211 (is (= (max x NaN) x))
212 (is (= (min x y NaN) min))
213 (is (= (max x y NaN) max))))))
214
215 (test ieee-fp.0007.infinity-min/max
216 (for-all-number-subtypes (x real)
217 (let* ((y (* 2 x))
218 (max (max x y))
219 (min (min x y)))
220 (for-all-infinities -infinity +infinity
221 (is (= (min -infinity x y) -infinity))
222 (is (= (min +infinity x y) min))
223 (is (= (max -infinity x y) max))
224 (is (= (max +infinity x y) +infinity))))))
225
226 (test ieee-fp.0008.NaN-minusp/zerop/plusp
227 (without-fpe-traps
228 (for-all-NaNs NaN
229 (is (not (minusp NaN)))
230 (is (not (zerop NaN)))
231 (is (not (plusp NaN))))))
232
233 (test ieee-fp.0009.infinity-minusp/zerop/plusp
234 (for-all-infinities -infinity +infinity
235 (is (minusp -infinity))
236 (is (not (minusp +infinity)))
237 (is (not (zerop -infinity)))
238 (is (not (zerop +infinity)))
239 (is (not (plusp -infinity)))
240 (is (plusp +infinity))))
241
242
243
244 ;;; If we supported NaN/infinity for rounding functions operating
245 ;;; on floats, sensible behaviour would be as follows ...
246
247 #|
248 (test ieee-fp.0010.NaN-ffloor/fceiling/ftruncate/fround
249 (without-fpe-traps
250 (loop :for function :in '(ffloor fceiling ftruncate fround)
251 :do
252 (for-all-number-subtypes (x real)
253 (for-all-NaNs NaN
254 (type-and-value-check (funcall function NaN) NaN NaN)
255 (type-and-value-check (funcall function x NaN) NaN NaN)
256 (type-and-value-check (funcall function NaN x) NaN NaN)
257 (type-and-value-check (funcall function NaN NaN) NaN NaN)))
258 (for-all-number-subtypes (x float 0)
259 (type-and-value-check (funcall function x x) (ext:nan) (ext:nan))))))
260
261 (test ieee-fp.0011.infinity-ffloor/fceiling/ftruncate/fround
262 (without-fpe-traps
263 (loop :for function :in '(ffloor fceiling ftruncate fround)
264 :do
265 (for-all-infinities -infinity +infinity
266 ;; +-infinity and 1
267 (type-and-value-check (funcall function -infinity)
268 -infinity (ext:nan))
269 (type-and-value-check (funcall function +infinity)
270 +infinity (ext:nan))
271 (for-all-number-subtypes (x real)
272 ;; +-infinity and finite real
273 (type-and-value-check (funcall function -infinity (abs x))
274 -infinity (ext:nan))
275 (type-and-value-check (funcall function -infinity (- (abs x)))
276 +infinity (ext:nan))
277 (type-and-value-check (funcall function +infinity (abs x))
278 +infinity (ext:nan))
279 (type-and-value-check (funcall function +infinity (- (abs x)))
280 -infinity (ext:nan))
281 ;; finite real and +-infinity
282 (type-and-value-check (funcall function (abs x) -infinity)
283 (case function
284 (ffloor -1)
285 (t 0))
286 (case function
287 (ffloor -infinity)
288 (t x)))
289 (type-and-value-check (funcall function (- (abs x)) -infinity)
290 (case function
291 (fceiling 1)
292 (t 0))
293 (case function
294 (fceiling +infinity)
295 (t x)))
296 (type-and-value-check (funcall function (abs x) +infinity)
297 (case function
298 (fceiling 1)
299 (t 0))
300 (case function
301 (fceiling -infinity)
302 (t x)))
303 (type-and-value-check (funcall function (- (abs x)) +infinity)
304 (case function
305 (ffloor -1)
306 (t 0))
307 (case function
308 (ffloor +infinity)
309 (t x))))
310 ;; +-infinity and +-infinity
311 (type-and-value-check (funcall function -infinity -infinity)
312 (ext:nan) (ext:nan))
313 (type-and-value-check (funcall function -infinity +infinity)
314 (ext:nan) (ext:nan))
315 (type-and-value-check (funcall function +infinity -infinity)
316 (ext:nan) (ext:nan))
317 (type-and-value-check (funcall function +infinity +infinity)
318 (ext:nan) (ext:nan))
319 (for-all-number-subtypes (x float 0)
320 ;; finite real and 0
321 (for-all-number-subtypes (y real)
322 (type-and-value-check (funcall function (abs y) x)
323 +infinity (ext:nan))
324 (type-and-value-check (funcall function (- (abs y)) x)
325 -infinity (ext:nan)))
326 ;; +-infinity and 0
327 (type-and-value-check (funcall function -infinity 0)
328 -infinity (ext:nan))
329 (type-and-value-check (funcall function +infinity 0)
330 +infinity (ext:nan)))))))
331 |#
332
333 ;;; ... but we don't, therefore everything throws arithmetic errors.
334
335 #+floating-point-exceptions
336 (test ieee-fp.0010.NaN-floor/ceiling/truncate/round/mod/rem
337 (loop :for function :in '(floor ceiling truncate round
338 ffloor fceiling ftruncate fround
339 mod rem)
340 :do
341 (for-all-NaNs NaN
342 (unless (member function '(mod rem))
343 (signals arithmetic-error (funcall function NaN)))
344 (signals arithmetic-error (funcall function NaN NaN))
345 (for-all-number-subtypes (x real)
346 (signals arithmetic-error (funcall function x NaN))
347 (signals arithmetic-error (funcall function NaN x))))
348 (for-all-number-subtypes (x float 0)
349 (signals arithmetic-error (funcall function x x)))))
350
351 #+floating-point-exceptions
352 (test ieee-fp.0011.infinity-floor/ceiling/truncate/round
353 (loop :for function :in '(floor ceiling truncate round
354 ffloor fceiling ftruncate fround
355 mod rem)
356 :do
357 (for-all-infinities -infinity +infinity
358 ;; +-infinity and 1
359 (unless (member function '(mod rem))
360 (signals arithmetic-error (funcall function -infinity))
361 (signals arithmetic-error (funcall function +infinity)))
362 (for-all-number-subtypes (x real)
363 ;; +-infinity and finite real
364 (signals arithmetic-error (funcall function -infinity x))
365 (signals arithmetic-error (funcall function +infinity x))
366 ;; finite real and +-infinity
367 (signals arithmetic-error (funcall function x -infinity))
368 (signals arithmetic-error (funcall function x +infinity)))
369 ;; +-infinity and +-infinity
370 (signals arithmetic-error (funcall function -infinity -infinity))
371 (signals arithmetic-error (funcall function -infinity +infinity))
372 (signals arithmetic-error (funcall function +infinity -infinity))
373 (signals arithmetic-error (funcall function +infinity +infinity))
374 (for-all-number-subtypes (x float 0)
375 ;; finite real and 0
376 (for-all-number-subtypes (y real)
377 (signals arithmetic-error (funcall function y x)))
378 ;; +-infinity and 0
379 (signals arithmetic-error (funcall function -infinity x))
380 (signals arithmetic-error (funcall function +infinity x))))))
381
382
383
384 ;;; All numeric functions should return a NaN if given one
385 (test ieee-fp.0012.sticky-NaN
386 (without-fpe-traps
387 (for-all-NaNs NaN
388 (macrolet ((f-NaN (&rest functions)
389 `(progn ,@(loop :for f :in functions :collect
390 `(is (eql (,f NaN) NaN))))))
391 (f-NaN sin cos tan
392 asin acos atan
393 sinh cosh tanh
394 asinh acosh atanh
395 * + - / 1+ 1-
396 abs signum exp log sqrt
397 conjugate phase))
398 (for-all-number-subtypes (x real)
399 (macrolet ((f-NaN (&rest functions)
400 `(progn ,@(loop :for f :in functions :append
401 `((type-and-value-check (,f x NaN) NaN)
402 (type-and-value-check (,f NaN x) NaN)
403 (type-and-value-check (,f NaN NaN) NaN))))))
404 (f-NaN atan * + - /))
405 (macrolet ((f-NaN (&rest functions)
406 `(progn ,@(loop :for f :in functions :append
407 `((type-and-value-check (,f (abs x) NaN) NaN)
408 (type-and-value-check (,f NaN (abs x)) NaN)
409 (type-and-value-check (,f NaN NaN) NaN))))))
410 (f-NaN expt log)))
411 (is (eql (cis NaN)
412 (complex (coerce (ext:nan) (type-of NaN))
413 (coerce (ext:nan) (type-of NaN))))))))
414
415
416
417 (test ieee-fp.0013.infinity-sin/cos/tan
418 (without-fpe-traps
419 (for-all-infinities -infinity +infinity
420 (type-and-value-check (sin -infinity) (ext:nan))
421 (type-and-value-check (sin +infinity) (ext:nan))
422 #|(type-and-value-check (sin (complex 0 -infinity)) (complex 0 -infinity))
423 (type-and-value-check (sin (complex 0 +infinity)) (complex 0 +infinity))|#
424 (type-and-value-check (cos -infinity) (ext:nan))
425 (type-and-value-check (cos +infinity) (ext:nan))
426 #|(type-and-value-check (cos (complex 0 -infinity)) +infinity)
427 (type-and-value-check (cos (complex 0 +infinity)) +infinity)|#
428 (type-and-value-check (tan -infinity) (ext:nan))
429 (type-and-value-check (tan +infinity) (ext:nan))
430 #|(type-and-value-check (tan (complex 0 -infinity)) (complex 0 -1))
431 (type-and-value-check (tan (complex 0 +infinity)) (complex 0 +1))|#)))
432
433 #|(test ieee-fp.0014.infinity-asin/acos/atan
434 (for-all-infinities -infinity +infinity
435 (complex-equality #'approx= (asin -infinity) (- (/ pi 2)) +infinity)
436 (complex-equality #'approx= (asin +infinity) (/ pi 2) +infinity)
437 (complex-equality #'approx= (acos -infinity) pi -infinity)
438 (complex-equality #'approx= (acos +infinity) 0 -infinity)
439 (is (approx= (atan -infinity) (- (/ pi 2))))
440 (is (approx= (atan +infinity) (/ pi 2)))))|#
441
442
443 ;;; Reported by: Raymond Toy
444 ;;; Test corner-cases of calculating atan2
445 ;;; URL: https://gitlab.com/embeddable-common-lisp/ecl/issues/332
446
447 (flet((+pi-p (elt) (= (coerce pi (type-of elt)) elt))
448 (-pi-p (elt) (= (coerce pi (type-of elt)) (- elt)))
449 (+pi/2-p (elt) (approx= (coerce (/ pi +2) (type-of elt)) elt))
450 (-pi/2-p (elt) (approx= (coerce (/ pi -2) (type-of elt)) elt))
451 (+zerop (elt) (and (zerop elt) (plusp (float-sign elt))))
452 (-zerop (elt) (and (zerop elt) (minusp (float-sign elt)))))
453
454 (test ieee-fp.0015.atan2-special-case.zero-arg
455 (+zerop (atan +0.0 +0.0))
456 (-zerop (atan -0.0 +0.0))
457 (+pi-p (atan +0.0 -0.0))
458 (-pi-p (atan -0.0 -0.0))
459 (without-fpe-traps
460 (map () (lambda (n)
461 ;; (atan +-0 +(anything-but/nan)) -> +-0
462 (is (+zerop (atan +0.0 n)))
463 (is (-zerop (atan -0.0 n)))
464 ;; (atan +-0 -(anything-but/nan)) -> +-pi
465 (is (+pi-p (atan +0.0 (- n))))
466 (is (-pi-p (atan -0.0 (- n))))
467 ;; (atan +-(anything-but/0+nan) 0) -> +-pi/2
468 (is (+pi/2-p (atan n +0.0)))
469 (is (+pi/2-p (atan n -0.0)))
470 (is (-pi/2-p (atan (- n) +0.0)))
471 (is (-pi/2-p (atan (- n) -0.0))))
472 (remove-if-not #'plusp (append *floats* *ieee-fp.inf*)))))
473
474 (test ieee-fp.0016.atan2-special-case.inf-arg
475 ;; (atan +-inf +inf) -> +-pi/4
476 (let ((+inf ext:single-float-positive-infinity)
477 (-inf ext:single-float-negative-infinity))
478 (is (approx= (atan +inf +inf) (* +1/4 pi)))
479 (is (approx= (atan -inf +inf) (* -1/4 pi)))
480 (is (approx= (atan +inf -inf) (* +3/4 pi)))
481 (is (approx= (atan -inf -inf) (* -3/4 pi)))
482 (map () (lambda (n)
483 (is (+zerop (atan n +inf)))
484 (is (-zerop (atan (- n) +inf)))
485 (is (+pi-p (atan n -inf)))
486 (is (-pi-p (atan (- n) -inf)))
487 (unless (zerop n)
488 (+pi/2-p (atan +inf n))
489 (-pi/2-p (atan -inf n))))
490 (remove-if-not #'plusp *floats*)))))
491
492 (test ieee-fp.0017.infinity-sinh/cosh/tanh
493 (for-all-infinities -infinity +infinity
494 (type-and-value-check (sinh -infinity) -infinity)
495 (type-and-value-check (sinh +infinity) +infinity)
496 (type-and-value-check (cosh -infinity) +infinity)
497 (type-and-value-check (cosh +infinity) +infinity)
498 (type-and-value-check (tanh -infinity) -1)
499 (type-and-value-check (tanh +infinity) +1)
500 #|(without-fpe-traps
501 (type-and-value-check (sinh (complex 0 -infinity)) (complex (ext:nan) (ext:nan)))
502 (type-and-value-check (sinh (complex 0 +infinity)) (complex (ext:nan) (ext:nan)))
503 (type-and-value-check (cosh (complex 0 -infinity)) (complex (ext:nan) (ext:nan)))
504 (type-and-value-check (cosh (complex 0 +infinity)) (complex (ext:nan) (ext:nan)))
505 (type-and-value-check (tanh (complex 0 -infinity)) (complex (ext:nan) (ext:nan)))
506 (type-and-value-check (tanh (complex 0 +infinity)) (complex (ext:nan) (ext:nan))))|#))
507
508 (test ieee-fp.0018.infinity-asinh/acosh/atanh
509 (for-all-infinities -infinity +infinity
510 (type-and-value-check (asinh -infinity) -infinity)
511 (type-and-value-check (asinh +infinity) +infinity)
512 #| (without-fpe-traps
513 (complex-equality #'approx= (acosh -infinity) +infinity pi))|#
514 (type-and-value-check (acosh +infinity) +infinity)
515 #| (complex-equality #'approx= (atanh -infinity) 0 (/ pi 2))
516 (complex-equality #'approx= (atanh +infinity) 0 (/ pi 2))|#))
517
518 (test ieee-fp.0019.infinity-+/-
519 (for-all-infinities -infinity +infinity
520 (type-and-value-check (+ -infinity) -infinity)
521 (type-and-value-check (+ +infinity) +infinity)
522
523 (type-and-value-check (+ -infinity -infinity) -infinity)
524 (without-fpe-traps
525 (type-and-value-check (+ -infinity +infinity) (ext:nan))
526 (type-and-value-check (+ +infinity -infinity) (ext:nan)))
527 (type-and-value-check (+ +infinity +infinity) +infinity)
528
529 (type-and-value-check (- -infinity) +infinity)
530 (type-and-value-check (- +infinity) -infinity)
531
532 (without-fpe-traps
533 (type-and-value-check (- -infinity -infinity) (ext:nan)))
534 (type-and-value-check (- +infinity -infinity) +infinity)
535 (type-and-value-check (- -infinity +infinity) -infinity)
536 (without-fpe-traps
537 (type-and-value-check (- +infinity +infinity) (ext:nan)))
538 (for-all-number-subtypes (x real)
539 (type-and-value-check (+ x -infinity) -infinity)
540 (type-and-value-check (+ x +infinity) +infinity)
541 (type-and-value-check (+ -infinity x) -infinity)
542 (type-and-value-check (+ +infinity x) +infinity)
543
544 (type-and-value-check (- x -infinity) +infinity)
545 (type-and-value-check (- x +infinity) -infinity)
546 (type-and-value-check (- -infinity x) -infinity)
547 (type-and-value-check (- +infinity x) +infinity))))
548
549 (test ieee-fp.0020.infinity-*//
550 (for-all-infinities -infinity +infinity
551 (type-and-value-check (* -infinity) -infinity)
552 (type-and-value-check (* +infinity) +infinity)
553
554 (type-and-value-check (* -infinity -infinity) +infinity)
555 (type-and-value-check (* -infinity +infinity) -infinity)
556 (type-and-value-check (* +infinity -infinity) -infinity)
557 (type-and-value-check (* +infinity +infinity) +infinity)
558
559 (is (eql (/ -infinity) (coerce -0.0 (type-of -infinity))))
560 (is (eql (/ +infinity) (coerce +0.0 (type-of +infinity))))
561
562 (without-fpe-traps
563 (type-and-value-check (/ -infinity -infinity) (ext:nan))
564 (type-and-value-check (/ -infinity +infinity) (ext:nan))
565 (type-and-value-check (/ +infinity -infinity) (ext:nan))
566 (type-and-value-check (/ +infinity +infinity) (ext:nan)))
567 (for-all-number-subtypes (x real)
568 (type-and-value-check (* (abs x) -infinity) -infinity)
569 (type-and-value-check (* (- (abs x)) -infinity) +infinity)
570 (type-and-value-check (* (abs x) +infinity) +infinity)
571 (type-and-value-check (* (- (abs x)) +infinity) -infinity)
572
573 (type-and-value-check (* -infinity (abs x)) -infinity)
574 (type-and-value-check (* -infinity (- (abs x))) +infinity)
575 (type-and-value-check (* +infinity (abs x)) +infinity)
576 (type-and-value-check (* +infinity (- (abs x))) -infinity)
577
578 (is (eql (/ (abs x) -infinity) (coerce -0.0 (type-contagion (abs x) -infinity))))
579 (is (eql (/ (- (abs x)) -infinity) (coerce +0.0 (type-contagion (- (abs x)) -infinity))))
580 (is (eql (/ (abs x) +infinity) (coerce +0.0 (type-contagion (abs x) +infinity))))
581 (is (eql (/ (- (abs x)) +infinity) (coerce -0.0 (type-contagion (- (abs x)) +infinity))))
582
583 (type-and-value-check (/ -infinity (abs x)) -infinity)
584 (type-and-value-check (/ -infinity (- (abs x))) +infinity)
585 (type-and-value-check (/ +infinity (abs x)) +infinity)
586 (type-and-value-check (/ +infinity (- (abs x))) -infinity))))
587
588 (test ieee-fp.0021.infinity-1+/1-
589 (for-all-infinities -infinity +infinity
590 (type-and-value-check (1- -infinity) -infinity)
591 (type-and-value-check (1- +infinity) +infinity)
592 (type-and-value-check (1+ -infinity) -infinity)
593 (type-and-value-check (1+ +infinity) +infinity)))
594
595 (test ieee-fp.0022.infinity-abs
596 (for-all-infinities -infinity +infinity
597 (type-and-value-check (abs -infinity) +infinity)
598 (type-and-value-check (abs +infinity) +infinity)))
599
600 (test ieee-fp.0023.infinity-exp
601 (for-all-infinities -infinity +infinity
602 (type-and-value-check (exp -infinity) 0)
603 (type-and-value-check (exp +infinity) +infinity)))
604
605 (test ieee-fp.0024.infinity-expt
606 (without-fpe-traps
607 (flet ((relaxed-eql (x y)
608 ;; (relaxed-eql +0.0 -0.0) => true
609 (and (eql (type-of x) (type-of y))
610 (= x y))))
611 (for-all-infinities -infinity +infinity
612 ;; x^∞: |x| < 1
613 (for-all-number-subtypes (x (and real (not integer)) 1/2)
614 (type-and-value-check (expt x -infinity) +infinity)
615 #| (complex-equality #'eql (expt (- x) -infinity)
616 (coerce +infinity (type-contagion x -infinity)) ;
617 (coerce (ext:nan) (type-contagion x -infinity))) |#
618 (type-and-value-check (expt x +infinity) 0)
619 #| (complex-equality #'relaxed-eql (expt (- x) +infinity)
620 (coerce 0.0 (type-contagion x +infinity)) ;
621 (coerce 0.0 (type-contagion x +infinity))) |#)
622 ;; x^∞: |x| = 1
623 (for-all-number-subtypes (x (or fixnum float) 1)
624 (type-and-value-check (expt x -infinity) 1)
625 (without-fpe-traps
626 (complex-equality #'eql (expt (- x) -infinity)
627 (coerce (ext:nan) (type-contagion x -infinity))
628 (coerce (ext:nan) (type-contagion x -infinity))))
629 (type-and-value-check (expt x +infinity) 1)
630 (without-fpe-traps
631 (complex-equality #'eql (expt (- x) +infinity)
632 (coerce (ext:nan) (type-contagion x +infinity))
633 (coerce (ext:nan) (type-contagion x +infinity)))))
634 ;; x^∞: |x| > 1
635 (for-all-number-subtypes (x (or fixnum float) 2)
636 (type-and-value-check (expt x -infinity) 0)
637 #| (complex-equality #'relaxed-eql (expt (- x) -infinity)
638 (coerce 0.0 (type-contagion x -infinity)) ;
639 (coerce 0.0 (type-contagion x -infinity))) |#
640 (type-and-value-check (expt x +infinity) +infinity)
641 #| (complex-equality #'eql (expt (- x) +infinity)
642 (coerce +infinity (type-contagion x +infinity)) ;
643 (coerce (ext:nan) (type-contagion x +infinity))) |#)
644 ;; ∞^y: even y
645 (for-all-number-subtypes (y float 2)
646 (is (eql (realpart (expt -infinity y))
647 (coerce +infinity (type-contagion -infinity y))))
648 (complex-equality #'relaxed-eql (expt -infinity (- y))
649 (coerce 0.0 (type-contagion -infinity y))
650 (coerce 0.0 (type-contagion -infinity y)))
651 (type-and-value-check (expt +infinity y) +infinity)
652 (type-and-value-check (expt +infinity (- y)) 0.0))
653 (for-all-number-subtypes (y fixnum 2)
654 (type-and-value-check (expt -infinity y) +infinity)
655 (type-and-value-check (expt -infinity (- y)) 0.0)
656 (type-and-value-check (expt +infinity y) +infinity)
657 (type-and-value-check (expt +infinity (- y)) 0.0))
658 ;; ∞^y: odd y
659 (for-all-number-subtypes (y float 3)
660 ;; Should be -infinity, but C99 complex functions return +infinity
661 #| (is (eql (realpart (expt -infinity y))
662 (coerce -infinity (type-contagion -infinity y)))) |#
663 (complex-equality #'relaxed-eql (expt -infinity (- y))
664 (coerce 0.0 (type-contagion -infinity y))
665 (coerce 0.0 (type-contagion -infinity y)))
666 (type-and-value-check (expt +infinity y) +infinity)
667 (type-and-value-check (expt +infinity (- y)) 0.0))
668 (for-all-number-subtypes (y fixnum 3)
669 (type-and-value-check (expt -infinity y) -infinity)
670 (complex-equality #'relaxed-eql (expt -infinity (- y))
671 (coerce 0.0 (type-contagion -infinity y))
672 (coerce 0.0 (type-contagion -infinity y)))
673 (type-and-value-check (expt +infinity y) +infinity)
674 (type-and-value-check (expt +infinity (- y)) 0.0))
675 ;; ∞^y: non-integer y
676 (for-all-number-subtypes (y (and real (not integer)) 5/2)
677 (complex-equality #'relaxed-eql (expt -infinity (- y))
678 (coerce 0.0 (type-contagion -infinity y))
679 (coerce 0.0 (type-contagion -infinity y)))
680 (type-and-value-check (expt +infinity y) +infinity)
681 (type-and-value-check (expt +infinity (- y)) 0.0))
682 ;; ∞^∞
683 #| (complex-equality #'eql (expt -infinity -infinity)
684 (coerce 0.0 (type-contagion -infinity -infinity)) ;
685 (coerce -0.0 (type-contagion -infinity -infinity))) |#
686 #| (complex-equality #'eql (expt -infinity +infinity)
687 (coerce +infinity (type-contagion -infinity +infinity)) ;
688 (coerce (ext:nan) (type-contagion -infinity +infinity))) |#
689 (type-and-value-check (expt +infinity -infinity) 0.0)
690 (type-and-value-check (expt +infinity +infinity) +infinity)))))
691
692 (test ieee-fp.0025.infinity-log
693 (for-all-infinities -infinity +infinity
694 (complex-equality #'approx= (log -infinity) +infinity pi)
695 (type-and-value-check (log +infinity) +infinity)))
696
697 (test ieee-fp.0026.infinity-signum
698 (for-all-infinities -infinity +infinity
699 (type-and-value-check (signum -infinity) -1)
700 (type-and-value-check (signum +infinity) +1)))
701
702 (test ieee-fp.0027.infinity-sqrt
703 (for-all-infinities -infinity +infinity
704 (without-fpe-traps
705 (is (= (imagpart (sqrt -infinity)) +infinity)))
706 (type-and-value-check (sqrt +infinity) +infinity)))
707
708 (test ieee-fp.0028.infinity-cis
709 (without-fpe-traps
710 (for-all-infinities -infinity +infinity
711 (is (eql (cis -infinity)
712 (complex (coerce (ext:nan) (type-of -infinity))
713 (coerce (ext:nan) (type-of -infinity)))))
714 (is (eql (cis +infinity)
715 (complex (coerce (ext:nan) (type-of +infinity))
716 (coerce (ext:nan) (type-of +infinity))))))))
717
718 (test ieee-fp.0029.infinity-conjugate
719 (for-all-infinities -infinity +infinity
720 (type-and-value-check (conjugate -infinity) -infinity)
721 (type-and-value-check (conjugate +infinity) +infinity)
722 #| (for-all-number-subtypes (x real)
723 (type-and-value-check (conjugate (complex x -infinity)) (complex x +infinity))
724 (type-and-value-check (conjugate (complex x +infinity)) (complex x -infinity))) |#))
725
726 (test ieee-fp.0030.infinity-phase
727 (for-all-infinities -infinity +infinity
728 (is (approx= (phase -infinity) (- pi)))
729 (is (approx= (phase +infinity) 0))
730 #| (is (approx= (phase (complex 0 -infinity)) (- (/ pi 2))))
731 (is (approx= (phase (complex 0 +infinity)) (+ (/ pi 2))))
732 (is (approx= (phase (complex -infinity -infinity)) (- (* pi 3/4))))
733 (is (approx= (phase (complex -infinity +infinity)) (+ (* pi 3/4))))
734 (is (approx= (phase (complex +infinity -infinity)) (- (* pi 1/4))))
735 (is (approx= (phase (complex +infinity +infinity)) (+ (* pi 1/4)))) |#))
736
737
738
739 (test ieee-fp.0031.branch-cuts-signed-zero
740 (for-all-number-subtypes (x float (+ 1.0 (random 10.0)))
741 ;; branch cuts in [1,infinity)
742 (let ((z-above (complex x +0.0))
743 (z-below (complex x -0.0)))
744 (is (plusp (imagpart (asin z-above))))
745 (is (minusp (imagpart (asin z-below))))
746 (is (minusp (imagpart (acos z-above))))
747 (is (plusp (imagpart (acos z-below))))
748 (is (plusp (imagpart (atanh z-above))))
749 (is (minusp (imagpart (atanh z-below)))))
750 ;; branch cuts in (-infinity,-1]
751 (let ((z-above (complex (- x) +0.0))
752 (z-below (complex (- x) -0.0)))
753 (is (plusp (imagpart (asin z-above))))
754 (is (minusp (imagpart (asin z-below))))
755 (is (minusp (imagpart (acos z-above))))
756 (is (plusp (imagpart (acos z-below))))
757 (is (plusp (imagpart (atanh z-above))))
758 (is (minusp (imagpart (atanh z-below)))))
759 ;; branch cuts in [i,i*infinity)
760 (let ((z-left (complex -0.0 x))
761 (z-right (complex +0.0 x)))
762 (is (minusp (realpart (atan z-left))))
763 (is (plusp (realpart (atan z-right))))
764 (is (minusp (realpart (asinh z-left))))
765 (is (plusp (realpart (asinh z-right)))))
766 ;; branch cuts in (-i*infinity,-i]
767 (let ((z-left (complex -0.0 (- x)))
768 (z-right (complex +0.0 (- x))))
769 (is (minusp (realpart (atan z-left))))
770 (is (plusp (realpart (atan z-right))))
771 (is (minusp (realpart (asinh z-left))))
772 (is (plusp (realpart (asinh z-right))))))
773 (for-all-number-subtypes (x float (- 1.0 (random 10.0)))
774 ;; branch cuts in (-infinity,1]
775 (let ((z-above (complex x +0.0))
776 (z-below (complex x -0.0)))
777 (is (plusp (imagpart (acosh z-above))))
778 (is (minusp (imagpart (acosh z-below))))))
779 (for-all-number-subtypes (x float (- (random 10.0)))
780 ;; branch cuts in (-infinity,0]
781 (let ((z-above (complex x +0.0))
782 (z-below (complex x -0.0)))
783 (is (plusp (imagpart (sqrt z-above))))
784 (is (minusp (imagpart (sqrt z-below)))))))
785
786 (test ieee-fp.0032.bit-conversion/smoke
787 (is (= 3.14 (si:bits-single-float (si:single-float-bits 3.14))))
788 (is (= 3.14 (si:bits-double-float (si:double-float-bits 3.14))))
789 #-long-float
790 (is (= 3.14 (si:bits-long-float (si:long-float-bits 3.14))))
791 #+long-float
792 (progn (signals error (si:long-float-bits 3.14))
793 (signals error (si:bits-long-float 3.14))))
794
795 (test ieee-fp.0033.trap-fpe-smoke-test
796 (let ((bits (si:trap-fpe 'cl:last t)))
797 (unwind-protect
798 (dolist (flag '(t nil))
799 (finishes (si:trap-fpe t flag))
800 (finishes (si:trap-fpe bits flag))
801 (finishes (si:trap-fpe 'last flag))
802 (loop for sym in '(division-by-zero
803 floating-point-overflow
804 floating-point-underflow
805 floating-point-invalid-operation
806 floating-point-inexact)
807 do (finishes (si:trap-fpe sym flag) "~s should be a valid EXT:FPE-TRAP condition." sym))
808 (loop for sym in '(:last
809 :division-by-zero
810 :floating-point-overflow
811 :floating-point-underflow
812 :floating-point-invalid-operation
813 :floating-point-inexact)
814 do (signals error (si:trap-fpe sym flag) "~s should be an invalid EXT:FPE-TRAP condition." sym)))
815 (si:trap-fpe bits t))))
816
817 (test ieee-fp.0034.decode-float
818 (labels ((test-float (proto num res exp sign)
819 (equal (multiple-value-list (decode-float (float num proto)))
820 (list (float res proto) exp (float sign proto))))
821 (test-float* (num res exp sign)
822 (is (test-float 1.0f0 num res exp sign))
823 (is (test-float 1.0d0 num res exp sign))
824 (is (test-float 1.0l0 num res exp sign))))
825 (test-float* -10.0 0.625 4 -1.0)
826 (test-float* -1.0 0.5 1 -1.0)
827 (test-float* -0.0 0.0 0 -1.0)
828 (test-float* +0.0 0.0 0 +1.0)
829 (test-float* +1.0 0.5 1 +1.0)
830 (test-float* +10.0 0.625 4 +1.0)))