0.9.4.12:
[sbcl.git] / tests / float.pure.lisp
1 ;;;; floating-point-related tests with no side effects
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; While most of SBCL is derived from the CMU CL system, the test
7 ;;;; files (like this one) were written from scratch after the fork
8 ;;;; from CMU CL.
9 ;;;;
10 ;;;; This software is in the public domain and is provided with
11 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
12 ;;;; more information.
13
14 (cl:in-package :cl-user)
15
16 (dolist (ifnis (list (cons single-float-positive-infinity
17                            single-float-negative-infinity)
18                      (cons double-float-positive-infinity
19                            double-float-negative-infinity)))
20   (destructuring-bind (+ifni . -ifni) ifnis
21     (assert (= (* +ifni 1) +ifni))
22     (assert (= (* +ifni -0.1) -ifni))
23     (assert (= (+ +ifni -0.1) +ifni))
24     (assert (= (- +ifni -0.1) +ifni))
25     (assert (= (sqrt +ifni) +ifni))
26     (assert (= (* -ifni -14) +ifni))
27     (assert (= (/ -ifni 0.1) -ifni))
28     (assert (= (/ -ifni 100/3) -ifni))
29     (assert (not (= +ifni -ifni)))
30     (assert (= -ifni -ifni))
31     (assert (not (= +ifni 100/3)))
32     (assert (not (= -ifni -1.0 -ifni)))
33     (assert (not (= -ifni -17/02 -ifni)))
34     (assert (< -ifni +ifni))
35     (assert (not (< +ifni 100)))
36     (assert (not (< +ifni 100.0)))
37     (assert (not (< +ifni -ifni)))
38     (assert (< 100 +ifni))
39     (assert (< 100.0 +ifni))
40     (assert (>= 100 -ifni))
41     (assert (not (<= 6/7 (* 3 -ifni))))
42     (assert (not (> +ifni +ifni)))))
43
44 ;;; ANSI: FLOAT-RADIX should signal an error if its argument is not a
45 ;;; float.
46 ;;;
47 ;;; (Peter Van Eynde's ansi-test suite caught this, and Eric Marsden
48 ;;; reported a fix for CMU CL, which was ported to sbcl-0.6.12.35.)
49 (assert (typep (nth-value 1 (ignore-errors (float-radix "notfloat")))
50                'type-error))
51
52 (assert (typep (nth-value 1 (ignore-errors
53                               (funcall (fdefinition 'float-radix) "notfloat")))
54                'type-error))
55
56 ;;; Before 0.8.2.14 the cross compiler failed to work with
57 ;;; denormalized numbers
58 (when (subtypep 'single-float 'short-float)
59   (assert (eql least-positive-single-float least-positive-short-float)))
60
61 ;;; bug found by Paul Dietz: FFLOOR and similar did not work for integers
62 (let ((tests '(((ffloor -8 3) (-3.0 1))
63                ((fround -8 3) (-3.0 1))
64                ((ftruncate -8 3) (-2.0 -2))
65                ((fceiling -8 3) (-2.0 -2)))))
66   (loop for (exp res) in tests
67         for real-res = (multiple-value-list (eval exp))
68         do (assert (equal real-res res))))
69
70 ;;; bug 45b reported by PVE
71 (dolist (type '(short single double long))
72   (dolist (sign '(positive negative))
73     (let* ((name (find-symbol (format nil "LEAST-~A-~A-FLOAT"
74                                       sign type)
75                               :cl))
76            (value (symbol-value name)))
77       (assert (zerop (/ value 2))))))
78
79 ;;; bug found by Paul Dietz: bad rounding on small floats
80 (assert (= (fround least-positive-short-float least-positive-short-float) 1.0))
81
82 ;;; bug found by Peter Seibel: scale-float was only accepting float
83 ;;; exponents, when it should accept all integers.  (also bug #269)
84 (assert (= (multiple-value-bind (significand expt sign)
85                (integer-decode-float least-positive-double-float)
86              (* (scale-float (float significand 0.0d0) expt) sign))
87            least-positive-double-float))
88 (assert (= (multiple-value-bind (significand expt sign)
89                (decode-float least-positive-double-float)
90              (* (scale-float significand expt) sign))
91            least-positive-double-float))
92 (assert (= 0.0 (scale-float 1.0 most-negative-fixnum)))
93 (assert (= 0.0d0 (scale-float 1.0d0 (1- most-negative-fixnum))))
94
95 (with-test (:fails-on '(or :ppc)) ;; bug 372
96   (progn
97     (assert (raises-error? (scale-float 1.0 most-positive-fixnum)
98                            floating-point-overflow))
99     (assert (raises-error? (scale-float 1.0d0 (1+ most-positive-fixnum))
100                            floating-point-overflow))))
101
102 ;;; bug found by jsnell when nfroyd tried to implement better LOGAND
103 ;;; type derivation.
104 (assert (= (integer-decode-float (coerce -1756510900000000000
105                                          'single-float))
106            12780299))
107
108 ;;; MISC.564: no out-of-line %ATAN2 for constant folding
109 (assert (typep
110   (funcall
111    (compile
112     nil
113     '(lambda (p1)
114       (declare (optimize (speed 3) (safety 2) (debug 3) (space 0))
115        (type complex p1))
116       (phase (the (eql #c(1.0d0 2.0d0)) p1))))
117    #c(1.0d0 2.0d0))
118     'double-float))
119
120 (with-test (:fails-on '(or :ppc))
121   (assert (typep (nth-value
122                   1
123                   (ignore-errors
124                     (sb-sys:without-interrupts
125                      (loop repeat 2 summing most-positive-double-float)
126                      (sleep 2))))
127                  'floating-point-overflow)))