1 functor Test (structure Real : REAL
) =
4 datatype z
= datatype IEEEReal
.float_class
5 datatype z
= datatype IEEEReal
.rounding_mode
8 print (Real.fmt
StringCvt.EXACT r
)
11 print (Int.fmt
StringCvt.DEC i
)
14 print (LargeInt
.fmt
StringCvt.DEC li
)
18 TO_NEAREST
=> print
"TO_NEAREST"
19 | TO_NEGINF
=> print
"TO_NEGINF"
20 | TO_POSINF
=> print
"TO_POSINF"
21 | TO_ZERO
=> print
"TO_ZERO"
23 datatype 'a res
= OK
of 'a | OVERFLOW
24 fun showRes showOk r
=
27 | OVERFLOW
=> print
"Overflow"
28 fun wrap th
= (OK (th ())) handle Overflow
=> OVERFLOW
30 fun checkIntToReal i
=
32 val li
= Int.toLarge i
33 val ri
= Real.fromInt i
34 val rli
= Real.fromLargeInt li
46 ; print (if Real.== (ri
, rli
) then "OK\n" else "BAD\n")
50 fun checkRealToInt mode r
=
52 val i
= wrap (fn () => Real.toInt mode r
)
53 val li
= Real.toLargeInt mode r
65 ; print (if wrap (fn () => Int.fromLarge li
) = i
then "OK\n" else "BAD\n")
69 val is
: Int.int list
=
76 val minInt
= valOf
Int.minInt
77 val maxInt
= valOf
Int.maxInt
78 val (zero
,one
,two
,three
) =
79 (Int.fromInt
0, Int.fromInt
1,
80 Int.fromInt
2, Int.fromInt
3)
81 val one_half
= maxInt
div two
83 val one_fourth
= maxInt
div four
84 val eight
= four
* two
85 val one_eighth
= maxInt
div eight
86 val sixteen
= eight
* two
87 val one_sixteenth
= maxInt
div sixteen
88 val thirtytwo
= sixteen
* two
89 val one_thirtysecond
= maxInt
div thirtytwo
90 val sixtyfour
= thirtytwo
* two
91 val one_sixtyfourth
= maxInt
div sixtyfour
93 [minInt
, minInt
+ one
, minInt
+ two
, minInt
+ three
,
94 minInt
+ one_sixtyfourth
, minInt
+ one_thirtysecond
,
95 minInt
+ one_sixteenth
, minInt
+ one_eighth
,
96 minInt
+ one_fourth
, minInt
+ one_half
,
97 ~three
,~two
,~one
,zero
,one
,two
,three
,
98 maxInt
- one_half
, maxInt
- one_fourth
,
99 maxInt
- one_eighth
, maxInt
- one_sixteenth
,
100 maxInt
- one_thirtysecond
, maxInt
- one_sixtyfourth
,
101 maxInt
- three
, maxInt
- two
, maxInt
- one
, maxInt
]
103 val () = List.app checkIntToReal is
107 val r
= Real.fromInt i
108 fun make (fold
,inf
) =
111 val r
' = Real.nextAfter (r
, inf
)
115 (r
,[]) (List.tabulate (64,fn _
=> ()))
116 val make
= fn (fold
,inf
) => #
2 (make (fold
,inf
))
118 (make (foldl
,Real.negInf
)) @
120 (make (foldr
,Real.posInf
))
123 val rs
= List.concat rs
124 val () = List.app (checkRealToInt TO_NEAREST
) rs
125 val () = List.app (checkRealToInt TO_NEGINF
) rs
126 val () = List.app (checkRealToInt TO_POSINF
) rs
127 val () = List.app (checkRealToInt TO_ZERO
) rs
131 structure Z
= Test (structure Real = Real32
)
132 structure Z
= Test (structure Real = Real64
)