Import Upstream version 20180207
[hcoop/debian/mlton.git] / regression / math.sml
CommitLineData
7f918cf1
CE
1(* Auxiliary functions for test cases *)
2
3infix 1 seq
4fun e1 seq e2 = e2;
5fun check b = if b then "OK" else "WRONG";
6fun check' f = (if f () then "OK" else "WRONG") handle _ => "EXN";
7
8fun range (from, to) p =
9 let open Int
10 in
11 (from > to) orelse (p from) andalso (range (from+1, to) p)
12 end;
13
14fun checkrange bounds = check o range bounds;
15
16fun tst0 s s' = print (s ^ " \t" ^ s' ^ "\n");
17fun tst s b = tst0 s (check b);
18fun tst' s f = tst0 s (check' f);
19
20fun tstrange s bounds = (tst s) o range bounds
21
22(* test/math.sml
23 PS 1995-02-25, 1996-04-01, 1997-03-07
24*)
25
26val _ = print "\nFile math.sml: Testing structure Math...\n"
27
28local
29 open Math
30 val MAXDOUBLE = 8.98846567431157E307;
31 val MINDOUBLE = 4.94065645841246544E~324
32 val PI = 3.14159265358979323846;
33 val E = 2.7182818284590452354;
34
35 val eps = 1E~8
36 infix 4 ===
37 fun x === y =
38 abs (x - y) <= eps orelse abs(x-y) <= eps * (abs x + abs y)
39
40 fun check1 (opr, a, r) = if opr a === r then "OK" else "WRONG"
41 fun check2 (opr, a1, a2, r) =
42 if opr(a1, a2) === r then "OK" else "WRONG"
43 fun tst1 s (opr, a, r) = tst0 s (check1 (opr, a, r))
44 fun tst2 s (opr, a1, a2, r) = tst0 s (check2 (opr, a1, a2, r))
45
46val test0a = tst "test0a" (PI === pi);
47val test0b = tst "test0b" (E === e);
48
49val test1a = tst1 "test1a" (sqrt, 64.0, 8.0);
50val test1b = tst1 "test1b" (sqrt, 0.0, 0.0);
51val test1c = tst0 "test1c" (if Real.isNan(sqrt ~1.0) then "OK" else "WRONG")
52
53val test2a = tst1 "test2a" (sin, 0.0, 0.0);
54val test2b = tst1 "test2b" (sin, pi/2.0, 1.0);
55val test2c = tst1 "test2c" (sin, pi, 0.0);
56val test2d = tst1 "test2d" (sin, 3.0*pi/2.0, ~1.0);
57
58val test3a = tst1 "test3a" (cos, 0.0, 1.0);
59val test3b = tst1 "test3b" (cos, pi/2.0, 0.0);
60val test3c = tst1 "test3c" (cos, pi, ~1.0);
61val test3d = tst1 "test3d" (cos, 3.0*pi/2.0, 0.0);
62
63val test4a = tst1 "test4a" (tan, 0.0, 0.0);
64val test4b = tst1 "test4b" (tan, pi/4.0, 1.0);
65val test4c = tst1 "test4c" (tan, pi, 0.0);
66val test4d = tst1 "test4d" (tan, 3.0*pi/4.0, ~1.0);
67val test4e = tst1 "test4e" (tan, ~pi/4.0, ~1.0);
68val test4f = tst "test4f" ((abs(tan (pi/2.0)) > 1E8) handle _ => true);
69val test4g = tst "test4g" ((abs(tan (~pi/2.0)) > 1E8) handle _ => true);
70
71val test5a = tst1 "test5a" (asin, 0.0, 0.0);
72val test5b = tst1 "test5b" (asin, 1.0, pi/2.0);
73val test5c = tst1 "test5c" (asin, ~1.0, ~pi/2.0);
74val test5d = tst0 "test5d" (if Real.isNan(asin 1.1) then "OK" else "WRONG")
75val test5e = tst0 "test5e" (if Real.isNan(asin ~1.1) then "OK" else "WRONG")
76
77val test6a = tst1 "test6a" (acos, 1.0, 0.0);
78val test6b = tst1 "test6b" (acos, 0.0, pi/2.0);
79val test6c = tst1 "test6c" (acos, ~1.0, pi);
80val test6d = tst0 "test6d" (if Real.isNan(acos 1.1) then "OK" else "WRONG")
81val test6e = tst0 "test6e" (if Real.isNan(acos ~1.1) then "OK" else "WRONG")
82
83val test7a = tst1 "test7a" (atan, 0.0, 0.0);
84val test7b = tst1 "test7b" (atan, 1.0, pi/4.0);
85val test7c = tst1 "test7c" (atan, ~1.0, ~pi/4.0);
86val test7d = tst1 "test7d" (atan, 1E8, pi/2.0);
87val test7e = tst1 "test7e" (atan, ~1E8, ~pi/2.0);
88
89(* atan2 -- here I am in doubt over the argument order, since the New
90Basis document is inconsistent with itself and with atan2 in the C
91libraries. *)
92
93val test8a = tst2 "test8a" (atan2, 0.0, 0.0, 0.0);
94val test8b = tst2 "test8b" (atan2, 1.0, 0.0, pi/2.0);
95val test8c = tst2 "test8c" (atan2, ~1.0, 0.0, ~pi/2.0);
96val test8d = tst2 "test8d" (atan2, 1.0, 1.0, pi/4.0);
97val test8e = tst2 "test8e" (atan2, ~1.0, 1.0, ~pi/4.0);
98val test8f = tst2 "test8f" (atan2, ~1.0, ~1.0, ~3.0*pi/4.0);
99val test8g = tst2 "test8g" (atan2, 1.0, ~1.0, 3.0*pi/4.0);
100val test8h = tst2 "test8h" (atan2, 1E8, 1.0, pi/2.0);
101val test8i = tst2 "test8i" (atan2, ~1E8, 1.0, ~pi/2.0);
102val test8j = tst2 "test8j" (atan2, 1.0, 1E8, 0.0);
103val test8k = tst2 "test8k" (atan2, 1.0, ~1E8, pi);
104val test8l = tst2 "test8l" (atan2, ~1.0, ~1E8, ~pi);
105
106val test9a = tst1 "test9a" (exp, 0.0, 1.0);
107val test9b = tst1 "test9b" (exp, 1.0, e);
108val test9c = tst1 "test9c" (exp, ~1.0, 1.0/e);
109
110val test10a = tst1 "test10a" (ln, 1.0, 0.0);
111val test10b = tst1 "test10b" (ln, e, 1.0);
112val test10c = tst1 "test10c" (ln, 1.0/e, ~1.0);
113val test10d = tst0 "test10d" (if Real.==(ln 0.0,Real.negInf) then "OK" else "WRONG")
114val test10e = tst0 "test10e" (if Real.isNan(ln ~1.0) then "OK" else "WRONG")
115val test10f = tst0 "test10f" (if Real.==(ln Real.posInf, Real.posInf) then "OK" else "WRONG")
116val test10g = tst0 "test10g" (if Real.==(Real.posInf, Real.posInf) then "OK" else "WRONG")
117
118val test12a = tst2 "test12a" (pow, 0.0, 0.0, 1.0); (* arbitrary, might be 0.0 *)
119val test12b = tst2 "test12b" (pow, 7.0, 0.0, 1.0);
120val test12c = tst2 "test12c" (pow, 0.0, 7.0, 0.0);
121val test12d = tst2 "test12d" (pow, 64.0, 0.5, 8.0);
122val test12e = tst2 "test12e" (pow, ~9.0, 2.0, 81.0);
123val test12f = tst2 "test12f" (pow, 10.0, ~2.0, 0.01);
124val test12g = tst2 "test12g" (pow, ~10.0, ~2.0, 0.01);
125val test12h = tst2 "test12h" (pow, 0.0, 0.5, 0.0);
126val test12i = tst2 "test12i" (pow, 0.4, ~2.0, 6.25);
127
128(*we do not follow the Basis Library specification exactly here, but rather follow math.h
129val test12j = tst0 "test12j" (if Real.==(pow(0.0, ~1.0),Real.posInf) then "OK" else "WRONG")
130val test12k = tst0 "test12k" (if Real.==(pow(0.0, ~0.5),Real.posInf) then "OK" else "WRONG")
131*)
132val test12l = tst0 "test12l" (if Real.isNan(pow(~1.0, 1.1)) then "OK" else "WRONG")
133val test12m = tst0 "test12m" (if Real.isNan(pow(~1.0, 0.5)) then "OK" else "WRONG")
134(* sweeks removed 12n because it fails on FreeBSD on x86, apparently due to a
135 * 64 bit vs 80 bit issue.
136 *)
137(* val test12n = tst0 "test12n" (if Real.==(pow(3.0, 1000000.0),Real.posInf) then "OK" else "WRONG") *) (* not in basis lib spec.*)
138val test13a = tst1 "test13a" (log10, 1.0, 0.0);
139val test13b = tst1 "test13b" (log10, 10.0, 1.0);
140val test13c = tst1 "test13c" (log10, 100.0, 2.0);
141val test13d = tst1 "test13d" (log10, 0.1, ~1.0);
142val test13e = tst1 "test13e" (log10, 0.01, ~2.0);
143
144val check14a = tst1 "test14a" (sinh, 0.0, 0.0);
145val check14b = tst1 "test14b" (sinh, 1.0, 1.17520119364);
146val check14c = tst1 "test14c" (sinh, ~1.0, ~1.17520119364);
147val check14d = tst1 "test14d" (sinh, 2.0, 3.62686040785);
148val check14e = tst1 "test14e" (sinh, ~2.0, ~3.62686040785);
149
150val check15a = tst1 "test15a" (cosh, 0.0, 1.0);
151val check15b = tst1 "test15b" (cosh, 1.0, 1.54308063482);
152val check15c = tst1 "test15c" (cosh, ~1.0, 1.54308063482);
153val check15d = tst1 "test15d" (cosh, 2.0, 3.76219569108);
154val check15e = tst1 "test15e" (cosh, ~2.0, 3.76219569108);
155
156val check16a = tst1 "test16a" (tanh, 0.0, 0.0);
157val check16b = tst1 "test16b" (tanh, 1.0, 0.761594155956);
158val check16c = tst1 "test16c" (tanh, ~1.0, ~0.761594155956);
159val check16d = tst1 "test16d" (tanh, 2.0, 0.964027580076);
160val check16e = tst1 "test16e" (tanh, ~2.0, ~0.964027580076);
161val check16f = tst1 "test16f" (tanh, 100.0, 1.0);
162val check16g = tst1 "test16g" (tanh, ~100.0, ~1.0);
163in
164end