-
Notifications
You must be signed in to change notification settings - Fork 4
/
abs.lsp
179 lines (153 loc) · 3.61 KB
/
abs.lsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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
175
176
177
178
179
;-*- Mode: Lisp -*-
;;;; Author: Paul Dietz
;;;; Created: Mon Sep 1 20:16:42 2003
;;;; Contains: Tests of ABS
(in-package :cl-test)
(compile-and-load "numbers-aux.lsp")
(deftest abs.error.1
(signals-error (abs) program-error)
t)
(deftest abs.error.2
(signals-error (abs 0 0) program-error)
t)
(deftest abs.error.3
(signals-error (abs 0 nil nil) program-error)
t)
(deftest abs.1
(loop for x in *numbers*
for a = (abs x)
always (and (realp a) (not (minusp a))))
t)
(deftest abs.2
(loop for x = (random-fixnum)
for a = (abs x)
repeat 10000
unless (if (plusp x) (eql x a) (eql (- x) a))
collect (list x a))
nil)
(deftest abs.3
(let ((bound (ash 1 300)))
(loop for x = (random-from-interval bound)
for a = (abs x)
repeat 10000
unless (if (plusp x) (eql x a) (eql (- x) a))
collect (list x a)))
nil)
(deftest abs.4
(loop for num = (random-fixnum)
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))
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)
(eql (- x) a)
(eql x a))
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)
(eql (- x) a)
(eql x a))
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)
(eql (- x) a)
(eql x a))
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)
(eql (- x) a)
(eql x a))
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)
;;;
;;; (deftest abs.11
;;; (abs -0.0f0)
;;; -0.0f0)
;;;
;;; (deftest abs.12
;;; (abs -0.0d0)
;;; -0.0d0)
;;;
;;; (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)
(deftest abs.22
(macrolet ((%m (z) z))
(abs (expand-in-current-env (%m -4))))
4)