expt.lsp 5.35 KB
Newer Older
1 2 3 4 5
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Tue Sep  2 19:36:22 2003
;;;; Contains: Tests of EXPT

6

7

8 9
;;; Error tests

10
(deftest expt.error.1
11 12
  (signals-error (expt) program-error)
  t)
13 14

(deftest expt.error.2
15 16
  (signals-error (expt 1 1 1) program-error)
  t)
17 18

(deftest expt.error.3
19 20 21
  (signals-error (expt 1 1 nil nil) program-error)
  t)

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
(deftest expt.error.4
  (signals-error (expt most-positive-short-float 2) floating-point-overflow)
  t)

(deftest expt.error.5
  (signals-error (expt most-positive-single-float 2) floating-point-overflow)
  t)

(deftest expt.error.6
  (signals-error (expt most-positive-double-float 2) floating-point-overflow)
  t)

(deftest expt.error.7
  (signals-error (expt most-positive-long-float 2) floating-point-overflow)
  t)

(deftest expt.error.8
  (signals-error (expt least-positive-short-float 2) floating-point-underflow)
  t)

(deftest expt.error.9
  (signals-error (expt least-positive-single-float 2) floating-point-underflow)
  t)

(deftest expt.error.10
  (signals-error (expt least-positive-double-float 2) floating-point-underflow)
  t)

(deftest expt.error.11
pfdietz's avatar
pfdietz committed
51
  (signals-error (expt least-positive-long-float 2) floating-point-underflow)
52 53 54 55 56
  t)




57
;;; Non-error tests
58 59 60 61 62 63 64

(deftest expt.1
  (expt 0 0)
  1)

(deftest expt.2
  (loop for i from -1000 to 1000
sds's avatar
sds committed
65
        always (eql (expt i 0) 1))
66 67 68
  t)

(deftest expt.3
pfdietz's avatar
pfdietz committed
69
  (loop for i = (random 1.0s3)
sds's avatar
sds committed
70 71
        repeat 1000
        always (eql (expt i 0) 1.0s0))
72 73 74 75
  t)

(deftest expt.4
  (loop for i = (random 1.0f6)
sds's avatar
sds committed
76 77
        repeat 1000
        always (eql (expt i 0) 1.0f0))
78 79 80
  t)

(deftest expt.5
pfdietz's avatar
pfdietz committed
81
  (loop for i = (random 1.0d10)
sds's avatar
sds committed
82 83
        repeat 1000
        always (eql (expt i 0) 1.0d0))
84 85 86
  t)

(deftest expt.6
pfdietz's avatar
pfdietz committed
87
  (loop for i = (random 1.0l10)
sds's avatar
sds committed
88 89
        repeat 1000
        always (eql (expt i 0) 1.0l0))
90 91
  t)

pfdietz's avatar
pfdietz committed
92 93
(deftest expt.7
  (loop for i from -1000 to 1000
sds's avatar
sds committed
94 95
        for c = (complex i i)
        always (eql (expt c 0) 1))
pfdietz's avatar
pfdietz committed
96
  t)
97

pfdietz's avatar
pfdietz committed
98 99
(deftest expt.8
  (loop for i = (random 1.0s3)
sds's avatar
sds committed
100 101 102
        for c = (complex i i)
        repeat 1000
        always (eql (expt c 0) #c(1.0s0 0.0s0)))
pfdietz's avatar
pfdietz committed
103
  t)
104

pfdietz's avatar
pfdietz committed
105 106
(deftest expt.9
  (loop for i = (random 1.0f6)
sds's avatar
sds committed
107 108 109
        for c = (complex i i)
        repeat 1000
        always (eql (expt c 0) #c(1.0f0 0.0f0)))
pfdietz's avatar
pfdietz committed
110 111 112 113
  t)

(deftest expt.10
  (loop for i = (random 1.0d10)
sds's avatar
sds committed
114 115 116
        for c = (complex i i)
        repeat 1000
        always (eql (expt c 0) #c(1.0d0 0.0d0)))
pfdietz's avatar
pfdietz committed
117 118 119 120
  t)

(deftest expt.11
  (loop for i = (random 1.0l10)
sds's avatar
sds committed
121 122 123
        for c = (complex i i)
        repeat 1000
        always (eql (expt c 0) #c(1.0l0 0.0l0)))
pfdietz's avatar
pfdietz committed
124
  t)
125

pfdietz's avatar
pfdietz committed
126 127
(deftest expt.12
  (loop for x in *numbers*
sds's avatar
sds committed
128 129 130
        unless (or (floatp (realpart x))
                   (eql (expt x 1) x))
        collect x)
pfdietz's avatar
pfdietz committed
131 132 133 134
  nil)

(deftest expt.13
  (loop for x in *rationals*
sds's avatar
sds committed
135 136 137 138
        unless (and (eql (expt x 2) (* x x))
                    (or (zerop x)
                        (eql (expt x -1) (/ x))))
        collect x)
pfdietz's avatar
pfdietz committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
  nil)

(deftest expt.14
  (expt #c(0 2) 2)
  -4)

(deftest expt.15
  (expt #c(1 1) 2)
  #c(0 2))

(deftest expt.16
  (expt #c(1/2 1/3) 3)
  #c(-1/24 23/108))

(deftest expt.17
  (expt #c(1 1) -2)
  #c(0 -1/2))

(deftest expt.18
  (loop
   for zero in '(0.0s0 0.0f0 0.0d0 0.0l0)
   always
   (loop for i from -1000 to 1000
sds's avatar
sds committed
162 163
         always (or (zerop i)
                    (eql (expt i zero) (float 1 zero)))))
pfdietz's avatar
pfdietz committed
164 165 166 167 168 169 170
  t)

(deftest expt.19
  (loop
   for zero in '(0.0s0 0.0f0 0.0d0 0.0l0)
   always
   (loop for i from -1000 to 1000
sds's avatar
sds committed
171 172
         always (or (zerop i)
                    (eql (expt (float i 0.0s0) zero) (float 1 zero)))))
pfdietz's avatar
pfdietz committed
173 174 175 176 177 178 179
  t)

(deftest expt.20
  (loop
   for zero in '(0.0f0 0.0d0 0.0l0)
   always
   (loop for i from -1000 to 1000
sds's avatar
sds committed
180 181
         always (or (zerop i)
                    (eql (expt (float i 0.0f0) zero) (float 1 zero)))))
pfdietz's avatar
pfdietz committed
182 183 184 185 186 187 188
  t)

(deftest expt.21
  (loop
   for zero in '(0.0d0 0.0l0)
   always
   (loop for i from -1000 to 1000
sds's avatar
sds committed
189 190
         always (or (zerop i)
                    (eql (expt (float i 0.0d0) zero) (float 1 zero)))))
pfdietz's avatar
pfdietz committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
  t)

(deftest expt.22
  (expt 2.0f0 0.0s0)
  1.0f0)

(deftest expt.23
  (expt 2.0d0 0.0s0)
  1.0d0)

(deftest expt.24
  (expt 2.0l0 0.0s0)
  1.0l0)

(deftest expt.25
  (expt 2.0d0 0.0f0)
  1.0d0)

(deftest expt.26
  (expt 2.0l0 0.0f0)
  1.0l0)

(deftest expt.27
  (expt 2.0l0 0.0d0)
  1.0l0)

217 218 219
(deftest expt.28
  (<= (realpart (expt -8 1/3)) 0.0)
  nil)
pfdietz's avatar
pfdietz committed
220

221
#|
222
;;; FIXME
223 224 225 226 227
;;; I need to think more about how to do approximate float
;;; equality in a principled way.

(deftest expt.29
  (loop for bound in '(1.0s4 1.0f6 1.0d8 1.0l8)
sds's avatar
sds committed
228 229 230 231 232 233 234 235 236 237 238
        for ebound in (list short-float-epsilon single-float-epsilon
                            double-float-epsilon long-float-epsilon)
        for ebound2 = (max (* 2 ebound) (/ bound))
        nconc
        (loop for x = (1+ (random 1.0f6))
              for s1 = (sqrt x)
              for s2 = (expt x 1/2)
              for error = (/ (abs (- s2 s2)) x)
              repeat 1000
              unless (< error ebound2)
              collect (list x s1 s2)))
239
  nil)
pfdietz's avatar
pfdietz committed
240

241 242
(deftest expt.30
  (loop for bound in '(1.0s4 1.0f6 1.0d8 1.0l8)
sds's avatar
sds committed
243 244 245 246 247 248 249 250 251 252 253
        for ebound in (list short-float-epsilon single-float-epsilon
                            double-float-epsilon long-float-epsilon)
        for ebound2 = (max (* 2 ebound) (/ bound))
        nconc
        (loop for x = (- (1+ (random 1.0f6)))
              for s1 = (sqrt x)
              for s2 = (expt x 1/2)
              for error = (/ (abs (- s2 s2)) x)
              repeat 1000
              unless (< error ebound2)
              collect (list x s1 s2)))
254 255
  nil)
|#