Backport from sid to buster
[hcoop/debian/mlton.git] / regression / real-basic.sml
CommitLineData
7f918cf1
CE
1functor Basic(structure Real : REAL
2 structure Pack : PACK_REAL
3 where type real = Real.real) =
4 struct
5 open Real
6 val () = print " Reported\n"
7 val () = print (" precision: " ^ Int.toString precision ^ "\n")
8 val {man=_, exp} = toManExp maxFinite
9 val () = print (" max exponent: " ^ Int.toString exp ^ "\n")
10 val {man=_, exp} = toManExp minNormalPos
11 val () = print (" min exponent: " ^ Int.toString exp ^ "\n")
12 val {man=_, exp} = toManExp minPos
13 val () = print (" min denormal: " ^ Int.toString exp ^ "\n")
14
15 (* Now let's compute the actual mantissa *)
16 val zero = fromInt 0
17 val one = fromInt 1
18 val two = fromInt 2
19
20 fun precision eq x =
21 if eq (x+one, x) then 0 else
22 Int.+ (1, precision eq (x+x))
23 fun maxExp eq x =
24 if eq (x, x+x) then 0 else
25 Int.+ (1, maxExp eq (x+x))
26 fun lowBit (1, x) = x
27 | lowBit (i, x) = lowBit (Int.- (i, 1), x / two)
28 fun minExp eq x =
29 if not (eq (x, (x / two) * two)) orelse eq (x, zero) then 1 else
30 Int.- (minExp eq (x / two), 1)
31
32 val eq = ==
33 val xprecision = precision eq one
34 val lastBit = one + lowBit (xprecision, one)
35 val xmaxExp = maxExp eq one
36 val xminNormalExp = minExp eq lastBit
37 val xminExp = minExp eq one
38
39 val () = print " Actual\n"
40 val () = print (" precision: " ^ Int.toString xprecision ^ "\n")
41 val () = print (" max exponent: " ^ Int.toString xmaxExp ^ "\n")
42 val () = print (" min exponent: " ^ Int.toString xminNormalExp ^ "\n")
43 val () = print (" min denormal: " ^ Int.toString xminExp ^ "\n")
44
45 val a = Word8Array.array (Pack.bytesPerElem, 0w0)
46 fun id x = (Pack.update (a, 0, x); Pack.subArr (a, 0))
47
48 val eq = fn (x, y) => == (id x, id y)
49 val xprecision = precision eq one
50 val lastBit = one + lowBit (xprecision, one)
51 val xmaxExp = maxExp eq one
52 val xminNormalExp = minExp eq lastBit
53 val xminExp = minExp eq one
54
55 val () = print " Exported\n"
56 val () = print (" precision: " ^ Int.toString xprecision ^ "\n")
57 val () = print (" max exponent: " ^ Int.toString xmaxExp ^ "\n")
58 val () = print (" min exponent: " ^ Int.toString xminNormalExp ^ "\n")
59 val () = print (" min denormal: " ^ Int.toString xminExp ^ "\n")
60 end
61
62val () = print "Real32\n"
63structure Z = Basic(structure Real = Real32
64 structure Pack = PackReal32Little)
65val () = print "Real64\n"
66structure Z = Basic(structure Real = Real64
67 structure Pack = PackReal64Big)