Debian Codegraph @

regression/real.sml

raw
1 functor Test (structure Real: REAL
2 val size: int) =
3 struct
4
5 open Real
6 open Math
7
8 infix == !=
9
10 datatype z = datatype IEEEReal.float_class
11 datatype z = datatype IEEEReal.rounding_mode
12 datatype z = datatype General.order
13
14 val b2s = Bool.toString
15 val i2s = Int.toString
16 val exact = fmt StringCvt.EXACT
17
18 val s2r = valOf o fromString
19
20 val zero = s2r "0.0"
21 val one = s2r "1.0"
22 val two = s2r "2.0"
23 val nan = posInf + negInf
24
25 val halfMaxFinite = maxFinite / two
26 val halfMinNormalPos = minNormalPos / two
27
28 val reals =
29 [maxFinite,
30 halfMaxFinite,
31 s2r "1.23E3",
32 s2r "1.23E1",
33 Math.pi,
34 Math.e,
35 s2r "1.23E0",
36 s2r "1.23E~1",
37 s2r "1.23E~3",
38 minNormalPos,
39 halfMinNormalPos,
40 minPos,
41 zero]
42
43 fun for f = (List.app f reals; List.app (f o ~) reals)
44
45 val reals' =
46 [posInf,
47 negInf,
48 posInf + negInf,
49 maxFinite * s2r "2.0"]
50
51 fun for' f = (for f; List.app f reals')
52
53 val _ = print (concat ["\nTesting Real", Int.toString size, "\n"])
54
55 val _ = print "\nTesting fmt\n"
56
57 val _ =
58 for
59 (fn r =>
60 List.app (fn spec => print (concat [fmt spec r, "\n"]))
61 let
62 open StringCvt
63 in
64 [EXACT, SCI NONE, FIX NONE, GEN NONE,
65 SCI (SOME 0), FIX (SOME 0), GEN (SOME 1),
66 SCI (SOME 10), FIX (SOME 10), GEN (SOME 10)]
67 end)
68
69 val _ =
70 let
71 fun doit (r, s0, s1, s2, s6) =
72 if (fmt (StringCvt.FIX (SOME 0)) r = s0
73 andalso fmt (StringCvt.FIX (SOME 1)) r = s1
74 andalso fmt (StringCvt.FIX (SOME 2)) r = s2
75 andalso fmt (StringCvt.FIX (SOME 6)) r = s6
76 andalso fmt (StringCvt.FIX NONE) r = s6)
77 then ()
78 else print (concat ["fmt FIX bug: ", exact r, "\n",
79 "\t", fmt (StringCvt.FIX (SOME 0)) r, " ", s0, "\n",
80 "\t", fmt (StringCvt.FIX (SOME 1)) r, " ", s1, "\n",
81 "\t", fmt (StringCvt.FIX (SOME 2)) r, " ", s2, "\n",
82 "\t", fmt (StringCvt.FIX (SOME 6)) r, " ", s6, "\n",
83 "\t", fmt (StringCvt.FIX NONE) r, " ", s6, "\n"])
84 in
85 List.app
86 (fn (r, s0, s1, s2, s6) =>
87 (doit (r, s0, s1, s2, s6)
88 ; if r == zero
89 then ()
90 else doit (~r, "~"^s0, "~"^s1, "~"^s2, "~"^s6)))
91 [(s2r "0.0", "0", "0.0", "0.00", "0.000000"),
92 (s2r "1.0", "1", "1.0", "1.00", "1.000000"),
93 (s2r "1.4", "1", "1.4", "1.40", "1.400000"),
94 (s2r "1.5", "2", "1.5", "1.50", "1.500000"),
95 (s2r "2.5", "2", "2.5", "2.50", "2.500000"),
96 (s2r "1.6", "2", "1.6", "1.60", "1.600000"),
97 (s2r "3.141592653589", "3", "3.1", "3.14", "3.141593"),
98 (s2r "91827365478400.0", "91827365478400", "91827365478400.0",
99 "91827365478400.00", "91827365478400.000000")]
100 end
101
102 val _ =
103 let
104 fun doit (r, s0, s1, s2, s6) =
105 if (fmt (StringCvt.SCI (SOME 0)) r = s0
106 andalso fmt (StringCvt.SCI (SOME 1)) r = s1
107 andalso fmt (StringCvt.SCI (SOME 2)) r = s2
108 andalso fmt (StringCvt.SCI (SOME 6)) r = s6
109 andalso fmt (StringCvt.SCI NONE) r = s6)
110 then ()
111 else print (concat ["fmt SCI bug: ", exact r, "\n",
112 "\t", fmt (StringCvt.SCI (SOME 0)) r, " ", s0, "\n",
113 "\t", fmt (StringCvt.SCI (SOME 1)) r, " ", s1, "\n",
114 "\t", fmt (StringCvt.SCI (SOME 2)) r, " ", s2, "\n",
115 "\t", fmt (StringCvt.SCI (SOME 6)) r, " ", s6, "\n",
116 "\t", fmt (StringCvt.SCI NONE) r, " ", s6, "\n"])
117 in
118 List.app
119 (fn (r, s0, s1, s2, s6) =>
120 (doit (r, s0, s1, s2, s6)
121 ; if r == zero
122 then ()
123 else doit (~r, "~"^s0, "~"^s1, "~"^s2, "~"^s6)))
124 [(s2r "0.0", "0E0", "0.0E0", "0.00E0", "0.000000E0"),
125 (s2r "0.0012345678", "1E~3", "1.2E~3", "1.23E~3", "1.234568E~3"),
126 (s2r "1.0", "1E0", "1.0E0", "1.00E0", "1.000000E0"),
127 (s2r "1.4", "1E0", "1.4E0", "1.40E0", "1.400000E0"),
128 (s2r "1.5", "2E0", "1.5E0", "1.50E0", "1.500000E0"),
129 (s2r "1.6", "2E0", "1.6E0", "1.60E0", "1.600000E0"),
130 (s2r "3.141592653589", "3E0", "3.1E0", "3.14E0", "3.141593E0"),
131 (s2r "91827365478400.0", "9E13", "9.2E13", "9.18E13", "9.182737E13")]
132 end
133
134 val _ =
135 let
136 fun doit (r, s1, s2, s6, s12) =
137 if (fmt (StringCvt.GEN (SOME 1)) r = s1
138 andalso fmt (StringCvt.GEN (SOME 2)) r = s2
139 andalso fmt (StringCvt.GEN (SOME 6)) r = s6
140 andalso fmt (StringCvt.GEN (SOME 12)) r = s12
141 andalso fmt (StringCvt.GEN NONE) r = s12)
142 then ()
143 else print (concat ["fmt GEN bug: ", exact r, "\n",
144 "\t", fmt (StringCvt.GEN (SOME 1)) r, " ", s1, "\n",
145 "\t", fmt (StringCvt.GEN (SOME 2)) r, " ", s2, "\n",
146 "\t", fmt (StringCvt.GEN (SOME 6)) r, " ", s6, "\n",
147 "\t", fmt (StringCvt.GEN (SOME 12)) r, " ", s12, "\n",
148 "\t", fmt (StringCvt.GEN NONE) r, " ", s12, "\n"])
149 in
150 List.app
151 (fn (r, s1, s2, s6, s12) =>
152 (doit (r, s1, s2, s6, s12)
153 ; if r == zero
154 then ()
155 else doit (~r, "~"^s1, "~"^s2, "~"^s6, "~"^s12)))
156 [(s2r "0.0", "0", "0", "0", "0"),
157 (s2r "1.0", "1", "1", "1", "1"),
158 (s2r "1.5", "2", "1.5", "1.5", "1.5"),
159 (s2r "91827365478400.0", "9E13", "9.2E13", "9.18274E13",
160 "91827365478400")]
161 end
162
163 val _ = print "\nTesting scan\n"
164
165 val _ = for' (fn r =>
166 let
167 val r' = valOf (StringCvt.scanString scan (exact r))
168 val _ = print (concat [exact r, "\t", exact r', "\n"])
169 in
170 if r == r' orelse unordered (r, r')
171 then ()
172 else print (concat ["scan bug ", exact r, "\n"])
173 end)
174
175 val _ = print "\nTesting checkFloat\n"
176 val _ =
177 for'
178 (fn r =>
179 if (case class r of
180 INF => ((checkFloat r; false) handle Overflow => true | _ => false)
181 | NAN => ((checkFloat r; false) handle Div => true | _ => false)
182 | _ => (checkFloat r; true) handle _ => false)
183 then ()
184 else print (concat ["checkFloat bug: ", exact r, "\n"]))
185
186 val _ = print "\nTesting class, isFinite, isNan, isNormal\n"
187 val _ =
188 for'
189 (fn r =>
190 let
191 val c =
192 case class r of
193 INF => "inf"
194 | NAN => "nan"
195 | NORMAL => "normal"
196 | SUBNORMAL => "subnormal"
197 | ZERO => "zero"
198 in
199 print (concat [exact r, "\t", c, "\n",
200 "\tisFinite = ", b2s (isFinite r),
201 "\tisNan = ", b2s (isNan r),
202 "\tisNormal = ", b2s (isNormal r),
203 "\n"])
204 end)
205
206 val _ = print "\nTesting maxFinite, minPos, minNormalPos\n"
207
208 local
209 val isNormal = Real.isNormal
210 val isFinite = Real.isFinite
211 val isPositive = fn r =>
212 case class r of
213 NORMAL => r > zero
214 | SUBNORMAL => r > zero
215 | INF => r > zero
216 | _ => false
217
218 fun min (p: real -> bool): real =
219 let
220 fun loop (x: real): real =
221 let
222 val y = x / two
223 in
224 if p y
225 then loop y
226 else x
227 end
228 in
229 loop one
230 end
231 in
232 val minNormalPos = min isNormal
233 val minPos = min isPositive
234
235 val maxFinite =
236 let
237 fun up (x: real): real =
238 let
239 val y = x * two
240 in
241 if isFinite y
242 then up y
243 else x
244 end
245 fun down (x: real, y: real): real =
246 let
247 val y = y / two
248 val z = x + y
249 in
250 if isFinite z
251 then down (z, y)
252 else x
253 end
254 val z = up one
255 in
256 down (z, z)
257 end
258 end
259
260 val _ = print ((Real.toString maxFinite) ^ "\n")
261 val _ = print ((Real.toString Real.maxFinite) ^ "\n")
262 val _ = print ((Bool.toString (Real.==(Real.maxFinite, maxFinite))) ^ "\n")
263 val _ = print ((Real.toString minPos) ^ "\n")
264 val _ = print ((Real.toString Real.minPos) ^ "\n")
265 val _ = print ((Bool.toString (Real.==(Real.minPos, minPos))) ^ "\n")
266 val _ = print ((Real.toString minNormalPos) ^ "\n")
267 val _ = print ((Real.toString Real.minNormalPos) ^ "\n")
268 val _ = print ((Bool.toString (Real.==(Real.minNormalPos, minNormalPos))) ^ "\n")
269
270 val _ = print "\nTestring fromString\n"
271
272 val _ =
273 List.app
274 (fn (s1, s2) =>
275 if valOf (fromString s1) == valOf (fromString s2)
276 then ()
277 else print (concat ["fromString bug: ",
278 s1, " ", exact (valOf (fromString s1)), " ",
279 s2, " ", exact (valOf (fromString s2)), "\n"]))
280 [("12.", "12.0"),
281 ("12.E", "12.0"),
282 ("12.E+", "12.0"),
283 ("12.E-", "12.0"),
284 ("12.E2", "12.0"),
285 ("12.E+2", "12.0"),
286 ("12.E-2", "12.0"),
287 ("12E+", "12.0"),
288 ("12E-", "12.0"),
289 ("0", "0.0"),
290 ("156", "156.0"),
291 ("+156", "156.0"),
292 ("~156", "~156.0"),
293 ("-156", "~156.0"),
294 ("156.25", "156.25"),
295 ("+156.25", "156.25"),
296 ("~156.25", "~156.25"),
297 ("-156.25", "~156.25"),
298 (".25", "0.25"),
299 ("+.25", "0.25"),
300 ("~.25", "~0.25"),
301 ("-.25", "~0.25"),
302 ("156E024", "156E024"),
303 ("+156E024", "156E024"),
304 ("~156E024", "~156E024"),
305 ("-156E024", "~156E024"),
306 ("156.25E024", "156.25E024"),
307 ("+156.25E024", "156.25E024"),
308 ("~156.25E024", "~156.25E024"),
309 ("-156.25E024", "~156.25E024"),
310 (".25E024", "0.25E024"),
311 ("+.25E024", "0.25E024"),
312 ("~.25E024", "~0.25E024"),
313 ("-.25E024", "~0.25E024"),
314 ("156E+024", "156E024"),
315 ("+156E+024", "156E024"),
316 ("~156E+024", "~156E024"),
317 ("-156E+024", "~156E024"),
318 ("156.25E+024", "156.25E024"),
319 ("+156.25E+024", "156.25E024"),
320 ("~156.25E+024", "~156.25E024"),
321 ("-156.25E+024", "~156.25E024"),
322 (".25E+024", "0.25E024"),
323 ("+.25E+024", "0.25E024"),
324 ("~.25E+024", "~0.25E024"),
325 ("-.25E+024", "~0.25E024"),
326 ("156E~024", "156E~024"),
327 ("+156E~024", "156E~024"),
328 ("~156E~024", "~156E~024"),
329 ("-156E~024", "~156E~024"),
330 ("156.25E~024", "156.25E~024"),
331 ("+156.25E~024", "156.25E~024"),
332 ("~156.25E~024", "~156.25E~024"),
333 ("-156.25E~024", "~156.25E~024"),
334 (".25E~024", "0.25E~024"),
335 ("+.25E~024", "0.25E~024"),
336 ("~.25E~024", "~0.25E~024"),
337 ("-.25E~024", "~0.25E~024"),
338 ("156E-024", "156E~024"),
339 ("+156E-024", "156E~024"),
340 ("~156E-024", "~156E~024"),
341 ("-156E-024", "~156E~024"),
342 ("156.25E-024", "156.25E~024"),
343 ("+156.25E-024", "156.25E~024"),
344 ("~156.25E-024", "~156.25E~024"),
345 ("-156.25E-024", "~156.25E~024"),
346 (".25E-024", "0.25E~024"),
347 ("+.25E-024", "0.25E~024"),
348 ("~.25E-024", "~0.25E~024"),
349 ("-.25E-024", "~0.25E~024"),
350 ("156e024", "156E024"),
351 ("+156e024", "156E024"),
352 ("~156e024", "~156E024"),
353 ("-156e024", "~156E024"),
354 ("156.25e024", "156.25E024"),
355 ("+156.25e024", "156.25E024"),
356 ("~156.25e024", "~156.25E024"),
357 ("-156.25e024", "~156.25E024"),
358 (".25e024", "0.25E024"),
359 ("+.25e024", "0.25E024"),
360 ("~.25e024", "~0.25E024"),
361 ("-.25e024", "~0.25E024"),
362 ("156e+024", "156E024"),
363 ("+156e+024", "156E024"),
364 ("~156e+024", "~156E024"),
365 ("-156e+024", "~156E024"),
366 ("156.25e+024", "156.25E024"),
367 ("+156.25e+024", "156.25E024"),
368 ("~156.25e+024", "~156.25E024"),
369 ("-156.25e+024", "~156.25E024"),
370 (".25e+024", "0.25E024"),
371 ("+.25e+024", "0.25E024"),
372 ("~.25e+024", "~0.25E024"),
373 ("-.25e+024", "~0.25E024"),
374 ("156e~024", "156E~024"),
375 ("+156e~024", "156E~024"),
376 ("~156e~024", "~156E~024"),
377 ("-156e~024", "~156E~024"),
378 ("156.25e~024", "156.25E~024"),
379 ("+156.25e~024", "156.25E~024"),
380 ("~156.25e~024", "~156.25E~024"),
381 ("-156.25e~024", "~156.25E~024"),
382 (".25e~024", "0.25E~024"),
383 ("+.25e~024", "0.25E~024"),
384 ("~.25e~024", "~0.25E~024"),
385 ("-.25e~024", "~0.25E~024"),
386 ("156e-024", "156E~024"),
387 ("+156e-024", "156E~024"),
388 ("~156e-024", "~156E~024"),
389 ("-156e-024", "~156E~024"),
390 ("156.25e-024", "156.25E~024"),
391 ("+156.25e-024", "156.25E~024"),
392 ("~156.25e-024", "~156.25E~024"),
393 ("-156.25e-024", "~156.25E~024"),
394 (".25e-024", "0.25E~024"),
395 ("+.25e-024", "0.25E~024"),
396 ("~.25e-024", "~0.25E~024"),
397 ("-.25e-024", "~0.25E~024")]
398
399
400 val _ = print "\nTesting {from,to}Decimal\n"
401
402 val _ =
403 List.app (fn r =>
404 let
405 val da = valOf (IEEEReal.fromString r)
406 val s1 = IEEEReal.toString da
407 val x = valOf (fromDecimal da)
408 val s2 = exact x
409 val da' = toDecimal x
410 val b = Bool.toString (da = da')
411 in
412 print (concat [s1, " ", s2, " ", b, "\n"])
413 end)
414 ["inf", "+inF", "~iNf", "-Inf",
415 "infinity", "+infinity", "~infinity", "-infinity",
416 "nan", "+naN", "~nAn", "-Nan",
417 "0", "0.0", "0.0E0", "~0",
418 "15",
419 "1.5",
420 "~1.5e+1",
421 "15.0",
422 ".15e~2",
423 ".15e-2",
424 "000.0015e0",
425 "1.2E999",
426 "~1.2E999",
427 "1E~999",
428 "~1E~999",
429 "1E12345678901234567890"]
430
431 val _ = print "\nTesting {from,to}LargeInt\n"
432 val _ =
433 for
434 (fn r =>
435 let
436 val i = toLargeInt IEEEReal.TO_NEGINF r
437 val r' = fromLargeInt i
438 val _ = print (concat [exact r,
439 "\t", LargeInt.toString i,
440 "\t", exact r',
441 "\n"])
442 in
443 if r' == realFloor r
444 then ()
445 else print (concat ["{from,to}LargeInt bug: ", exact r, "\n"])
446 end)
447
448 val roundingModes =
449 [(TO_NEAREST, "nearest"),
450 (TO_NEGINF, "neginf"),
451 (TO_POSINF, "posinf"),
452 (TO_ZERO, "zero")]
453
454 val _ =
455 let
456 fun doit (x, mode, name) =
457 let
458 val i = toLargeInt mode x
459 in
460 print (concat [name, "\t", exact x, "\t", LargeInt.toString i, "\n"])
461 end
462 in
463 List.app
464 (fn (mode, name) =>
465 List.app (fn s =>
466 let
467 val x = s2r s
468 in
469 doit (x, mode, name)
470 ; doit (~ x, mode, name)
471 ; doit (s2r "1E12" + x, mode, name)
472 ; doit (s2r "~1E12" + x, mode, name)
473 end)
474 ["0.0", "0.25", "0.5", "0.75", "1.0", "1.25", "1.5", "1.75", "2.0",
475 "2.5", "3.0"])
476 roundingModes
477 end
478
479 val _ = print "\nTesting fromInt\n"
480
481 val _ =
482 for
483 (fn r =>
484 case SOME (round r) handle Overflow => NONE of
485 NONE => ()
486 | SOME i =>
487 let
488 val r = fromInt i
489 in
490 if r == fromInt (round r)
491 then ()
492 else print (concat ["fromInt bug: ", exact r, "\n"])
493 end)
494
495 val _ = print "\nTesting toInt\n"
496
497 val _ =
498 for
499 (fn r =>
500 List.app
501 (fn (mode, name) =>
502 case SOME (toInt mode r) handle Overflow => NONE of
503 NONE => ()
504 | SOME i => if i = LargeInt.toInt (toLargeInt mode r)
505 then ()
506 else print (concat ["toInt ", name, " bug: ", exact r, "\n"]))
507 roundingModes)
508
509 val _ = print "\nTesting ceil,floor,round,trunc\n"
510
511 val _ =
512 for
513 (fn r =>
514 List.app
515 (fn (mode, f, name) =>
516 case SOME (toInt mode r) handle Overflow => NONE of
517 NONE => ()
518 | SOME i => if i = f r
519 then ()
520 else print (concat [name, " bug: ", exact r, "\n"]))
521 [(TO_NEAREST, round, "round"),
522 (TO_NEGINF, floor, "floor"),
523 (TO_POSINF, ceil, "ceil"),
524 (TO_ZERO, trunc, "trunc")])
525
526 val _ = print "\nTesting copySign, sameSign, sign, signBit\n"
527 val _ =
528 for'
529 (fn r1 =>
530 (for'
531 (fn r2 =>
532 if unordered (r1, r2)
533 orelse (if false
534 then print (concat [b2s (signBit r1), "\t",
535 b2s (signBit r2), "\t",
536 i2s (sign r1), "\t",
537 b2s (sameSign (r1, r2)), "\t",
538 exact (copySign (r1, r2)), "\n"])
539 else ()
540 ; (signBit r1 = Int.< (sign r1, 0)
541 orelse r1 == zero)
542 andalso (sameSign (r1, r2)) = (signBit r1 = signBit r2)
543 andalso sameSign (r2, copySign (r1, r2)))
544 then ()
545 else print (concat ["copySign,sameSign,sign,signBit bug: ", exact r1, " ", exact r2, "\n"]))))
546
547 val _ = print "\nTesting max, min\n"
548
549 val _ =
550 for'
551 (fn r1 =>
552 for'
553 (fn r2 =>
554 let
555 val max = max (r1, r2)
556 val min = min (r1, r2)
557 in
558 if (isNan r1 orelse (r1 <= max andalso min <= r1))
559 andalso (isNan r2 orelse (r2 <= max andalso min <= r2))
560 andalso (r1 == max orelse r2 == max
561 orelse (isNan r1 andalso isNan r2))
562 andalso (r1 == min orelse r2 == min
563 orelse (isNan r1 andalso isNan r2))
564 then ()
565 else print (concat ["max,min bug: ", exact r1, " ", exact r2, "\n"])
566 end))
567
568 val _ = print "\nTesting Real.{~,*,+,-,/,nextAfter,rem}\n"
569
570 val _ =
571 for'
572 (fn r =>
573 List.app
574 (fn (name, f, except) =>
575 if List.exists (fn r' => r == r') except
576 then ()
577 else
578 print (concat [name, " (", exact r, ") = ",
579 exact (f r), "\n"]))
580 [("~", ~, [])
581 ])
582
583 val _ =
584 for'
585 (fn r1 =>
586 for'
587 (fn r2 =>
588 List.app
589 (fn (name, f, except) =>
590 if List.exists (fn (r1', r2') => r1 == r1' andalso r2 == r2') except
591 then ()
592 else
593 print (concat [name, " (", exact r1, ", ", exact r2, ") = ",
594 exact (f (r1, r2)), "\n"]))
595 [("*", op *, []),
596 ("+", op +, []),
597 ("-", op -, []),
598 ("/", op /, []),
599 ("nextAfter", nextAfter, []),
600 ("rem", rem, [])
601 ]))
602
603 val _ =
604 List.app
605 (fn (r1, r2, name, f, chk) =>
606 if chk (f (r1, r2))
607 then ()
608 else print (concat [name, "(", exact r1, ",", exact r2, ") bug: ", exact (f (r1, r2)), "\n"]))
609 [(posInf, posInf, "+", op +, fn x => x == posInf),
610 (negInf, negInf, "+", op +, fn x => x == negInf),
611 (posInf, negInf, "-", op -, fn x => x == posInf),
612 (negInf, posInf, "-", op -, fn x => x == negInf),
613 (nan, one, "+", op +, isNan),
614 (nan, one, "-", op -, isNan),
615 (nan, one, "*", op *, isNan),
616 (nan, one, "/", op /, isNan),
617 (posInf, negInf, "+", op +, isNan),
618 (negInf, posInf, "+", op +, isNan),
619 (posInf, posInf, "-", op -, isNan),
620 (negInf, negInf, "-", op -, isNan)]
621
622 val _ = print "\nTesting Real.Math.{acos,asin,atan,cos,cosh,exp,ln,log10,sin,sinh,sqrt,tan,tanh,atan2,pow}\n"
623
624 val _ =
625 for' (fn r =>
626 List.app
627 (fn (name, f, except) =>
628 if List.exists (fn r' => r == r') except
629 then ()
630 else
631 print (concat [name, " (", exact r, ") = ",
632 fmt (StringCvt.GEN (SOME 10)) (f r), "\n"]))
633 let
634 open Real.Math
635 in
636 [("acos", acos, []),
637 ("asin", asin, []),
638 ("atan", atan, []),
639 ("cos", cos, []),
640 ("cosh", cosh, []),
641 ("exp", exp, []),
642 ("ln", ln, []),
643 ("log10", log10, []),
644 ("sin", sin, []),
645 ("sinh", sinh, []),
646 ("sqrt", sqrt, []),
647 ("tan", tan, []),
648 ("tanh", tanh, [])]
649 end)
650
651 val _ =
652 for'
653 (fn r1 =>
654 for'
655 (fn r2 =>
656 List.app
657 (fn (name, f, except) =>
658 if List.exists (fn (r1', r2') => r1 == r1' andalso r2 == r2') except
659 then ()
660 else
661 print (concat [name, " (", exact r1, ", ", exact r2, ") = ",
662 fmt (StringCvt.GEN (SOME 10)) (f (r1, r2)), "\n"]))
663 let
664 open Real.Math
665 in
666 [("atan2", atan2, []),
667 ("pow", pow, [])]
668 end))
669
670 val _ = print "\nTesting *+, *-\n"
671 val fmaDiffs = ref false
672 val _ =
673 for
674 (fn r1 =>
675 for
676 (fn r2 =>
677 for
678 (fn r3 =>
679 let
680 val fma = *+ (r1, r2, r3)
681 val std = r1 * r2 + r3
682 in
683 if fma == std
684 then ()
685 else (print o concat)
686 [(if !fmaDiffs then
687 ""
688 else
689 let
690 val _ = fmaDiffs := true
691 in
692 concat["SOME *+ AND STANDARD RESULTS DIFFER\n",
693 "(r1, r2, r3): *+(r1, r2, r3)\t(r1 * r2 + r3)\n",
694 "--------------------------------------------\n"]
695 end),
696 "(",
697 exact r1,
698 ", ",
699 exact r2,
700 ", ",
701 exact r3,
702 "): ",
703 exact fma,
704 "\t",
705 exact std,
706 "\n"]
707 end)))
708
709 val _ = print "\nTesting Real.{realCeil,realFloor,realTrunc}\n"
710 val _ =
711 for
712 (fn r =>
713 let
714 val ceil = realCeil r
715 val floor = realFloor r
716 val trunc = realTrunc r
717 val _ = print (concat [exact r, " ",
718 exact ceil, " ",
719 exact floor, " ",
720 exact trunc, "\n"])
721 in
722 if floor <= r
723 andalso r <= ceil
724 andalso abs trunc <= abs r
725 then ()
726 else print (concat ["Real.{realCeil,realFloor,realTrunc} bug ", exact r, "\n"])
727 end)
728
729 val _ = print "\nTesting Real.{<,<=,>,>=,==,!=,?=,unordered}\n"
730
731 val _ =
732 for
733 (fn r1 =>
734 for
735 (fn r2 =>
736 let
737 val _ =
738 List.app
739 (fn (f, name) =>
740 print (concat [name, " (", exact r1, ", ", exact r2, ") = ",
741 b2s (f (r1, r2)), "\n"]))
742 [(Real.<, "<"),
743 (Real.>, ">"),
744 (Real.==, "=="),
745 (Real.?=, "?=")]
746 in
747 if unordered (r1, r2) = (isNan r1 orelse isNan r2)
748 andalso (r1 != r2) = not (r1 == r2)
749 andalso if unordered (r1, r2)
750 then (false = (r1 <= r2)
751 andalso false = (r1 < r2)
752 andalso false = (r1 >= r2)
753 andalso false = (r1 > r2)
754 andalso false = (r1 == r2)
755 andalso if isNan r1 andalso isNan r2
756 then true = ?= (r1, r2)
757 else true)
758 else ((r1 < r2) = not (r1 >= r2)
759 andalso (r1 > r2) = not (r1 <= r2))
760 then ()
761 else print (concat ["Real.{<,<=,>,>=,==,!=,?=,unordered} bug: ", exact r1, " ", exact r2, "\n"])
762 end))
763
764 val _ = print "\nTesting compare, compareReal\n"
765
766 val _ =
767 for
768 (fn r =>
769 for
770 (fn r' =>
771 let
772 val c =
773 case SOME (compare (r, r')) handle IEEEReal.Unordered => NONE of
774 NONE => "Unordered"
775 | SOME z =>
776 case z of
777 EQUAL => "EQUAL"
778 | GREATER => "GREATER"
779 | LESS => "LESS"
780 datatype z = datatype IEEEReal.real_order
781 val cr =
782 case compareReal (r, r') of
783 EQUAL => "EQUAL"
784 | GREATER => "GREATER"
785 | LESS => "LESS"
786 | UNORDERED => "UNORDERED"
787 val _ = print (concat ["compare (", exact r, ", ", exact r', ") = ", c, "\n"])
788 val _ = print (concat ["compareReal (", exact r, ", ", exact r', ") = ", cr, "\n"])
789 in
790 if compareReal (r, r') = (case compareReal (r', r) of
791 EQUAL => EQUAL
792 | GREATER => LESS
793 | LESS => GREATER
794 | UNORDERED => UNORDERED)
795 then ()
796 else print (concat ["compareReal bug: ", exact r, " ", exact r', "\n"])
797 end))
798
799 val _ = print "\nTesting abs\n"
800
801 val _ = for (fn r =>
802 if abs r == abs (~ r)
803 then ()
804 else print (concat ["abs bug: ", exact r, "\n"]))
805
806 val _ = print "\nTesting {from,to}ManExp\n"
807
808 val _ =
809 for
810 (fn x =>
811 if List.exists (fn y => x == y) [halfMinNormalPos, minPos,
812 ~halfMinNormalPos, ~minPos]
813 then ()
814 else
815 let
816 val {exp, man} = toManExp x
817 val _ =
818 if true
819 then
820 print (concat [exact x, " = ", exact man, " * 2^", i2s exp,
821 "\n"])
822 else ()
823 val x' = fromManExp {exp = exp, man = man}
824 val _ =
825 if true
826 then
827 print (concat ["\t = ", exact x', "\n"])
828 else ()
829 in
830 if x == x'
831 then ()
832 else print (concat ["{from,to}ManExp bug: ", exact x, "\n"])
833 end)
834
835 val _ = print "\nTesting split\n"
836
837 val _ =
838 for (fn r =>
839 let
840 val {whole, frac} = split r
841 val _ =
842 if false
843 then
844 print (concat ["split ", exact r, " = {whole = ",
845 exact whole, ", frac = ", exact frac, "}\n",
846 "realMod ", exact whole, " = ",
847 exact (realMod whole), "\t",
848 b2s (sameSign (r, whole)), "\t",
849 b2s (sameSign (r, frac)), "\n"])
850 else ()
851 in
852 if realMod r == frac
853 andalso realMod whole == zero
854 andalso abs frac < one
855 andalso sameSign (r, whole)
856 andalso sameSign (r, frac)
857 andalso (case class r of
858 INF => whole == r andalso frac == zero
859 | NAN => isNan whole andalso isNan frac
860 | _ => r == whole + frac)
861 then ()
862 else print (concat ["split bug: ", exact r, "\n"])
863 end)
864
865 val _ = print "\nTesting {from,to}Large\n"
866
867 val _ =
868 for
869 (fn r =>
870 if r == fromLarge TO_NEAREST (toLarge r)
871 then ()
872 else print (concat ["{from,to}Large bug: ", exact r, "\n"]))
873
874 end
875
876 structure Z = Test (structure Real = Real32
877 val size = 32)
878 structure Z = Test (structure Real = Real64
879 val size = 64)