File: real.sml

package info (click to toggle)
mlton 20210117%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 58,464 kB
  • sloc: ansic: 27,682; sh: 4,455; asm: 3,569; lisp: 2,879; makefile: 2,347; perl: 1,169; python: 191; pascal: 68; javascript: 7
file content (879 lines) | stat: -rw-r--r-- 26,820 bytes parent folder | download | duplicates (3)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
functor Test (structure Real: REAL
              val size: int) =
struct

open Real
open Math

infix == !=

datatype z = datatype IEEEReal.float_class
datatype z = datatype IEEEReal.rounding_mode
datatype z = datatype General.order
   
val b2s = Bool.toString
val i2s = Int.toString
val exact = fmt StringCvt.EXACT

val s2r = valOf o fromString
   
val zero = s2r "0.0"
val one = s2r "1.0"
val two = s2r "2.0"
val nan = posInf + negInf

val halfMaxFinite = maxFinite / two
val halfMinNormalPos = minNormalPos / two
   
val reals =
   [maxFinite,
    halfMaxFinite,
    s2r "1.23E3",
    s2r "1.23E1",
    Math.pi,
    Math.e,
    s2r "1.23E0",
    s2r "1.23E~1",
    s2r "1.23E~3",
    minNormalPos,
    halfMinNormalPos,
    minPos,
    zero]

fun for f = (List.app f reals; List.app (f o ~) reals)

val reals' =
   [posInf,
    negInf,
    posInf + negInf,
    maxFinite * s2r "2.0"]

fun for' f = (for f; List.app f reals')

val _ = print (concat ["\nTesting Real", Int.toString size, "\n"])

val _ = print "\nTesting fmt\n"

val _ =
   for
   (fn r =>
    List.app (fn spec => print (concat [fmt spec r, "\n"]))
    let
       open StringCvt
    in
       [EXACT, SCI NONE, FIX NONE, GEN NONE,
        SCI (SOME 0), FIX (SOME 0), GEN (SOME 1),
        SCI (SOME 10), FIX (SOME 10), GEN (SOME 10)]
    end)

val _ =
   let
      fun doit (r, s0, s1, s2, s6) =
         if (fmt (StringCvt.FIX (SOME 0)) r = s0
             andalso fmt (StringCvt.FIX (SOME 1)) r = s1
             andalso fmt (StringCvt.FIX (SOME 2)) r = s2
             andalso fmt (StringCvt.FIX (SOME 6)) r = s6
             andalso fmt (StringCvt.FIX NONE) r = s6)
            then ()
         else print (concat ["fmt FIX bug: ", exact r, "\n",
                             "\t", fmt (StringCvt.FIX (SOME 0)) r, "  ", s0, "\n",
                             "\t", fmt (StringCvt.FIX (SOME 1)) r, "  ", s1, "\n",
                             "\t", fmt (StringCvt.FIX (SOME 2)) r, "  ", s2, "\n",
                             "\t", fmt (StringCvt.FIX (SOME 6)) r, "  ", s6, "\n",
                             "\t", fmt (StringCvt.FIX NONE) r, "  ", s6, "\n"])
   in
      List.app
      (fn (r, s0, s1, s2, s6) =>
       (doit (r, s0, s1, s2, s6)
        ; if r == zero
             then ()
          else doit (~r, "~"^s0, "~"^s1, "~"^s2, "~"^s6)))
      [(s2r "0.0", "0", "0.0", "0.00", "0.000000"),
       (s2r "1.0", "1", "1.0", "1.00", "1.000000"),
       (s2r "1.4", "1", "1.4", "1.40", "1.400000"),
       (s2r "1.5", "2", "1.5", "1.50", "1.500000"),
       (s2r "2.5", "2", "2.5", "2.50", "2.500000"),
       (s2r "1.6", "2", "1.6", "1.60", "1.600000"),
       (s2r "3.141592653589", "3", "3.1", "3.14", "3.141593"),
       (s2r "91827365478400.0", "91827365478400", "91827365478400.0",
        "91827365478400.00", "91827365478400.000000")]
   end

val _ =
   let
      fun doit (r, s0, s1, s2, s6) =
         if (fmt (StringCvt.SCI (SOME 0)) r = s0
             andalso fmt (StringCvt.SCI (SOME 1)) r = s1
             andalso fmt (StringCvt.SCI (SOME 2)) r = s2
             andalso fmt (StringCvt.SCI (SOME 6)) r = s6
             andalso fmt (StringCvt.SCI NONE) r = s6)
            then ()
         else print (concat ["fmt SCI bug: ", exact r, "\n",
                             "\t", fmt (StringCvt.SCI (SOME 0)) r, "  ", s0, "\n",
                             "\t", fmt (StringCvt.SCI (SOME 1)) r, "  ", s1, "\n",
                             "\t", fmt (StringCvt.SCI (SOME 2)) r, "  ", s2, "\n",
                             "\t", fmt (StringCvt.SCI (SOME 6)) r, "  ", s6, "\n",
                             "\t", fmt (StringCvt.SCI NONE) r, "  ", s6, "\n"])
   in
      List.app
      (fn (r, s0, s1, s2, s6) =>
       (doit (r, s0, s1, s2, s6)
        ; if r == zero
             then ()
          else doit (~r, "~"^s0, "~"^s1, "~"^s2, "~"^s6)))
      [(s2r "0.0", "0E0", "0.0E0", "0.00E0", "0.000000E0"),
       (s2r "0.0012345678", "1E~3", "1.2E~3", "1.23E~3", "1.234568E~3"),
       (s2r "1.0", "1E0", "1.0E0", "1.00E0", "1.000000E0"),
       (s2r "1.4", "1E0", "1.4E0", "1.40E0", "1.400000E0"),
       (s2r "1.5", "2E0", "1.5E0", "1.50E0", "1.500000E0"),
       (s2r "1.6", "2E0", "1.6E0", "1.60E0", "1.600000E0"),
       (s2r "3.141592653589", "3E0", "3.1E0", "3.14E0", "3.141593E0"),
       (s2r "91827365478400.0", "9E13", "9.2E13", "9.18E13", "9.182737E13")]
   end

val _ =
   let
      fun doit (r, s1, s2, s6, s12) =
         if (fmt (StringCvt.GEN (SOME 1)) r = s1
             andalso fmt (StringCvt.GEN (SOME 2)) r = s2
             andalso fmt (StringCvt.GEN (SOME 6)) r = s6
             andalso fmt (StringCvt.GEN (SOME 12)) r = s12
             andalso fmt (StringCvt.GEN NONE) r = s12)
            then ()
         else print (concat ["fmt GEN bug: ", exact r, "\n",
                             "\t", fmt (StringCvt.GEN (SOME 1)) r, "  ", s1, "\n",
                             "\t", fmt (StringCvt.GEN (SOME 2)) r, "  ", s2, "\n",
                             "\t", fmt (StringCvt.GEN (SOME 6)) r, "  ", s6, "\n",
                             "\t", fmt (StringCvt.GEN (SOME 12)) r, "  ", s12, "\n",
                             "\t", fmt (StringCvt.GEN NONE) r, "  ", s12, "\n"])
   in
      List.app
      (fn (r, s1, s2, s6, s12) =>
       (doit (r, s1, s2, s6, s12)
        ; if r == zero
             then ()
          else doit (~r, "~"^s1, "~"^s2, "~"^s6, "~"^s12)))
      [(s2r "0.0",               "0", "0",     "0", "0"),
       (s2r "1.0",              "1", "1",  "1", "1"),
       (s2r "1.5",              "2", "1.5",  "1.5", "1.5"),
       (s2r "91827365478400.0", "9E13", "9.2E13",  "9.18274E13", 
        "91827365478400")]
   end

val _ = print "\nTesting scan\n"

val _ = for' (fn r =>
              let
                 val r' = valOf (StringCvt.scanString scan (exact r))
                 val _ = print (concat [exact r, "\t", exact r', "\n"])
              in
                 if r == r' orelse unordered (r, r')
                    then ()
                 else print (concat ["scan bug ", exact r, "\n"])
              end)

val _ = print "\nTesting checkFloat\n"
val _ =
   for'
   (fn r =>
    if (case class r of
           INF => ((checkFloat r; false) handle Overflow => true | _ => false)
         | NAN => ((checkFloat r; false) handle Div => true | _ => false)
         | _ => (checkFloat r; true) handle _ => false)
       then ()
    else print (concat ["checkFloat bug: ", exact r, "\n"]))
   
val _ = print "\nTesting class, isFinite, isNan, isNormal\n"
val _ =
   for'
   (fn r =>
    let
       val c = 
          case class r of
             INF => "inf"
           | NAN => "nan"
           | NORMAL => "normal"
           | SUBNORMAL => "subnormal"
           | ZERO => "zero"
    in
       print (concat [exact r, "\t", c, "\n",
                      "\tisFinite = ", b2s (isFinite r),
                      "\tisNan = ", b2s (isNan r),
                      "\tisNormal = ", b2s (isNormal r),
                      "\n"])
    end)

val _ = print "\nTesting maxFinite, minPos, minNormalPos\n"

local
  val isNormal = Real.isNormal
  val isFinite = Real.isFinite
  val isPositive = fn r =>
     case class r of
        NORMAL => r > zero
      | SUBNORMAL => r > zero
      | INF => r > zero
      | _ => false

  fun min (p: real -> bool): real =
    let
      fun loop (x: real): real =
        let
          val y = x / two
        in
          if p y
            then loop y
            else x
        end
    in
       loop one
    end
in
  val minNormalPos = min isNormal
  val minPos = min isPositive
    
  val maxFinite =
    let
      fun up (x: real): real =
        let
          val y = x * two
        in
          if isFinite y
            then up y
            else x
        end
      fun down (x: real, y: real): real =
                  let
                    val y = y / two
                    val z = x + y
                  in
                    if isFinite z
                      then down (z, y)
                      else x
                  end
      val z = up one
    in
       down (z, z)
    end
end

val _ = print ((Real.toString maxFinite) ^ "\n")
val _ = print ((Real.toString Real.maxFinite) ^ "\n")
val _ = print ((Bool.toString (Real.==(Real.maxFinite, maxFinite))) ^ "\n")
val _ = print ((Real.toString minPos) ^ "\n")
val _ = print ((Real.toString Real.minPos) ^ "\n")
val _ = print ((Bool.toString (Real.==(Real.minPos, minPos))) ^ "\n")
val _ = print ((Real.toString minNormalPos) ^ "\n")
val _ = print ((Real.toString Real.minNormalPos) ^ "\n")
val _ = print ((Bool.toString (Real.==(Real.minNormalPos, minNormalPos))) ^ "\n")

val _ = print "\nTestring fromString\n"

val _ =
   List.app
   (fn (s1, s2) =>
    if valOf (fromString s1) == valOf (fromString s2)
       then ()
    else print (concat ["fromString bug: ",
                        s1, " ", exact (valOf (fromString s1)), " ",
                        s2, " ", exact (valOf (fromString s2)), "\n"]))
   [("12.", "12.0"),
    ("12.E", "12.0"),
    ("12.E+", "12.0"),
    ("12.E-", "12.0"),
    ("12.E2", "12.0"),
    ("12.E+2", "12.0"),
    ("12.E-2", "12.0"),
    ("12E+", "12.0"),
    ("12E-", "12.0"),
    ("0", "0.0"),
    ("156", "156.0"),
    ("+156", "156.0"), 
    ("~156", "~156.0"), 
    ("-156", "~156.0"), 
    ("156.25", "156.25"), 
    ("+156.25", "156.25"), 
    ("~156.25", "~156.25"), 
    ("-156.25", "~156.25"),
    (".25", "0.25"),
    ("+.25", "0.25"),
    ("~.25", "~0.25"),
    ("-.25", "~0.25"),
    ("156E024", "156E024"),
    ("+156E024", "156E024"),
    ("~156E024", "~156E024"),
    ("-156E024", "~156E024"),
    ("156.25E024", "156.25E024"),
    ("+156.25E024", "156.25E024"),
    ("~156.25E024", "~156.25E024"),
    ("-156.25E024", "~156.25E024"),
    (".25E024", "0.25E024"),
    ("+.25E024", "0.25E024"),
    ("~.25E024", "~0.25E024"),
    ("-.25E024", "~0.25E024"),
    ("156E+024", "156E024"),
    ("+156E+024", "156E024"),
    ("~156E+024", "~156E024"),
    ("-156E+024", "~156E024"),
    ("156.25E+024", "156.25E024"),
    ("+156.25E+024", "156.25E024"),
    ("~156.25E+024", "~156.25E024"),
    ("-156.25E+024", "~156.25E024"),
    (".25E+024", "0.25E024"),
    ("+.25E+024", "0.25E024"),
    ("~.25E+024", "~0.25E024"),
    ("-.25E+024", "~0.25E024"),
    ("156E~024", "156E~024"),
    ("+156E~024", "156E~024"),
    ("~156E~024", "~156E~024"),
    ("-156E~024", "~156E~024"),
    ("156.25E~024", "156.25E~024"),
    ("+156.25E~024", "156.25E~024"),
    ("~156.25E~024", "~156.25E~024"),
    ("-156.25E~024", "~156.25E~024"),
    (".25E~024", "0.25E~024"),
    ("+.25E~024", "0.25E~024"),
    ("~.25E~024", "~0.25E~024"),
    ("-.25E~024", "~0.25E~024"),
    ("156E-024", "156E~024"),
    ("+156E-024", "156E~024"),
    ("~156E-024", "~156E~024"),
    ("-156E-024", "~156E~024"),
    ("156.25E-024", "156.25E~024"),
    ("+156.25E-024", "156.25E~024"),
    ("~156.25E-024", "~156.25E~024"),
    ("-156.25E-024", "~156.25E~024"),
    (".25E-024", "0.25E~024"),
    ("+.25E-024", "0.25E~024"),
    ("~.25E-024", "~0.25E~024"),
    ("-.25E-024", "~0.25E~024"),
    ("156e024", "156E024"),
    ("+156e024", "156E024"),
    ("~156e024", "~156E024"),
    ("-156e024", "~156E024"),
    ("156.25e024", "156.25E024"),
    ("+156.25e024", "156.25E024"),
    ("~156.25e024", "~156.25E024"),
    ("-156.25e024", "~156.25E024"),
    (".25e024", "0.25E024"),
    ("+.25e024", "0.25E024"),
    ("~.25e024", "~0.25E024"),
    ("-.25e024", "~0.25E024"),
    ("156e+024", "156E024"),
    ("+156e+024", "156E024"),
    ("~156e+024", "~156E024"),
    ("-156e+024", "~156E024"),
    ("156.25e+024", "156.25E024"),
    ("+156.25e+024", "156.25E024"),
    ("~156.25e+024", "~156.25E024"),
    ("-156.25e+024", "~156.25E024"),
    (".25e+024", "0.25E024"),
    ("+.25e+024", "0.25E024"),
    ("~.25e+024", "~0.25E024"),
    ("-.25e+024", "~0.25E024"),
    ("156e~024", "156E~024"),
    ("+156e~024", "156E~024"),
    ("~156e~024", "~156E~024"),
    ("-156e~024", "~156E~024"),
    ("156.25e~024", "156.25E~024"),
    ("+156.25e~024", "156.25E~024"),
    ("~156.25e~024", "~156.25E~024"),
    ("-156.25e~024", "~156.25E~024"),
    (".25e~024", "0.25E~024"),
    ("+.25e~024", "0.25E~024"),
    ("~.25e~024", "~0.25E~024"),
    ("-.25e~024", "~0.25E~024"),
    ("156e-024", "156E~024"),
    ("+156e-024", "156E~024"),
    ("~156e-024", "~156E~024"),
    ("-156e-024", "~156E~024"),
    ("156.25e-024", "156.25E~024"),
    ("+156.25e-024", "156.25E~024"),
    ("~156.25e-024", "~156.25E~024"),
    ("-156.25e-024", "~156.25E~024"),
    (".25e-024", "0.25E~024"),
    ("+.25e-024", "0.25E~024"),
    ("~.25e-024", "~0.25E~024"),
    ("-.25e-024", "~0.25E~024")]


val _ = print "\nTesting {from,to}Decimal\n"
   
val _ =
   List.app (fn r =>
             let
                val da = valOf (IEEEReal.fromString r)
                val s1 = IEEEReal.toString da
                val x = valOf (fromDecimal da)
                val s2 = exact x
                val da' = toDecimal x
                val b = Bool.toString (da = da')
             in
                print (concat [s1, " ", s2, " ", b, "\n"])
             end)
   ["inf", "+inF", "~iNf", "-Inf",
    "infinity", "+infinity", "~infinity", "-infinity",
    "nan", "+naN", "~nAn", "-Nan",
    "0", "0.0", "0.0E0", "~0",
    "15",
    "1.5",
    "~1.5e+1",
    "15.0",
    ".15e~2",
    ".15e-2",
    "000.0015e0",
    "1.2E999",
    "~1.2E999",
    "1E~999",
    "~1E~999",
    "1E12345678901234567890"]

val _ = print "\nTesting {from,to}LargeInt\n"
val _ =
   for
   (fn r =>
    let
       val i = toLargeInt IEEEReal.TO_NEGINF r
       val r' = fromLargeInt i
       val _ = print (concat [exact r,
                              "\t", LargeInt.toString i,
                              "\t", exact r',
                              "\n"])
    in
       if r' == realFloor r
          then ()
       else print (concat ["{from,to}LargeInt bug: ", exact r, "\n"])
    end)

val roundingModes =
   [(TO_NEAREST, "nearest"),
    (TO_NEGINF, "neginf"),
    (TO_POSINF, "posinf"),
    (TO_ZERO, "zero")]

val _ =
   let
      fun doit (x, mode, name) =
         let
            val i = toLargeInt mode x
         in
            print (concat [name, "\t", exact x, "\t", LargeInt.toString i, "\n"])
         end
   in
      List.app
      (fn (mode, name) =>
       List.app (fn s =>
                 let
                    val x = s2r s
                 in
                    doit (x, mode, name)
                    ; doit (~ x, mode, name)
                    ; doit (s2r "1E12" + x, mode, name)
                    ; doit (s2r "~1E12" + x, mode, name)
                 end)
       ["0.0", "0.25", "0.5", "0.75", "1.0", "1.25", "1.5", "1.75", "2.0",
        "2.5", "3.0"])
      roundingModes
   end

val _ = print "\nTesting fromInt\n"

val _ =
   for
   (fn r =>
    case SOME (round r) handle Overflow => NONE of
       NONE => ()
     | SOME i =>
          let
             val r = fromInt i
          in
             if r == fromInt (round r)
                then ()
             else print (concat ["fromInt bug: ", exact r, "\n"])
          end)

val _ = print "\nTesting toInt\n"

val _ =
   for
   (fn r =>
    List.app
    (fn (mode, name) =>
     case SOME (toInt mode r) handle Overflow => NONE of
        NONE => ()
      | SOME i => if i = LargeInt.toInt (toLargeInt mode r)
                     then ()
                  else print (concat ["toInt ", name, " bug: ", exact r, "\n"]))
    roundingModes)

val _ = print "\nTesting ceil,floor,round,trunc\n"

val _ =
   for
   (fn r =>
    List.app
    (fn (mode, f, name) =>
     case SOME (toInt mode r) handle Overflow => NONE of
        NONE => ()
      | SOME i => if i = f r
                     then ()
                  else print (concat [name, " bug: ", exact r, "\n"]))
    [(TO_NEAREST, round, "round"),
     (TO_NEGINF, floor, "floor"),
     (TO_POSINF, ceil, "ceil"),
     (TO_ZERO, trunc, "trunc")])

val _ = print "\nTesting copySign, sameSign, sign, signBit\n"
val _ =
    for'
    (fn r1 =>
     (for'
      (fn r2 =>
       if unordered (r1, r2)
          orelse (if false
                     then print (concat [b2s (signBit r1), "\t",
                                         b2s (signBit r2), "\t",
                                         i2s (sign r1), "\t",
                                         b2s (sameSign (r1, r2)), "\t",
                                         exact (copySign (r1, r2)), "\n"])
                  else ()
                     ; (signBit r1 = Int.< (sign r1, 0)
                        orelse r1 == zero)
                     andalso (sameSign (r1, r2)) = (signBit r1 = signBit r2)
                     andalso sameSign (r2, copySign (r1, r2)))
          then ()
       else print (concat ["copySign,sameSign,sign,signBit bug: ", exact r1, " ", exact r2, "\n"]))))

val _ = print "\nTesting max, min\n"

val _ =
   for'
   (fn r1 =>
    for'
    (fn r2 =>
     let
        val max = max (r1, r2)
        val min = min (r1, r2)
     in
        if (isNan r1 orelse (r1 <= max andalso min <= r1))
           andalso (isNan r2 orelse (r2 <= max andalso min <= r2))
           andalso (r1 == max orelse r2 == max
                    orelse (isNan r1 andalso isNan r2))
           andalso (r1 == min orelse r2 == min
                    orelse (isNan r1 andalso isNan r2))
           then ()
        else print (concat ["max,min bug: ", exact r1, " ", exact r2, "\n"])
     end))

val _ = print "\nTesting Real.{~,*,+,-,/,nextAfter,rem}\n"

val _ =
   for'
   (fn r =>
    List.app
    (fn (name, f, except) =>
     if List.exists (fn r' => r == r') except
         then ()
      else
         print (concat [name, " (", exact r, ") = ",
                        exact (f r), "\n"]))
     [("~", ~, [])
      ])

val _ =
   for'
   (fn r1 =>
    for'
    (fn r2 =>
     List.app
     (fn (name, f, except) =>
      if List.exists (fn (r1', r2') => r1 == r1' andalso r2 == r2') except
         then ()
      else
         print (concat [name, " (", exact r1, ", ", exact r2, ") = ",
                        exact (f (r1, r2)), "\n"]))
     [("*", op *, []),
      ("+", op +, []),
      ("-", op -, []),
      ("/", op /, []),
      ("nextAfter", nextAfter, []),
      ("rem", rem, [])
      ]))

val _ =
   List.app
   (fn (r1, r2, name, f, chk) =>
    if chk (f (r1, r2))
       then ()
       else print (concat [name, "(", exact r1, ",", exact r2, ") bug: ", exact (f (r1, r2)), "\n"]))
   [(posInf, posInf, "+", op +, fn x => x == posInf),
    (negInf, negInf, "+", op +, fn x => x == negInf),
    (posInf, negInf, "-", op -, fn x => x == posInf),
    (negInf, posInf, "-", op -, fn x => x == negInf),
    (nan, one, "+", op +, isNan),
    (nan, one, "-", op -, isNan),
    (nan, one, "*", op *, isNan),
    (nan, one, "/", op /, isNan),
    (posInf, negInf, "+", op +, isNan),
    (negInf, posInf, "+", op +, isNan),
    (posInf, posInf, "-", op -, isNan),
    (negInf, negInf, "-", op -, isNan)]

val _ = print "\nTesting Real.Math.{acos,asin,atan,cos,cosh,exp,ln,log10,sin,sinh,sqrt,tan,tanh,atan2,pow}\n"

val _ =
   for' (fn r =>
         List.app
         (fn (name, f, except) =>
          if List.exists (fn r' => r == r') except
             then ()
          else
             print (concat [name, " (", exact r, ") = ",
                            fmt (StringCvt.GEN (SOME 10)) (f r), "\n"]))
         let
            open Real.Math
         in
            [("acos", acos, []),
             ("asin", asin, []),
             ("atan", atan, []),
             ("cos", cos, []),
             ("cosh", cosh, []),
             ("exp", exp, []),
             ("ln", ln, []),
             ("log10", log10, []),
             ("sin", sin, []),
             ("sinh", sinh, []),
             ("sqrt", sqrt, []),
             ("tan", tan, []),
             ("tanh", tanh, [])]
         end)

val _ =
   for'
   (fn r1 =>
    for'
    (fn r2 =>
     List.app
     (fn (name, f, except) =>
      if List.exists (fn (r1', r2') => r1 == r1' andalso r2 == r2') except
         then ()
      else
         print (concat [name, " (", exact r1, ", ", exact r2, ") = ",
                        fmt (StringCvt.GEN (SOME 10)) (f (r1, r2)), "\n"]))
     let
        open Real.Math
     in
        [("atan2", atan2, []),
         ("pow", pow, [])]
     end))

val _ = print "\nTesting *+, *-\n"
val fmaDiffs = ref false
val _ =
   for
   (fn r1 =>
    for
    (fn r2 =>
     for
     (fn r3 =>
      let
        val fma = *+ (r1, r2, r3)
        val std = r1 * r2 + r3
      in
        if fma == std
           then ()
        else (print o concat)
               [(if !fmaDiffs then
                  ""
                else
                  let
                    val _ = fmaDiffs := true
                  in
                    concat["SOME *+ AND STANDARD RESULTS DIFFER\n",
                           "(r1, r2, r3): *+(r1, r2, r3)\t(r1 * r2 + r3)\n",
                           "--------------------------------------------\n"]
                  end),
                "(",
                exact r1,
                ", ",
                exact r2,
                ", ",
                exact r3,
                "): ",
                exact fma,
                "\t",
                exact std,
                "\n"]
      end)))

val _ = print "\nTesting Real.{realCeil,realFloor,realTrunc}\n"
val _ =
   for
   (fn r =>
    let
       val ceil = realCeil r
       val floor = realFloor r
       val trunc = realTrunc r
       val _ = print (concat [exact r, "  ",
                              exact ceil, " ",
                              exact floor, " ",
                              exact trunc, "\n"])
    in
       if floor <= r
          andalso r <= ceil
          andalso abs trunc <= abs r
          then ()
       else print (concat ["Real.{realCeil,realFloor,realTrunc} bug ", exact r, "\n"])
    end)

val _ = print "\nTesting Real.{<,<=,>,>=,==,!=,?=,unordered}\n"

val _ =
   for
   (fn r1 =>
    for
    (fn r2 =>
     let
        val _ = 
           List.app
           (fn (f, name) =>
            print (concat [name, " (", exact r1, ", ", exact r2, ") = ",
                           b2s (f (r1, r2)), "\n"]))
           [(Real.<, "<"),
            (Real.>, ">"),
            (Real.==, "=="),
            (Real.?=, "?=")]
     in
        if unordered (r1, r2) = (isNan r1 orelse isNan r2)
           andalso (r1 != r2) = not (r1 == r2)
           andalso if unordered (r1, r2)
                      then (false = (r1 <= r2)
                            andalso false = (r1 < r2)
                            andalso false = (r1 >= r2)
                            andalso false = (r1 > r2)
                            andalso false = (r1 == r2)
                            andalso if isNan r1 andalso isNan r2
                                       then true = ?= (r1, r2) 
                                    else true)
                   else ((r1 < r2) = not (r1 >= r2)
                         andalso (r1 > r2) = not (r1 <= r2))
           then ()
        else print (concat ["Real.{<,<=,>,>=,==,!=,?=,unordered} bug: ", exact r1, " ", exact r2, "\n"])
     end))

val _ = print "\nTesting compare, compareReal\n"

val _ =
   for
   (fn r =>
    for
    (fn r' =>
     let
        val c =
           case SOME (compare (r, r')) handle IEEEReal.Unordered => NONE of
              NONE => "Unordered"
            | SOME z =>
                 case z of
                    EQUAL => "EQUAL"
                  | GREATER => "GREATER"
                  | LESS => "LESS"
        datatype z = datatype IEEEReal.real_order
        val cr =
           case compareReal (r, r') of
              EQUAL => "EQUAL"
            | GREATER => "GREATER"
            | LESS => "LESS"
            | UNORDERED => "UNORDERED"
        val _ = print (concat ["compare (", exact r, ", ", exact r', ") = ", c, "\n"])
        val _ = print (concat ["compareReal (", exact r, ", ", exact r', ") = ", cr, "\n"])
     in
        if compareReal (r, r') = (case compareReal (r', r) of
                                     EQUAL => EQUAL
                                   | GREATER => LESS
                                   | LESS => GREATER
                                   | UNORDERED => UNORDERED)
           then ()
        else print (concat ["compareReal bug: ", exact r, " ", exact r', "\n"])
     end))

val _ = print "\nTesting abs\n"

val _ = for (fn r =>
             if abs r == abs (~ r)
                then ()
             else print (concat ["abs bug: ", exact r, "\n"]))

val _ = print "\nTesting {from,to}ManExp\n"
         
val _ =
   for
   (fn x =>
    if List.exists (fn y => x == y) [halfMinNormalPos, minPos,
                                     ~halfMinNormalPos, ~minPos]
       then ()
    else
       let
          val {exp, man} = toManExp x
          val _ =
             if true
                then
                   print (concat [exact x, " = ", exact man, " * 2^", i2s exp,
                                  "\n"])
             else ()
          val x' = fromManExp {exp = exp, man = man}
          val _ =
             if true
                then
                   print (concat ["\t = ", exact x', "\n"])
             else ()
       in
          if x == x'
             then ()
          else print (concat ["{from,to}ManExp bug: ", exact x, "\n"])
       end)

val _ = print "\nTesting split\n"

val _ =
   for (fn r =>
        let
           val {whole, frac} = split r
           val _ =
              if false
                 then
                    print (concat ["split ", exact r, " = {whole = ",
                                   exact whole, ", frac = ", exact frac, "}\n",
                                   "realMod ", exact whole, " = ",
                                   exact (realMod whole), "\t",
                                   b2s (sameSign (r, whole)), "\t",
                                   b2s (sameSign (r, frac)), "\n"])
              else ()
        in
           if realMod r == frac
              andalso realMod whole == zero
              andalso abs frac < one
              andalso sameSign (r, whole)
              andalso sameSign (r, frac)
              andalso (case class r of
                          INF => whole == r andalso frac == zero
                        | NAN => isNan whole andalso isNan frac
                        | _ => r == whole + frac)
              then ()
           else print (concat ["split bug: ", exact r, "\n"])
        end)

val _ = print "\nTesting {from,to}Large\n"

val _ =
   for
   (fn r =>
    if r == fromLarge TO_NEAREST (toLarge r)
       then ()
    else print (concat ["{from,to}Large bug: ", exact r, "\n"]))

end

structure Z = Test (structure Real = Real32
                    val size = 32)
structure Z = Test (structure Real = Real64
                    val size = 64)