1.0.26.13: OpenBSD x86-64 support
[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 (:name (:scale-float-overflow :bug-372)
96             :fails-on '(or :ppc :darwin (and :x86 :openbsd))) ;; bug 372
97   (progn
98     (assert (raises-error? (scale-float 1.0 most-positive-fixnum)
99                            floating-point-overflow))
100     (assert (raises-error? (scale-float 1.0d0 (1+ most-positive-fixnum))
101                            floating-point-overflow))))
102
103 ;;; bug found by jsnell when nfroyd tried to implement better LOGAND
104 ;;; type derivation.
105 (assert (= (integer-decode-float (coerce -1756510900000000000
106                                          'single-float))
107            12780299))
108
109 ;;; MISC.564: no out-of-line %ATAN2 for constant folding
110 (assert (typep
111   (funcall
112    (compile
113     nil
114     '(lambda (p1)
115       (declare (optimize (speed 3) (safety 2) (debug 3) (space 0))
116        (type complex p1))
117       (phase (the (eql #c(1.0d0 2.0d0)) p1))))
118    #c(1.0d0 2.0d0))
119     'double-float))
120
121 ;;; More out of line functions (%COS, %SIN, %TAN) for constant folding,
122 ;;; reported by Mika Pihlajamäki
123 (funcall (compile nil '(lambda () (cos (tan (round 0))))))
124 (funcall (compile nil '(lambda () (sin (tan (round 0))))))
125 (funcall (compile nil '(lambda () (tan (tan (round 0))))))
126
127 (with-test (:name (:addition-overflow :bug-372)
128             :fails-on '(or :ppc :darwin (and (or :x86 :x86-64)
129                                              (or :netbsd :openbsd))))
130   (assert (typep (nth-value
131                   1
132                   (ignore-errors
133                     (sb-sys:without-interrupts
134                      (loop repeat 2 summing most-positive-double-float)
135                      (sleep 2))))
136                  'floating-point-overflow)))
137
138 ;;; On x86-64 generating complex floats on the stack failed an aver in
139 ;;; the compiler if the stack slot was the same as the one containing
140 ;;; the real part of the complex. The following expression was able to
141 ;;; trigger this in 0.9.5.62.
142 (with-test (:name :complex-float-stack)
143   (dolist (type '((complex double-float)
144                   (complex single-float)))
145     (compile nil
146              `(lambda (x0 x1 x2 x3 x4 x5 x6 x7)
147                 (declare (type ,type x0 x1 x2 x3 x4 x5 x6 x7))
148                 (let ((x0 (+ x0 x0))
149                       (x1 (+ x1 x1))
150                       (x2 (+ x2 x2))
151                       (x3 (+ x3 x3))
152                       (x4 (+ x4 x4))
153                       (x5 (+ x5 x5))
154                       (x6 (+ x6 x6))
155                       (x7 (+ x7 x7)))
156                   (* (+ x0 x1 x2 x3) (+ x4 x5 x6 x7)
157                      (+ x0 x2 x4 x6) (+ x1 x3 x5 x7)
158                      (+ x0 x3 x4 x7) (+ x1 x2 x5 x6)
159                      (+ x0 x1 x6 x7) (+ x2 x3 x4 x5)))))))
160
161
162 (with-test (:name :nan-comparisons
163             :fails-on '(or :sparc :mips))
164   (sb-int:with-float-traps-masked (:invalid)
165     (macrolet ((test (form)
166                  (let ((nform (subst '(/ 0.0 0.0) 'nan form)))
167                    `(progn
168                       (assert (eval ',nform))
169                       (assert (eval `(let ((nan (/ 0.0 0.0)))
170                                        ,',form)))
171                       (assert (funcall
172                                (compile nil `(lambda () ,',nform))))
173                       (assert (funcall
174                                (compile nil `(lambda (nan) ,',form))
175                                (/ 0.0 0.0)))))))
176       (test (/= nan nan))
177       (test (/= nan nan nan))
178       (test (/= 1.0 nan 2.0 nan))
179       (test (/= nan 1.0 2.0 nan))
180       (test (not (= nan 1.0)))
181       (test (not (= nan nan)))
182       (test (not (= nan nan nan)))
183       (test (not (= 1.0 nan)))
184       (test (not (= nan 1.0)))
185       (test (not (= 1.0 1.0 nan)))
186       (test (not (= 1.0 nan 1.0)))
187       (test (not (= nan 1.0 1.0)))
188       (test (not (>= nan nan)))
189       (test (not (>= nan 1.0)))
190       (test (not (>= 1.0 nan)))
191       (test (not (>= 1.0 nan 0.0)))
192       (test (not (>= 1.0 0.0 nan)))
193       (test (not (>= nan 1.0 0.0)))
194       (test (not (<= nan nan)))
195       (test (not (<= nan 1.0)))
196       (test (not (<= 1.0 nan)))
197       (test (not (<= 1.0 nan 2.0)))
198       (test (not (<= 1.0 2.0 nan)))
199       (test (not (<= nan 1.0 2.0)))
200       (test (not (< nan nan)))
201       (test (not (< -1.0 nan)))
202       (test (not (< nan 1.0)))
203       (test (not (> nan nan)))
204       (test (not (> -1.0 nan)))
205       (test (not (> nan 1.0))))))
206
207 (with-test (:name :log-int/double-accuracy)
208   ;; we used to use single precision for intermediate results
209   (assert (eql 2567.6046442221327d0
210                (log (loop for n from 1 to 1000 for f = 1 then (* f n)
211                           finally (return f))
212                     10d0)))
213   ;; both ways
214   (assert (eql (log 123123123.0d0 10) (log 123123123 10.0d0))))
215
216 (with-test (:name :log-base-zero-return-type)
217   (assert (eql 0.0f0 (log 123 (eval 0))))
218   (assert (eql 0.0d0 (log 123.0d0 (eval 0))))
219   (assert (eql 0.0d0 (log 123 (eval 0.0d0))))
220   (let ((f (compile nil '(lambda (x y)
221                           (declare (optimize speed))
222                           (etypecase x
223                             (single-float
224                              (etypecase y
225                                (single-float (log x y))
226                                (double-float (log x y))))
227                             (double-float
228                              (etypecase y
229                                (single-float (log x y))
230                                (double-float (log x y)))))))))
231     (assert (eql 0.0f0 (funcall f 123.0 0.0)))
232     (assert (eql 0.0d0 (funcall f 123.0d0 0.0)))
233     (assert (eql 0.0d0 (funcall f 123.0d0 0.0d0)))
234     (assert (eql 0.0d0 (funcall f 123.0 0.0d0)))))