1828
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
204
|
3 |
"{ NameSpace: RegressionTests }"
|
|
4 |
|
|
5 |
TestCase subclass:#LargeFloatTest
|
2248
|
6 |
instanceVariableNames:'zero one two half minusOne minusTwo huge'
|
204
|
7 |
classVariableNames:''
|
|
8 |
poolDictionaries:''
|
1070
|
9 |
category:'tests-Regression-Numbers'
|
204
|
10 |
!
|
|
11 |
|
2248
|
12 |
LargeFloatTest comment:'Test to check FloatingPoint numbers with arbitrary precision'
|
1775
|
13 |
!
|
|
14 |
|
204
|
15 |
!LargeFloatTest class methodsFor:'documentation'!
|
|
16 |
|
2248
|
17 |
documentation
|
|
18 |
"
|
|
19 |
documentation to be added.
|
|
20 |
|
|
21 |
[author:]
|
|
22 |
Claus Gittinger
|
|
23 |
|
|
24 |
[instance variables:]
|
|
25 |
|
|
26 |
[class variables:]
|
|
27 |
|
|
28 |
[see also:]
|
|
29 |
|
|
30 |
"
|
|
31 |
! !
|
|
32 |
|
|
33 |
|
|
34 |
!LargeFloatTest methodsFor:'private'!
|
|
35 |
|
|
36 |
checkDoublePrecision: y forFunction: func precision: n
|
|
37 |
"Check that doubling the precision, then rounding would lead to the same result"
|
|
38 |
|
|
39 |
| anLargeFloat singlePrecisionResult |
|
|
40 |
anLargeFloat _ y asLargeFloatPrecision: n.
|
|
41 |
singlePrecisionResult _ anLargeFloat perform: func.
|
|
42 |
self checkThatEvaluatingFunction: func toDoublePrecisionOf: anLargeFloat equals: singlePrecisionResult.
|
|
43 |
^singlePrecisionResult
|
|
44 |
!
|
|
45 |
|
|
46 |
checkDoublePrecisionSerie: serie forFunction: func
|
|
47 |
^self checkDoublePrecisionSerie: serie forFunction: func precision: Float precision
|
|
48 |
!
|
|
49 |
|
|
50 |
checkDoublePrecisionSerie: serie forFunction: func precision: n
|
|
51 |
serie do: [:y | self checkDoublePrecision: y forFunction: func precision: n]
|
|
52 |
!
|
|
53 |
|
|
54 |
checkDoublePrecisionSerieVsFloat: serie forFunction: func
|
|
55 |
^serie reject: [:y |
|
|
56 |
| farb |
|
|
57 |
farb _ self checkDoublePrecision: y forFunction: func precision: Float precision.
|
|
58 |
[(y asFloat perform: func) = farb] on: ZeroDivide do: [false]]
|
|
59 |
!
|
|
60 |
|
|
61 |
checkThatEvaluatingFunction: func toDoublePrecisionOf: anLargeFloat equals: singlePrecisionResult
|
|
62 |
"Check that doubling the precision, then rounding would lead to the same result"
|
|
63 |
|
|
64 |
| n doublePrecision doublePrecisionResult lowBits |
|
|
65 |
n _ anLargeFloat precision.
|
|
66 |
doublePrecision _ anLargeFloat asLargeFloatPrecision: n * 2.
|
|
67 |
doublePrecisionResult _ doublePrecision perform: func.
|
|
68 |
|
|
69 |
"Note: the test must be guarded against double rounding error condition.
|
|
70 |
For example, suppose the single precision is 4 bits, double precision 8 bits.
|
|
71 |
If exact result is 1.001 | 0111 | 1001...
|
|
72 |
Then the nearest double is rounded to upper 1.001 | 1000
|
|
73 |
Then the nearest single to the double is rounded to upper 1.010
|
|
74 |
But the nearest single to the exact result should have been 1.001
|
|
75 |
To avoid this, we have to check if the second rounding is an exact tie"
|
|
76 |
doublePrecisionResult normalize.
|
|
77 |
lowBits _ doublePrecisionResult mantissa bitAnd: 1<<n-1.
|
|
78 |
lowBits = (1<<(n-1))
|
|
79 |
ifTrue:
|
|
80 |
["double precision is ambiguous - retry with quadruple..."
|
|
81 |
^self checkThatEvaluatingFunction: func toQuadruplePrecisionOf: anLargeFloat equals: singlePrecisionResult].
|
|
82 |
self assert: ((doublePrecisionResult asLargeFloatPrecision: n)- singlePrecisionResult) isZero
|
|
83 |
|
|
84 |
!
|
|
85 |
|
|
86 |
checkThatEvaluatingFunction: func toQuadruplePrecisionOf: anLargeFloat equals: singlePrecisionResult
|
|
87 |
"Check that quadrupling the precision, then rounding would lead to the same result"
|
|
88 |
|
|
89 |
| n quadruplePrecision quadruplePrecisionResult lowBits |
|
|
90 |
n _ anLargeFloat precision.
|
|
91 |
quadruplePrecision _ anLargeFloat asLargeFloatPrecision: n * 4.
|
|
92 |
quadruplePrecisionResult _ quadruplePrecision perform: func.
|
|
93 |
|
|
94 |
"Guard against double rounding error condition (exact tie)"
|
|
95 |
quadruplePrecisionResult normalize.
|
|
96 |
lowBits _ quadruplePrecisionResult mantissa bitAnd: 1<<(3*n)-1.
|
|
97 |
lowBits = (1<<(3*n-1))
|
|
98 |
ifTrue:
|
|
99 |
["quadruple precision is ambiguous - give up..."
|
|
100 |
^self].
|
|
101 |
self assert: ((quadruplePrecisionResult asLargeFloatPrecision: n)- singlePrecisionResult) isZero.
|
|
102 |
! !
|
|
103 |
|
|
104 |
!LargeFloatTest methodsFor:'setup'!
|
|
105 |
|
|
106 |
setUp
|
|
107 |
zero := 0 asLargeFloatPrecision: 53.
|
|
108 |
one := 1 asLargeFloatPrecision: 53.
|
|
109 |
two := 2 asLargeFloatPrecision: 53.
|
|
110 |
half := (1/2) asLargeFloatPrecision: 53.
|
|
111 |
minusOne := -1 asLargeFloatPrecision: 53.
|
|
112 |
minusTwo := -2 asLargeFloatPrecision: 53.
|
|
113 |
huge := (10 raisedTo: 100) asLargeFloatPrecision: 53.
|
|
114 |
|
|
115 |
"Modified (format): / 27-05-2019 / 08:25:42 / Claus Gittinger"
|
|
116 |
! !
|
|
117 |
|
|
118 |
!LargeFloatTest methodsFor:'testing-arithmetic'!
|
|
119 |
|
|
120 |
testDivide
|
|
121 |
| serie |
|
|
122 |
serie := {1. 2. 3. 5. 6. 7. 9. 10. 11. 12. 19. 243.
|
|
123 |
10 raisedTo: Float precision + 1.
|
|
124 |
Float precision factorial.
|
|
125 |
Float pi.
|
|
126 |
}.
|
|
127 |
serie do: [:num |
|
|
128 |
| nf na |
|
|
129 |
nf := num asFloat.
|
|
130 |
na := num asLargeFloatPrecision: Float precision.
|
|
131 |
serie do:[:den |
|
|
132 |
| df da ff fa |
|
|
133 |
df := den asFloat.
|
|
134 |
da := den asLargeFloatPrecision: Float precision.
|
|
135 |
ff := nf / df.
|
|
136 |
fa := na / da.
|
|
137 |
self assert: ff = fa]].
|
|
138 |
|
|
139 |
"Modified (format): / 27-05-2019 / 10:13:11 / Claus Gittinger"
|
|
140 |
!
|
|
141 |
|
|
142 |
testIEEEArithmeticVersusFloat
|
|
143 |
| floats ops ref new |
|
|
144 |
floats _ #(1.0 2.0 3.0 5.0 10.0 2r1.0e52 2r1.0e53 2r1.0e54 0.5 0.25 2r1.0e-52 2r1.0e-53 2r1.0e-54 1.0e60 0.1 1.1e-30 1.0e-60) copyWith: Float pi.
|
|
145 |
ops _ #(#+ #- #* #/ #= #< #> ).
|
|
146 |
ops
|
|
147 |
do: [:op | floats
|
|
148 |
do: [:f1 | floats
|
|
149 |
do: [:f2 |
|
|
150 |
ref _ f1 perform: op with: f2.
|
|
151 |
new _ (f1 asLargeFloatPrecision: 53)
|
|
152 |
perform: op
|
|
153 |
with: (f2 asLargeFloatPrecision: 53).
|
|
154 |
self assert: new = ref]]]
|
|
155 |
!
|
|
156 |
|
|
157 |
testIEEEArithmeticVersusIntegerAndFraction
|
|
158 |
"check that results are the same as IEEE 754 accelerated hardware
|
|
159 |
WARNING: this cannot be the case for denormalized numbers (gradual underflow)
|
|
160 |
because our exponent is unlimited"
|
|
161 |
|
|
162 |
| floats ops ref new intAndFractions |
|
|
163 |
floats _ #(1.0e0 2.0e0 3.0e0 5.0e0 10.0e0)
|
|
164 |
, (#(52 53 54 -52 -53 -54) collect: [:e | 1.0e0 timesTwoPower: e])
|
|
165 |
, #(0.5e0 0.25e0 1.0e60 0.1e0 1.1e-30 1.0e-60) copyWith: Float pi.
|
|
166 |
intAndFractions _ #(1 3 5 10 12345678901234567890 -1 -22 -3) copyWith: 7/9.
|
|
167 |
intAndFractions _ intAndFractions , (intAndFractions collect: [:e | e reciprocal]).
|
|
168 |
|
|
169 |
ops _ 1/10 = 0.1
|
|
170 |
ifTrue: [#(#+ #- #* #/)]
|
|
171 |
ifFalse: [#(#+ #- #* #/ #= #< #>)]. "BEWARE: LargeFloat compare exactly, Float don't, unless http://bugs.squeak.org/view.php?id=3374"
|
|
172 |
ops do:
|
|
173 |
[:op |
|
|
174 |
floats do:
|
|
175 |
[:f1 |
|
|
176 |
intAndFractions do:
|
|
177 |
[:f2 |
|
|
178 |
ref _ f1 perform: op with: f2 asFloat.
|
|
179 |
new _ (f1 asLargeFloatPrecision: 53) perform: op
|
|
180 |
with: (f2 asLargeFloatPrecision: 53).
|
|
181 |
self assert: new = ref.
|
|
182 |
new _ f1 perform: op
|
|
183 |
with: (f2 asLargeFloatPrecision: 53).
|
|
184 |
self assert: new = ref.
|
|
185 |
|
|
186 |
ref _ f1 perform: op with: f2.
|
|
187 |
new _ (f1 asLargeFloatPrecision: 53) perform: op
|
|
188 |
with: f2.
|
|
189 |
self assert: new = ref.
|
|
190 |
|
|
191 |
ref _ f2 asFloat perform: op with: f1.
|
|
192 |
new _ (f2 asLargeFloatPrecision: 53) perform: op
|
|
193 |
with: (f1 asLargeFloatPrecision: 53).
|
|
194 |
self assert: new = ref.
|
|
195 |
new _ (f2 asLargeFloatPrecision: 53) perform: op with: f1.
|
|
196 |
self assert: new = ref.
|
|
197 |
|
|
198 |
ref _ f2 perform: op with: f1.
|
|
199 |
new _ f2 perform: op
|
|
200 |
with: (f1 asLargeFloatPrecision: 53).
|
|
201 |
self assert: new = ref]]]
|
|
202 |
!
|
|
203 |
|
|
204 |
testMultiply
|
|
205 |
self assert: zero * zero = zero.
|
|
206 |
self assert: zero * minusOne = zero.
|
|
207 |
self assert: huge * zero = zero.
|
|
208 |
self assert: one * zero = zero.
|
|
209 |
|
|
210 |
self assert: one * two = two.
|
|
211 |
self assert: minusOne * one = minusOne.
|
|
212 |
self assert: minusOne * minusTwo = two.
|
|
213 |
|
|
214 |
self assert: half * two = one.
|
|
215 |
|
|
216 |
"check rounding"
|
|
217 |
self assert: huge * one = huge.
|
|
218 |
!
|
|
219 |
|
|
220 |
testNegated
|
|
221 |
self assert: zero negated = zero.
|
|
222 |
self assert: one negated = minusOne.
|
|
223 |
self assert: minusTwo negated = two.
|
|
224 |
self assert: huge negated negated = huge.
|
|
225 |
!
|
|
226 |
|
|
227 |
testPi
|
|
228 |
"check computation of pi"
|
|
229 |
|
|
230 |
self assert: (1 asLargeFloatPrecision: 53) pi = Float pi.
|
|
231 |
!
|
|
232 |
|
|
233 |
testRaisedToNegativeInteger
|
|
234 |
| n |
|
|
235 |
n _ 11.
|
|
236 |
1 to: 1<<n-1 do: [:i |
|
|
237 |
self assert: ((i asLargeFloatPrecision: n) raisedToInteger: -49)
|
|
238 |
equals: ((i raisedToInteger: -49) asLargeFloatPrecision: n) ].
|
|
239 |
!
|
|
240 |
|
|
241 |
testRaisedToPositiveInteger
|
|
242 |
| n |
|
|
243 |
n _ 11.
|
|
244 |
1 to: 1<<n-1 do: [:i |
|
|
245 |
self assert: ((i asLargeFloatPrecision: n) raisedToInteger: 49)
|
|
246 |
equals: ((i raisedToInteger: 49) asLargeFloatPrecision: n) ].
|
|
247 |
!
|
|
248 |
|
|
249 |
testReciprocal
|
|
250 |
| b |
|
|
251 |
b _ 1 << (Float precision - 1).
|
|
252 |
1 to: 10000 do: [:i |
|
|
253 |
| a |
|
|
254 |
a _ i asLargeFloatPrecision: Float precision.
|
|
255 |
self assert: a reciprocal = i asFloat reciprocal.
|
|
256 |
self assert: (a+b) reciprocal = (i+b) asFloat reciprocal.
|
|
257 |
self assert: a negated reciprocal = i asFloat negated reciprocal.]
|
|
258 |
!
|
|
259 |
|
|
260 |
testRoundToNearestEven
|
|
261 |
"Check that IEEE default rounding mode is honoured,
|
|
262 |
that is rounding to nearest even"
|
|
263 |
|
|
264 |
self assert: ((one timesTwoPower: 52)+(0+(1/4))) asTrueFraction = ((1 bitShift: 52)+0).
|
|
265 |
self assert: ((one timesTwoPower: 52)+(0+(1/2))) asTrueFraction = ((1 bitShift: 52)+0).
|
|
266 |
self assert: ((one timesTwoPower: 52)+(0+(3/4))) asTrueFraction = ((1 bitShift: 52)+1).
|
|
267 |
self assert: ((one timesTwoPower: 52)+(1+(1/4))) asTrueFraction = ((1 bitShift: 52)+1).
|
|
268 |
self assert: ((one timesTwoPower: 52)+(1+(1/2))) asTrueFraction = ((1 bitShift: 52)+2).
|
|
269 |
self assert: ((one timesTwoPower: 52)+(1+(3/4))) asTrueFraction = ((1 bitShift: 52)+2).
|
|
270 |
!
|
|
271 |
|
|
272 |
testRoundToNearestEvenAgainstIEEEDouble
|
|
273 |
"Check that IEEE default rounding mode is honoured"
|
|
274 |
|
|
275 |
#(1 2 3 5 6 7) do:
|
|
276 |
[:i |
|
|
277 |
self assert: ((one timesTwoPower: 52) + (i / 4)) asTrueFraction
|
|
278 |
= ((1 asFloat timesTwoPower: 52) + (i / 4)) asTrueFraction.
|
|
279 |
self assert: ((one timesTwoPower: 52) - (i / 4)) asTrueFraction
|
|
280 |
= ((1 asFloat timesTwoPower: 52) - (i / 4)) asTrueFraction]
|
|
281 |
!
|
|
282 |
|
|
283 |
testSubtract
|
|
284 |
self assert: zero - zero = zero.
|
|
285 |
self assert: zero - minusOne = one.
|
|
286 |
self assert: huge - zero = huge.
|
|
287 |
self assert: one - zero = one.
|
|
288 |
|
|
289 |
self assert: one - minusOne = two.
|
|
290 |
self assert: minusOne - minusTwo = one.
|
|
291 |
self assert: minusOne - one = minusTwo.
|
|
292 |
|
|
293 |
"check rounding"
|
|
294 |
self assert: huge - one = huge.
|
|
295 |
!
|
|
296 |
|
|
297 |
testSum
|
|
298 |
self assert: zero + zero = zero.
|
|
299 |
self assert: zero + minusOne = minusOne.
|
|
300 |
self assert: huge + zero = huge.
|
|
301 |
self assert: one + zero = one.
|
|
302 |
|
|
303 |
self assert: one + minusOne = zero.
|
|
304 |
self assert: minusOne + two = one.
|
|
305 |
self assert: one + minusTwo = minusOne.
|
|
306 |
|
|
307 |
"check rounding"
|
|
308 |
self assert: huge + one = huge.
|
|
309 |
!
|
|
310 |
|
|
311 |
testZeroOne
|
|
312 |
|
|
313 |
self assert: (312 asLargeFloatPrecision: 53) one = 1.
|
|
314 |
self assert: (312 asLargeFloatPrecision: 24) zero isZero.
|
|
315 |
|
|
316 |
self assert: (312 asLargeFloatPrecision: 53) one asInteger = 1.
|
|
317 |
self assert: (312 asLargeFloatPrecision: 24) zero asInteger isZero.
|
|
318 |
! !
|
|
319 |
|
|
320 |
!LargeFloatTest methodsFor:'testing-coercing'!
|
|
321 |
|
|
322 |
testCoercingDivide
|
|
323 |
(Array with: 1/2 with: 0.5e0) do: [:heteroHalf |
|
|
324 |
self assert: one / heteroHalf = two.
|
|
325 |
self assert: (one / heteroHalf) class = one class.
|
|
326 |
self assert: (one / heteroHalf) precision = one precision.
|
|
327 |
self assert: heteroHalf / one = half.
|
|
328 |
self assert: (heteroHalf / one) class = one class.
|
|
329 |
self assert: (heteroHalf / one) precision = one precision].
|
|
330 |
|
|
331 |
self assert: one / 2 = half.
|
|
332 |
self assert: (one / 2) class = one class.
|
|
333 |
self assert: (one / 2) precision = one precision.
|
|
334 |
self assert: -2 / two = minusOne.
|
|
335 |
self assert: (-2 / two) class = two class.
|
|
336 |
self assert: (-2 / two) precision = two precision.
|
|
337 |
!
|
|
338 |
|
|
339 |
testCoercingEqual
|
|
340 |
self assert: half = (1/2).
|
|
341 |
self assert: (1/2) = half.
|
|
342 |
self deny: half = (1/3).
|
|
343 |
self deny: (1/3) = half.
|
|
344 |
|
|
345 |
self assert: two = 2.
|
|
346 |
self assert: -2 = minusTwo.
|
|
347 |
self deny: -3 = two.
|
|
348 |
self deny: two = 3.
|
|
349 |
|
|
350 |
self assert: half = (0.5e0).
|
|
351 |
self assert: (0.5e0) = half.
|
|
352 |
self deny: half = (0.33e0).
|
|
353 |
self deny: (0.33e0) = half.
|
|
354 |
!
|
|
355 |
|
|
356 |
testCoercingLessThan
|
|
357 |
self deny: half < (1/2).
|
|
358 |
self assert: (1/3) < half.
|
|
359 |
self assert: minusOne < (1/2).
|
|
360 |
self deny: (1/3) < minusTwo.
|
|
361 |
|
|
362 |
self assert: two < 3.
|
|
363 |
self deny: two < 2.
|
|
364 |
self deny: two < 1.
|
|
365 |
self deny: two < -1.
|
|
366 |
self assert: minusTwo < -1.
|
|
367 |
self assert: minusTwo < 1.
|
|
368 |
self deny: minusTwo < -2.
|
|
369 |
self deny: minusTwo < -3.
|
|
370 |
|
|
371 |
self deny: half < (0.5e0).
|
|
372 |
self deny: half < (0.33e0).
|
|
373 |
self assert: half < (0.66e0).
|
|
374 |
self deny: (0.5e0) < half.
|
|
375 |
self assert: (0.33e0) < half.
|
|
376 |
self deny: (0.66e0) < half.
|
|
377 |
!
|
|
378 |
|
|
379 |
testCoercingMultiply
|
|
380 |
(Array with: 1/2 with: 0.5e0) do: [:heteroHalf |
|
|
381 |
self assert: two * heteroHalf = one.
|
|
382 |
self assert: (two * heteroHalf) class = half class.
|
|
383 |
self assert: (two * heteroHalf) precision = half precision.
|
|
384 |
self assert: heteroHalf * two = one.
|
|
385 |
self assert: (heteroHalf * two) class = half class.
|
|
386 |
self assert: (heteroHalf * two) precision = half precision].
|
|
387 |
|
|
388 |
self assert: minusOne * 2 = minusTwo.
|
|
389 |
self assert: (minusOne * 2) class = minusOne class.
|
|
390 |
self assert: (minusOne * 2) precision = minusOne precision.
|
|
391 |
self assert: 2 * one = two.
|
|
392 |
self assert: (2 * one) class = one class.
|
|
393 |
self assert: (2 * one) precision = one precision.
|
|
394 |
!
|
|
395 |
|
|
396 |
testCoercingSubtract
|
|
397 |
(Array with: 1/2 with: 0.5e0) do: [:heteroHalf |
|
|
398 |
self assert: half - heteroHalf = zero.
|
|
399 |
self assert: (half - heteroHalf) class = half class.
|
|
400 |
self assert: (half - heteroHalf) precision = half precision.
|
|
401 |
self assert: heteroHalf - half = zero.
|
|
402 |
self assert: (heteroHalf - half) class = half class.
|
|
403 |
self assert: (heteroHalf - half) precision = half precision].
|
|
404 |
|
|
405 |
self assert: one - 1 = zero.
|
|
406 |
self assert: (one - 1) class = minusOne class.
|
|
407 |
self assert: (one - 1) precision = minusOne precision.
|
|
408 |
self assert: -2 - minusTwo = zero.
|
|
409 |
self assert: (-2 - minusTwo) class = minusTwo class.
|
|
410 |
self assert: (-2 - minusTwo) precision = minusTwo precision.
|
1775
|
411 |
!
|
|
412 |
|
2248
|
413 |
testCoercingSum
|
|
414 |
(Array with: 1/2 with: 0.5e0) do: [:heteroHalf |
|
|
415 |
self assert: half + heteroHalf = one.
|
|
416 |
self assert: (half + heteroHalf) class = half class.
|
|
417 |
self assert: (half + heteroHalf) precision = half precision.
|
|
418 |
self assert: heteroHalf + half = one.
|
|
419 |
self assert: (heteroHalf + half) class = half class.
|
|
420 |
self assert: (heteroHalf + half) precision = half precision].
|
|
421 |
|
|
422 |
self assert: minusOne + 1 = zero.
|
|
423 |
self assert: (minusOne + 1) class = minusOne class.
|
|
424 |
self assert: (minusOne + 1) precision = minusOne precision.
|
|
425 |
self assert: 2 + minusTwo = zero.
|
|
426 |
self assert: (2 + minusTwo) class = minusTwo class.
|
|
427 |
self assert: (2 + minusTwo) precision = minusTwo precision.
|
|
428 |
!
|
|
429 |
|
|
430 |
testInfinityAndNaN
|
|
431 |
| inf nan |
|
|
432 |
inf _ Float infinity.
|
|
433 |
nan _ Float nan.
|
|
434 |
self assert: inf + two equals: inf.
|
|
435 |
self assert: half + inf negated equals: inf negated.
|
|
436 |
self assert: (nan + minusOne) isNaN .
|
|
437 |
self assert: inf - huge equals: inf.
|
|
438 |
self assert: half - inf equals: inf negated.
|
|
439 |
self assert: minusTwo - inf negated equals: inf.
|
|
440 |
self assert: (one - nan) isNaN.
|
|
441 |
self assert: inf * two equals: inf.
|
|
442 |
self assert: minusOne * inf equals: inf negated.
|
|
443 |
self assert: inf negated * minusOne equals: inf.
|
|
444 |
self assert: (huge * nan) isNaN.
|
|
445 |
self assert: inf negated / minusTwo equals: inf.
|
|
446 |
self assert: zero / inf negated equals: 0.
|
|
447 |
self assert: one / inf equals: 0.
|
|
448 |
self should: [inf / zero] raise: ZeroDivide.
|
|
449 |
self assert: (nan / two) isNaN.
|
|
450 |
self assert: (inf raisedTo: huge) equals: inf.
|
|
451 |
self assert: (huge raisedTo: inf) equals: inf.
|
|
452 |
self assert: (nan raisedTo: two) isNaN.
|
|
453 |
self assert: (two raisedTo: nan) isNaN.
|
|
454 |
self deny: nan <= one.
|
|
455 |
self deny: zero >= nan.
|
|
456 |
self assert: one < inf.
|
|
457 |
self assert: zero ~= nan.
|
|
458 |
self deny: nan = one.
|
|
459 |
! !
|
|
460 |
|
|
461 |
!LargeFloatTest methodsFor:'testing-compare'!
|
|
462 |
|
|
463 |
testEqual
|
|
464 |
self assert: zero = zero.
|
|
465 |
self assert: one = one.
|
|
466 |
self assert: one = one copy.
|
|
467 |
self assert: one = (one asLargeFloatPrecision: one precision * 2).
|
|
468 |
|
|
469 |
self deny: zero = one.
|
|
470 |
self deny: minusOne = one.
|
|
471 |
|
|
472 |
self assert: zero = 0.
|
|
473 |
self assert: 0 = zero.
|
|
474 |
self assert: zero = 0.0.
|
|
475 |
self assert: 0.0 = zero.
|
|
476 |
|
|
477 |
self deny: two = (1/2).
|
|
478 |
self deny: (1/2) = two.
|
|
479 |
self deny: zero = 1.0.
|
|
480 |
self deny: 0.0 = one.
|
|
481 |
|
|
482 |
self deny: one = nil.
|
|
483 |
self deny: nil = one.
|
|
484 |
!
|
|
485 |
|
|
486 |
testGreaterThan
|
|
487 |
|
|
488 |
self assert: zero < one.
|
|
489 |
self deny: one > two.
|
|
490 |
self deny: two > huge.
|
|
491 |
self deny: minusOne > one.
|
|
492 |
self deny: minusTwo > minusOne.
|
|
493 |
self deny: minusTwo > huge.
|
|
494 |
|
|
495 |
self assert: huge > one.
|
|
496 |
self assert: huge > zero.
|
|
497 |
self assert: huge > minusOne.
|
|
498 |
self assert: one > minusOne.
|
|
499 |
self assert: minusOne > minusTwo.
|
|
500 |
!
|
|
501 |
|
|
502 |
testIsZero
|
|
503 |
self assert: zero isZero.
|
|
504 |
self deny: one isZero.
|
|
505 |
self deny: minusTwo isZero.
|
|
506 |
!
|
|
507 |
|
|
508 |
testLessThan
|
|
509 |
|
|
510 |
self assert: zero < one.
|
|
511 |
self assert: one < two.
|
|
512 |
self assert: two < huge.
|
|
513 |
self assert: minusOne < one.
|
|
514 |
self assert: minusTwo < minusOne.
|
|
515 |
self assert: minusTwo < huge.
|
|
516 |
|
|
517 |
self deny: huge < one.
|
|
518 |
self deny: huge < zero.
|
|
519 |
self deny: huge < minusOne.
|
|
520 |
self deny: one < minusOne.
|
|
521 |
self deny: minusOne < minusTwo.
|
|
522 |
!
|
|
523 |
|
|
524 |
testNegative
|
|
525 |
|
|
526 |
self deny: zero negative.
|
|
527 |
self deny: two negative.
|
|
528 |
self assert: minusTwo negative.
|
|
529 |
!
|
|
530 |
|
|
531 |
testPositive
|
|
532 |
|
|
533 |
self assert: zero positive.
|
|
534 |
self assert: one positive.
|
|
535 |
self deny: minusOne positive.
|
|
536 |
! !
|
|
537 |
|
|
538 |
!LargeFloatTest methodsFor:'testing-converting'!
|
|
539 |
|
|
540 |
testAsFloat
|
|
541 |
self assert: (half asLargeFloatPrecision: Float precision) asFloat = 0.5e0.
|
|
542 |
self assert: (half asLargeFloatPrecision: Float precision * 2) asFloat = 0.5e0.
|
|
543 |
!
|
|
544 |
|
|
545 |
testAsFloatWithUnderflow
|
|
546 |
| fmin fminA |
|
|
547 |
fmin _ Float fmin.
|
|
548 |
fminA _ fmin asLargeFloatPrecision: one precision.
|
|
549 |
Float emin - Float precision + 1 to: Float emin + 1 do: [:n |
|
|
550 |
self assert: ((one timesTwoPower: n) + fminA) asFloat = ((1.0e0 timesTwoPower: n) + fmin)].
|
|
551 |
!
|
|
552 |
|
|
553 |
testAsMinimalDecimalFraction
|
|
554 |
| emax emin leadingOne significands |
|
|
555 |
significands _ 0 to: 1<<10-1.
|
|
556 |
leadingOne _ 1<<10.
|
|
557 |
emin _ -14.
|
|
558 |
emax _ 15.
|
|
559 |
|
|
560 |
"Test all normal finite half precision float"
|
|
561 |
emin to: emax do: [:e |
|
|
562 |
significands do: [:s |
|
|
563 |
| f |
|
|
564 |
f _ (leadingOne + s asLargeFloatPrecision: 11) timesTwoPower: e - 10.
|
|
565 |
self assert: (f asMinimalDecimalFraction asLargeFloatPrecision: 11) = f]].
|
|
566 |
|
|
567 |
"Test all subnormal finite half precision float"
|
|
568 |
significands do: [:s |
|
|
569 |
| f |
|
|
570 |
f _ (s asLargeFloatPrecision: s highBit) timesTwoPower: emin - 10.
|
|
571 |
self assert: (f asMinimalDecimalFraction asLargeFloatPrecision: s highBit) = f].
|
|
572 |
!
|
|
573 |
|
|
574 |
testPrintAndEvaluate
|
|
575 |
<timeout: 50 "seconds">
|
|
576 |
| emax emin leadingOne significands |
|
|
577 |
significands _ 0 to: 1<<10-1.
|
|
578 |
leadingOne _ 1<<10.
|
|
579 |
emin _ -14.
|
|
580 |
emax _ 15.
|
|
581 |
|
|
582 |
"Test all normal finite half precision float"
|
|
583 |
emin to: emax do: [:e |
|
|
584 |
significands do: [:s |
|
|
585 |
| f |
|
|
586 |
f _ (leadingOne + s asLargeFloatPrecision: 11) timesTwoPower: e - 10.
|
|
587 |
self assert: (Compiler evaluate: f storeString) = f.
|
|
588 |
self assert: (Compiler evaluate: f printString) = f.]].
|
|
589 |
|
|
590 |
"Test all subnormal finite half precision float"
|
|
591 |
significands do: [:s |
|
|
592 |
| f |
|
|
593 |
f _ (s asLargeFloatPrecision: s highBit) timesTwoPower: emin - 10.
|
|
594 |
self assert: (Compiler evaluate: f storeString) = f.
|
|
595 |
self assert: (Compiler evaluate: f printString) = f].
|
204
|
596 |
! !
|
1070
|
597 |
|
2248
|
598 |
!LargeFloatTest methodsFor:'testing-functions'!
|
|
599 |
|
|
600 |
testExp
|
|
601 |
<timeout: 10 "seconds">
|
|
602 |
| badExp serie |
|
|
603 |
serie _ ((-20 to: 20) collect: [:e |e asFloat]).
|
|
604 |
badExp _ self checkDoublePrecisionSerieVsFloat: serie forFunction: #exp.
|
|
605 |
badExp isEmpty ifFalse: [Transcript cr; show: 'bad exp for ' , badExp printString]
|
|
606 |
!
|
|
607 |
|
|
608 |
testExpLn
|
|
609 |
|n|
|
|
610 |
self assert: (1 asLargeFloatPrecision: 53) exp asFloat = 1 asFloat exp.
|
|
611 |
n _ 5 exp.
|
|
612 |
self assert: ((5 asLargeFloatPrecision: 53) exp - n)abs <= n ulp.
|
|
613 |
"self assert: (5 asLargeFloatPrecision: 53) exp asFloat = 5 asFloat exp."
|
|
614 |
self assert: ((5 asLargeFloatPrecision: 53) exp ln asFloat - n ln)abs <= 5.0 ulp.
|
|
615 |
"this test was skipped. changed that & loosened 2. test,
|
|
616 |
since '5 exp' seems to round up instead of down here,
|
|
617 |
which results in an error of almost one ulp in '5 exp'"
|
|
618 |
!
|
|
619 |
|
|
620 |
testLn
|
|
621 |
<timeout: 10 "seconds">
|
|
622 |
| badLn serie |
|
|
623 |
serie _ ((1 to: 100) collect: [:e |e asFloat]).
|
|
624 |
badLn _ self checkDoublePrecisionSerieVsFloat: serie forFunction: #ln.
|
|
625 |
badLn isEmpty ifFalse: [Transcript cr; show: 'bad ln for ' , badLn printString]
|
|
626 |
!
|
|
627 |
|
|
628 |
testLnDomainError
|
|
629 |
self should: [(-2 asLargeFloatPrecision: 24) ln] raise: DomainError.
|
|
630 |
!
|
|
631 |
|
|
632 |
testSqrt
|
|
633 |
<timeout: 10 "seconds">
|
|
634 |
| badSqrt serie |
|
|
635 |
"knowing that (10**3) < (2**10), 100 bits are enough for representing 10**30 exactly"
|
|
636 |
self assert: ((10 raisedTo: 30) asLargeFloatPrecision: 100) sqrt = (10 raisedTo: 15).
|
|
637 |
|
|
638 |
serie _ ((0 to: 20) collect: [:e | e asFloat]) , ((2 to: 20) collect: [:e | e reciprocal asFloat]).
|
|
639 |
badSqrt _ self checkDoublePrecisionSerieVsFloat: serie forFunction: #sqrt.
|
|
640 |
badSqrt isEmpty ifFalse: [Transcript cr; show: 'bad sqrt for ' , badSqrt printString]
|
|
641 |
!
|
|
642 |
|
|
643 |
testSqrtDomainError
|
|
644 |
self should: [(-2 asLargeFloatPrecision: 24) sqrt] raise: DomainError.
|
|
645 |
! !
|
|
646 |
|
|
647 |
!LargeFloatTest methodsFor:'testing-hyperbolic'!
|
|
648 |
|
|
649 |
hyperbolicSerie
|
|
650 |
^#(-3.0e0 -0.1e0 0.0e0 1.0e-20 1.0e-10 0.99e0 1.0e0 2.5e0 3.0e0 10.25e0) , (Array with: (3/10) asFloat with: (22/7) asFloat)
|
|
651 |
!
|
|
652 |
|
|
653 |
testArCosh
|
|
654 |
<timeout: 5 "seconds">
|
|
655 |
| serie |
|
|
656 |
serie _ ((1 to: 10) , #(1.0001 100 1000 1.0e20)) collect: [:e | e asFloat].
|
|
657 |
self checkDoublePrecisionSerie: serie forFunction: #arCosh
|
|
658 |
!
|
|
659 |
|
|
660 |
testArCoshDomainError
|
|
661 |
self should: [(1/2 asLargeFloatPrecision: 24) arCosh] raise: DomainError.
|
|
662 |
!
|
|
663 |
|
|
664 |
testArSinh
|
|
665 |
<timeout: 10 "seconds">
|
|
666 |
| serie |
|
|
667 |
serie _ ((-5 to: 10) , #(1.0e-20 1.0e-10 0.9999 1.0001 100 1000 1.0e20)) collect: [:e | e asFloat].
|
|
668 |
self checkDoublePrecisionSerie: serie forFunction: #arSinh
|
|
669 |
!
|
|
670 |
|
|
671 |
testArTanh
|
|
672 |
<timeout: 20 "seconds">
|
|
673 |
| serie |
|
|
674 |
serie _ ((-19 to: 19) collect: [:e | (e / 20) asFloat]) , ((-6 to: 6) collect: [:e | (e / 7) asFloat]) , #(1.0e-20 1.0e-10 0.99 0.9999 0.999999).
|
|
675 |
self checkDoublePrecisionSerie: serie forFunction: #arTanh
|
|
676 |
!
|
|
677 |
|
|
678 |
testArTanhDomainError
|
|
679 |
self should: [(2 asLargeFloatPrecision: 24) arTanh] raise: DomainError.
|
|
680 |
self should: [(-3 asLargeFloatPrecision: 24) arTanh] raise: DomainError.
|
|
681 |
!
|
|
682 |
|
|
683 |
testCosh
|
|
684 |
<timeout: 10 "seconds">
|
|
685 |
self checkDoublePrecisionSerie: self hyperbolicSerie forFunction: #cosh
|
|
686 |
!
|
|
687 |
|
|
688 |
testSinh
|
|
689 |
<timeout: 10 "seconds">
|
|
690 |
self checkDoublePrecisionSerie: self hyperbolicSerie forFunction: #sinh
|
|
691 |
!
|
|
692 |
|
|
693 |
testTanh
|
|
694 |
<timeout: 10 "seconds">
|
|
695 |
self checkDoublePrecisionSerie: self hyperbolicSerie forFunction: #tanh
|
|
696 |
! !
|
|
697 |
|
|
698 |
!LargeFloatTest methodsFor:'testing-trigonometry'!
|
|
699 |
|
|
700 |
inverseTrigonometricSerie
|
|
701 |
^((-20 to: 20) collect: [:e | (e / 20) asFloat]) , ((-6 to: 6) collect: [:e | (e / 7) asFloat])
|
|
702 |
!
|
|
703 |
|
|
704 |
largeTrigonometricSerie
|
|
705 |
^#(1.0e15 1.1e21 1.2e28 1.0e32 1.1e34 -1.23e51 1.345e67 1.777e151 1.211e308)
|
|
706 |
!
|
|
707 |
|
|
708 |
testArcCos
|
|
709 |
<timeout: 10 "seconds">
|
|
710 |
| badArcCos |
|
|
711 |
badArcCos _ self checkDoublePrecisionSerieVsFloat: self inverseTrigonometricSerie forFunction: #arcCos.
|
|
712 |
badArcCos isEmpty ifFalse: [Transcript cr; show: 'bad arcCos for ' , badArcCos printString]
|
|
713 |
!
|
|
714 |
|
|
715 |
testArcCosDomainError
|
|
716 |
self should: [(2 asLargeFloatPrecision: 24) arcCos] raise: DomainError.
|
|
717 |
self should: [(-3 asLargeFloatPrecision: 24) arcCos] raise: DomainError.
|
|
718 |
!
|
|
719 |
|
|
720 |
testArcSin
|
|
721 |
<timeout: 10 "seconds">
|
|
722 |
| badArcSin |
|
|
723 |
badArcSin _ self checkDoublePrecisionSerieVsFloat: self inverseTrigonometricSerie forFunction: #arcSin.
|
|
724 |
badArcSin isEmpty ifFalse: [Transcript cr; show: 'bad arcSin for ' , badArcSin printString]
|
|
725 |
!
|
|
726 |
|
|
727 |
testArcSinDomainError
|
|
728 |
self should: [(2 asLargeFloatPrecision: 24) arcSin] raise: DomainError.
|
|
729 |
self should: [(-3 asLargeFloatPrecision: 24) arcSin] raise: DomainError.
|
|
730 |
!
|
|
731 |
|
|
732 |
testArcTan
|
|
733 |
<timeout: 10 "seconds">
|
|
734 |
| badArcTan serie |
|
|
735 |
serie _ ((-50 to: 50) collect: [:e | (e / 10) asFloat]).
|
|
736 |
badArcTan _ self checkDoublePrecisionSerieVsFloat: serie forFunction: #arcTan.
|
|
737 |
badArcTan isEmpty ifFalse: [Transcript cr; show: 'bad arcTan for ' , badArcTan printString]
|
|
738 |
!
|
|
739 |
|
|
740 |
testArcTan2
|
|
741 |
<timeout: 30 "seconds">
|
|
742 |
-5 to: 5 by: 4/10 do: [:y |
|
|
743 |
| yf yd |
|
|
744 |
yf _ y asLargeFloatPrecision: Float precision.
|
|
745 |
yd _ yf asLargeFloatPrecision: Float precision * 2.
|
|
746 |
-5 to: 5 by: 4/10 do: [:x |
|
|
747 |
| xf xd |
|
|
748 |
xf _ x asLargeFloatPrecision: Float precision.
|
|
749 |
xd _ xf asLargeFloatPrecision: Float precision * 2.
|
|
750 |
self assert: ((yd arcTan: xd) asFloat - (yf arcTan: xf) asFloat) isZero]].
|
|
751 |
!
|
|
752 |
|
|
753 |
testCos
|
|
754 |
<timeout: 30 "seconds">
|
|
755 |
| badCos |
|
|
756 |
badCos _ self checkDoublePrecisionSerieVsFloat: self trigonometricSerie forFunction: #cos.
|
|
757 |
badCos isEmpty ifFalse: [Transcript cr; show: 'bad cos for angles (degrees) ' , (badCos collect: [:i | i radiansToDegrees rounded]) printString]
|
|
758 |
!
|
|
759 |
|
|
760 |
testSin
|
|
761 |
<timeout: 30 "seconds">
|
|
762 |
| badSin |
|
|
763 |
badSin _ self checkDoublePrecisionSerieVsFloat: self trigonometricSerie forFunction: #sin.
|
|
764 |
badSin isEmpty ifFalse: [Transcript cr; show: 'bad sin for angles (degrees) ' , (badSin collect: [:i | i radiansToDegrees rounded]) printString]
|
|
765 |
!
|
|
766 |
|
|
767 |
testSincos
|
|
768 |
<timeout: 30 "seconds">
|
|
769 |
self trigonometricSerie do: [:aFloat |
|
|
770 |
| x sc s c |
|
|
771 |
x _ aFloat asLargeFloatPrecision: 53.
|
|
772 |
sc _ x sincos.
|
|
773 |
s _ x sin.
|
|
774 |
c _ x cos.
|
|
775 |
self assert: sc size = 2.
|
|
776 |
|
|
777 |
self assert: sc first = s.
|
|
778 |
self assert: sc last = c]
|
|
779 |
!
|
|
780 |
|
|
781 |
testTan
|
|
782 |
<timeout: 30 "seconds">
|
|
783 |
| badTan |
|
|
784 |
badTan _ self checkDoublePrecisionSerieVsFloat: self trigonometricSerie forFunction: #tan.
|
|
785 |
badTan isEmpty ifFalse: [Transcript cr; show: 'bad tan for angles (degrees) ' , (badTan collect: [:i | i radiansToDegrees rounded]) printString]
|
|
786 |
!
|
|
787 |
|
|
788 |
testVeryLargeCos
|
|
789 |
<timeout: 10 "seconds">
|
|
790 |
self checkDoublePrecisionSerie: self largeTrigonometricSerie forFunction: #cos.
|
|
791 |
!
|
|
792 |
|
|
793 |
testVeryLargeSin
|
|
794 |
<timeout: 10 "seconds">
|
|
795 |
self checkDoublePrecisionSerie: self largeTrigonometricSerie forFunction: #sin.
|
|
796 |
!
|
|
797 |
|
|
798 |
testVeryLargeTan
|
|
799 |
<timeout: 10 "seconds">
|
|
800 |
self checkDoublePrecisionSerie: self largeTrigonometricSerie forFunction: #tan.
|
|
801 |
!
|
|
802 |
|
|
803 |
trigonometricSerie
|
|
804 |
^(-720 to: 720) collect: [:i | i asFloat degreesToRadians]
|
|
805 |
! !
|
|
806 |
|
|
807 |
|
|
808 |
|