abs.lsp 3.55 KB
Newer Older
1 2 3 4 5
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Mon Sep  1 20:16:42 2003
;;;; Contains: Tests of ABS

6

7

8

9 10

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

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

(deftest abs.error.3
19 20
  (signals-error (abs 0 nil nil) program-error)
  t)
21 22 23

(deftest abs.1
  (loop for x in *numbers*
sds's avatar
sds committed
24 25
        for a = (abs x)
        always (and (realp a) (not (minusp a))))
26 27 28 29
  t)

(deftest abs.2
  (loop for x = (random-fixnum)
sds's avatar
sds committed
30 31 32 33
        for a = (abs x)
        repeat 10000
        unless (if (plusp x) (eql x a) (eql (- x) a))
        collect (list x a))
34 35 36 37 38
  nil)

(deftest abs.3
  (let ((bound (ash 1 300)))
    (loop for x = (random-from-interval bound)
sds's avatar
sds committed
39 40 41 42
          for a = (abs x)
          repeat 10000
          unless (if (plusp x) (eql x a) (eql (- x) a))
          collect (list x a)))
43 44 45 46
  nil)

(deftest abs.4
  (loop for num = (random-fixnum)
sds's avatar
sds committed
47 48 49 50 51 52 53
        for den = (random-fixnum)
        for den2 = (if (zerop den) 1 den)
        for r = (/ num den)
        for a = (abs r)
        repeat 10000
        unless (if (>= r 0) (eql r a) (eql (- r) a))
        collect (list num den2 r a))
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
  nil)

(deftest abs.5
  (let ((bound (ash 1 210)))
    (loop
     for num = (random-from-interval bound)
     for den = (random-from-interval bound)
     for den2 = (if (zerop den) 1 den)
     for r = (/ num den)
     for a = (abs r)
     repeat 10000
     unless (if (>= r 0) (eql r a) (eql (- r) a))
     collect (list num den2 r a)))
  nil)

(deftest abs.6
  (let ((bound (float (ash 1 11) 1.0s0)))
    (loop
     for x = (random-from-interval bound)
     for a = (abs x)
     repeat 10000
     unless (if (minusp x)
sds's avatar
sds committed
76 77
                (eql (- x) a)
              (eql x a))
78 79 80 81 82 83 84 85 86 87
     collect (list x a)))
  nil)

(deftest abs.7
  (let ((bound (float (ash 1 22) 1.0f0)))
    (loop
     for x = (random-from-interval bound)
     for a = (abs x)
     repeat 10000
     unless (if (minusp x)
sds's avatar
sds committed
88 89
                (eql (- x) a)
              (eql x a))
90 91 92 93 94 95 96 97 98 99
     collect (list x a)))
  nil)

(deftest abs.8
  (let ((bound (float (ash 1 48) 1.0d0)))
    (loop
     for x = (random-from-interval bound)
     for a = (abs x)
     repeat 10000
     unless (if (minusp x)
sds's avatar
sds committed
100 101
                (eql (- x) a)
              (eql x a))
102 103 104 105 106 107 108 109 110 111
     collect (list x a)))
  nil)

(deftest abs.9
  (let ((bound (float (ash 1 48) 1.0l0)))
    (loop
     for x = (random-from-interval bound)
     for a = (abs x)
     repeat 10000
     unless (if (minusp x)
sds's avatar
sds committed
112 113
                (eql (- x) a)
              (eql x a))
114 115 116 117 118 119 120 121 122 123 124
     collect (list x a)))
  nil)

;;; The example on the abs page says that (abs -0.0) should be -0,0.
;;; However, FABS on the x86 returns 0.0 for that.  Since the examples
;;; in the hyperspec are not normative, the following four tests
;;; have been commented out.

;;; (deftest abs.10
;;;   (abs -0.0s0)
;;;   -0.0s0)
sds's avatar
sds committed
125
;;;
126 127 128
;;; (deftest abs.11
;;;   (abs -0.0f0)
;;;   -0.0f0)
sds's avatar
sds committed
129
;;;
130 131 132
;;; (deftest abs.12
;;;   (abs -0.0d0)
;;;   -0.0d0)
sds's avatar
sds committed
133
;;;
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
;;; (deftest abs.13
;;;   (abs -0.0l0)
;;;   -0.0l0)

;;; Complex numbers

(deftest abs.14
  (let ((result (abs #c(3 4))))
    (=t result 5))
  t)

(deftest abs.15
  (let ((result (abs #c(-3 4))))
    (=t result 5))
  t)

(deftest abs.16
  (let ((result (abs #c(3 -4))))
    (=t result 5))
  t)

(deftest abs.17
  (let ((result (abs #c(-3 -4))))
    (=t result 5))
  t)

(deftest abs.18
  (abs #c(3.0s0 4.0s0))
  5.0s0)

(deftest abs.19
  (abs #c(3.0f0 -4.0f0))
  5.0f0)

(deftest abs.20
  (abs #c(-3.0d0 4.0d0))
  5.0d0)

(deftest abs.21
  (abs #c(-3.0l0 4.0l0))
  5.0l0)
175 176 177

(deftest abs.22
  (macrolet ((%m (z) z))
sds's avatar
sds committed
178
            (abs (expand-in-current-env (%m -4))))
179
  4)