sqrt.lsp 5.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Sat Sep  6 10:54:17 2003
;;;; Contains: Tests of SQRT

(in-package :cl-test)

(compile-and-load "numbers-aux.lsp")

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

(deftest sqrt.error.2
15 16
  (signals-error (sqrt 0 nil) program-error)
  t)
17 18

(deftest sqrt.error.3
19
  (check-type-error #'sqrt #'numberp)
20 21 22 23 24
  nil)

(deftest sqrt.1
  (let ((s (sqrt 0)))
    (and (realp s)
sds's avatar
sds committed
25
         (=t s 0)))
26 27 28 29 30
  t)

(deftest sqrt.2
  (let ((s (sqrt 1)))
    (and (realp s)
sds's avatar
sds committed
31
         (=t s 1)))
32 33 34 35
  t)

(deftest sqrt.3
  (loop for x in '(0.0s0 1.0s0 0.0f0 1.0f0 0.0d0 1.0d0 0.0l0 1.0l0)
sds's avatar
sds committed
36 37 38
        for s = (sqrt x)
        unless (eql s x)
        collect (list x s))
39 40 41 42
  nil)

(deftest sqrt.4
  (loop for x in '(0.0s0 1.0s0 0.0f0 1.0f0 0.0d0 1.0d0 0.0l0 1.0l0)
sds's avatar
sds committed
43 44 45 46
        for c = (complex x 0)
        for s = (sqrt c)
        unless (eql s c)
        collect (list x c s))
47 48 49 50
  nil)

(deftest sqrt.5
  (loop for x in '(-1.0s0 -1.0f0 -1.0d0 -1.0l0)
sds's avatar
sds committed
51 52 53
        for s = (sqrt x)
        unless (eql s (complex 0 (- x)))
        collect (list x s))
54 55 56 57 58
  nil)

;;; (deftest sqrt.6
;;;  (let ((result (sqrt (ash 1 10000))))
;;;    (if (integerp result)
sds's avatar
sds committed
59
;;;     (=t result (ash 1 5000))
60 61 62 63 64 65
;;;      (=t result (float (ash 1 5000) result))))
;;;  t)

(deftest sqrt.7
  (let ((result (sqrt -1)))
    (or (eqlt result #c(0 1))
sds's avatar
sds committed
66
        (eqlt result #c(0.0 1.0))))
67 68 69 70
  t)

(deftest sqrt.8
  (loop for x in *floats*
sds's avatar
sds committed
71 72 73 74 75 76 77
        for s = (sqrt x)
        unless (cond
                ((zerop x) (=t x 0))
                ((plusp x) (and (eqlt (float s x) s)
                                (eqlt (float x s) x)))
                (t (complexp s)))
        collect (list x s))
78 79 80 81
  nil)

(deftest sqrt.9
  (let ((upper (rational most-positive-double-float))
sds's avatar
sds committed
82
        (lower (rational most-negative-double-float)))
83
    (loop for x = (random-fixnum)
sds's avatar
sds committed
84 85 86 87 88 89 90 91 92 93 94 95 96
          repeat 1000
          unless (or (< x lower)
                     (> x upper)
                     (let ((s (sqrt x)))
                       (or (and (rationalp s)
                                (>= s 0)
                                (eql (* s s) x))
                           (and (floatp s) (>= x 0))
                           (and (complexp s)
                                (zerop (realpart s))
                                (> (imagpart s) 0)
                                (< x 0)))))
          collect (list x (sqrt x))))
97 98 99 100
  nil)

(deftest sqrt.10
  (loop for x from 1 to 1000
sds's avatar
sds committed
101 102 103 104 105 106
        for x2 = (* x x)
        for s = (sqrt x2)
        unless (if (rationalp s) (eql x s)
                 (and (typep s 'single-float)
                      (= x s)))
        collect (list x s))
107 108 109 110
  nil)

(deftest sqrt.11
  (loop for x from 1 to 1000
sds's avatar
sds committed
111 112 113 114 115 116 117 118 119
        for x2 = (* x x)
        for s = (sqrt (- x2))
        unless (and (complexp s)
                    (zerop (realpart s))
                    (let ((i (imagpart s)))
                      (if (rationalp i)
                          (eql i x)
                        (= i x))))
        collect (list x s))
120 121 122 123 124
  nil)

;;; Tests of the branch cut
(deftest sqrt.12
  (loop for xr = (random-fixnum)
sds's avatar
sds committed
125 126 127 128 129 130 131 132
        for xi = (random-fixnum)
        for c = (complex xr xi)
        for s = (sqrt c)
        repeat 1000
        unless (or (> (realpart s) 0)
                   (and (= (realpart s) 0)
                        (>= (imagpart s) 0)))
        collect (list c s))
133 134 135 136
  nil)

(deftest sqrt.13
  (loop for xr = (random-from-interval 1.0f6 -1.0f6)
sds's avatar
sds committed
137 138 139 140 141 142 143 144 145 146
        for xi = (random-from-interval 1.0f6 -1.0f6)
        for c = (complex xr xi)
        for s = (sqrt c)
        repeat 1000
        unless (or (> (realpart s) 0)
                   (and (= (realpart s) 0)
                        (>= (imagpart s) 0)))
        collect (list c s))
  nil)

147 148
(deftest sqrt.14
  (loop for xr = (random-from-interval 1.0s3 -1.0s3)
sds's avatar
sds committed
149 150 151 152 153 154 155 156 157 158
        for xi = (random-from-interval 1.0s3 -1.0s3)
        for c = (complex xr xi)
        for s = (sqrt c)
        repeat 1000
        unless (or (> (realpart s) 0)
                   (and (= (realpart s) 0)
                        (>= (imagpart s) 0)))
        collect (list c s))
  nil)

159 160
(deftest sqrt.15
  (loop for xr = (random-from-interval 1.0d7 -1.0d7)
sds's avatar
sds committed
161 162 163 164 165 166 167 168
        for xi = (random-from-interval 1.0d7 -1.0d7)
        for c = (complex xr xi)
        for s = (sqrt c)
        repeat 1000
        unless (or (> (realpart s) 0)
                   (and (= (realpart s) 0)
                        (>= (imagpart s) 0)))
        collect (list c s))
169 170 171 172
  nil)

(deftest sqrt.16
  (loop for xr = (random-from-interval 1.0l9 -1.0l9)
sds's avatar
sds committed
173 174 175 176 177 178 179 180
        for xi = (random-from-interval 1.0l9 -1.0l9)
        for c = (complex xr xi)
        for s = (sqrt c)
        repeat 1000
        unless (or (> (realpart s) 0)
                   (and (= (realpart s) 0)
                        (>= (imagpart s) 0)))
        collect (list c s))
181 182 183
  nil)

(deftest sqrt.17
pfdietz's avatar
pfdietz committed
184
  (let ((b1 (find-largest-exactly-floatable-integer most-positive-fixnum)))
185
    (loop for i = (random-from-interval (* b1 b1) 0)
sds's avatar
sds committed
186 187 188
          repeat 1000
          unless (>= (sqrt i) (isqrt i))
          collect i))
189 190 191 192
  nil)

(deftest sqrt.18
  (loop for x = (random-from-interval 1.0f6 0.0f0)
sds's avatar
sds committed
193 194 195
        repeat 1000
        unless (>= (sqrt x) (isqrt (floor x)))
        collect x)
196 197 198 199
  nil)

(deftest sqrt.19
  (loop for x in '(0.0s0 0.0f0 0.0d0 0.0l0)
sds's avatar
sds committed
200 201 202
        for s = (sqrt x)
        unless (= s x)
        collect (list x s))
203 204
  nil)