Debian Codegraph @

math/+test/trig_test.ha

raw
1 // SPDX-License-Identifier: MPL-2.0
2 // (c) Hare authors <https://harelang.org>
3
4 @test fn trig_reduce() void = {
5 for (let idx = 0z; idx < 10; idx += 1) {
6 const reduced = trig_reduce(TEST_INPUTS[idx]);
7 const j = reduced.0;
8 const z = reduced.1;
9 const xred = (j: i64: f64) * (PI / 4f64) + z;
10 assert(isclosef64(
11 sinf64(TEST_INPUTS[idx]),
12 sinf64(xred)));
13 };
14 };
15
16 @test fn cos() void = {
17 for (let idx = 0z; idx < 10; idx += 1) {
18 assert(isclosef64(
19 cosf64(TEST_INPUTS[idx]), TEST_COS[idx]));
20 };
21 assert(isnan(cosf64(-INF)));
22 assert(isnan(cosf64(INF)));
23 assert(isnan(cosf64(NAN)));
24 };
25
26 @test fn sin() void = {
27 for (let idx = 0z; idx < 10; idx += 1) {
28 assert(isclosef64(
29 sinf64(TEST_INPUTS[idx]), TEST_SIN[idx]));
30 };
31 assert(isnan(sinf64(-INF)));
32 assert(sinf64(-0f64) == -0f64);
33 assert(sinf64(0f64) == 0f64);
34 assert(isnan(sinf64(INF)));
35 assert(isnan(sinf64(NAN)));
36 };
37
38 @test fn tan() void = {
39 for (let idx = 0z; idx < 10; idx += 1) {
40 assert(isclosef64(
41 tanf64(TEST_INPUTS[idx]), TEST_TAN[idx]));
42 };
43 assert(isnan(sinf64(-INF)));
44 assert(sinf64(-0f64) == -0f64);
45 assert(sinf64(0f64) == 0f64);
46 assert(isnan(sinf64(INF)));
47 assert(isnan(sinf64(NAN)));
48 };
49
50 @test fn asin() void = {
51 for (let idx = 0z; idx < 10; idx += 1) {
52 assert(isclosef64(
53 asinf64(TEST_INPUTS[idx] / 10f64),
54 TEST_ASIN[idx]));
55 };
56 assert(isnan(asinf64(-PI)));
57 assert(asinf64(-0f64) == -0f64);
58 assert(asinf64(0f64) == 0f64);
59 assert(isnan(asinf64(PI)));
60 assert(isnan(asinf64(NAN)));
61 };
62
63 @test fn acos() void = {
64 for (let idx = 0z; idx < 10; idx += 1) {
65 assert(isclosef64(
66 acosf64(TEST_INPUTS[idx] / 10f64),
67 TEST_ACOS[idx]));
68 };
69 assert(isnan(acosf64(-PI)));
70 assert(acosf64(1f64) == 0f64);
71 assert(isnan(acosf64(PI)));
72 assert(isnan(acosf64(NAN)));
73 };
74
75 @test fn atan() void = {
76 for (let idx = 0z; idx < 10; idx += 1) {
77 assert(isclosef64(
78 atanf64(TEST_INPUTS[idx]),
79 TEST_ATAN[idx]));
80 };
81 assert(atanf64(-INF) == -PI / 2f64);
82 assert(atanf64(-0f64) == -0f64);
83 assert(atanf64(0f64) == 0f64);
84 assert(atanf64(INF) == PI / 2f64);
85 assert(isnan(atanf64(NAN)));
86 };
87
88 @test fn sinh() void = {
89 for (let idx = 0z; idx < 10; idx += 1) {
90 assert(isclosef64(
91 sinhf64(TEST_INPUTS[idx]),
92 TEST_SINH[idx],
93 1e-6f64));
94 };
95 assert(sinhf64(-INF) == -INF);
96 assert(sinhf64(-0f64) == -0f64);
97 assert(sinhf64(0f64) == 0f64);
98 assert(sinhf64(INF) == INF);
99 assert(isnan(sinhf64(NAN)));
100 };
101
102 @test fn cosh() void = {
103 for (let idx = 0z; idx < 10; idx += 1) {
104 assert(isclosef64(
105 coshf64(TEST_INPUTS[idx]),
106 TEST_COSH[idx]));
107 };
108 assert(coshf64(-INF) == INF);
109 assert(coshf64(-0f64) == 1f64);
110 assert(coshf64(0f64) == 1f64);
111 assert(coshf64(INF) == INF);
112 assert(isnan(coshf64(NAN)));
113 };
114
115 @test fn tanh() void = {
116 for (let idx = 0z; idx < 10; idx += 1) {
117 assert(isclosef64(
118 tanhf64(TEST_INPUTS[idx]),
119 TEST_TANH[idx]));
120 };
121 assert(tanhf64(-INF) == -1f64);
122 assert(tanhf64(-0f64) == -0f64);
123 assert(tanhf64(0f64) == 0f64);
124 assert(tanhf64(INF) == 1f64);
125 assert(isnan(tanhf64(NAN)));
126 };
127
128 @test fn asinh() void = {
129 for (let idx = 0z; idx < 10; idx += 1) {
130 assert(isclosef64(
131 asinhf64(TEST_INPUTS[idx]),
132 TEST_ASINH[idx]));
133 };
134 assert(asinhf64(-INF) == -INF);
135 assert(asinhf64(-0f64) == -0f64);
136 assert(asinhf64(0f64) == 0f64);
137 assert(asinhf64(INF) == INF);
138 assert(isnan(asinhf64(NAN)));
139 };
140
141 @test fn acosh() void = {
142 for (let idx = 0z; idx < 10; idx += 1) {
143 assert(isclosef64(
144 acoshf64(1f64 + absf64(TEST_INPUTS[idx])),
145 TEST_ACOSH[idx]));
146 };
147 assert(isnan(acoshf64(-INF)));
148 assert(isnan(acoshf64(0.5f64)));
149 assert(acoshf64(1f64) == 0f64);
150 assert(acoshf64(INF) == INF);
151 assert(isnan(acoshf64(NAN)));
152 };
153
154 @test fn atanh() void = {
155 for (let idx = 0z; idx < 10; idx += 1) {
156 assert(isclosef64(
157 atanhf64(TEST_INPUTS[idx] / 10f64),
158 TEST_ATANH[idx]));
159 };
160 assert(isnan(atanhf64(-INF)));
161 assert(isnan(atanhf64(-PI)));
162 assert(atanhf64(-1f64) == -INF);
163 assert(atanhf64(-0f64) == -0f64);
164 assert(atanhf64(0f64) == 0f64);
165 assert(atanhf64(1f64) == INF);
166 assert(isnan(atanhf64(PI)));
167 assert(isnan(atanhf64(INF)));
168 assert(isnan(atanhf64(NAN)));
169 };
170
171 @test fn atan2() void = {
172 for (let idx = 0z; idx < 10; idx += 1) {
173 assert(isclosef64(
174 atan2f64(10f64, TEST_INPUTS[idx]),
175 TEST_ATAN2[idx]));
176 };
177 assert(isnan(atan2f64(-INF, NAN)));
178 assert(atan2f64(-PI, INF) == -0f64);
179 assert(isnan(atan2f64(-PI, NAN)));
180 assert(atan2f64(-0f64, 0f64) == -0f64);
181 assert(atan2f64(-0f64, PI) == -0f64);
182 assert(atan2f64(-0f64, INF) == -0f64);
183 assert(isnan(atan2f64(-0f64, NAN)));
184 assert(atan2f64(0f64, 0f64) == 0f64);
185 assert(atan2f64(0f64, PI) == 0f64);
186 assert(atan2f64(0f64, INF) == 0f64);
187 assert(isnan(atan2f64(0f64, NAN)));
188 assert(atan2f64(PI, INF) == 0f64);
189 assert(atan2f64(1f64, INF) == 0f64);
190 assert(atan2f64(-1f64, INF) == -0f64);
191 assert(isnan(atan2f64(PI, NAN)));
192 assert(isnan(atan2f64(INF, NAN)));
193 assert(isnan(atan2f64(NAN, NAN)));
194 };
195
196 @test fn hypot() void = {
197 for (let idx = 0z; idx < 10; idx += 1) {
198 const a = absf64(1e200f64 * TEST_TANH[idx] * SQRT_2);
199 assert(isclosef64(
200 hypotf64(1e200f64 * TEST_TANH[idx],
201 1e200f64 * TEST_TANH[idx]),
202 a));
203 };
204 assert(hypotf64(-INF, -INF) == INF);
205 assert(hypotf64(-INF, 0f64) == INF);
206 assert(hypotf64(-INF, INF) == INF);
207 assert(hypotf64(-INF, NAN) == INF);
208 assert(hypotf64(-0f64, -0f64) == 0f64);
209 assert(hypotf64(-0f64, 0f64) == 0f64);
210 assert(hypotf64(0f64, -0f64) == 0f64);
211 assert(hypotf64(0f64, 0f64) == 0f64);
212 assert(hypotf64(0f64, -INF) == INF);
213 assert(hypotf64(0f64, INF) == INF);
214 assert(isnan(hypotf64(0f64, NAN)));
215 assert(hypotf64(INF, -INF) == INF);
216 assert(hypotf64(INF, 0f64) == INF);
217 assert(hypotf64(INF, INF) == INF);
218 assert(hypotf64(INF, NAN) == INF);
219 assert(hypotf64(NAN, -INF) == INF);
220 assert(isnan(hypotf64(NAN, 0f64)));
221 assert(hypotf64(NAN, INF) == INF);
222 assert(isnan(hypotf64(NAN, NAN)));
223 };