Debian Codegraph @

t/10_math.t

raw
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use sigtrap qw(die normal-signals error-signals);
6
7 use Test::More tests => 157;
8
9 use POSIX::2008;
10
11 sub _cmp_between {
12 my ($lower, $value, $upper, $desc) = @_;
13 local $Test::Builder::Level = $Test::Builder::Level + 1;
14
15 cmp_ok($lower, '<', $value, $desc);
16 cmp_ok($value, '<', $upper, $desc);
17 }
18
19 SKIP: {
20 if (! defined &POSIX::2008::acos) { skip 'acos() UNAVAILABLE', 3; }
21 cmp_ok(POSIX::2008::acos(1.0), '==', 0.0, 'acos(1) == 0');
22 _cmp_between(1.047197, POSIX::2008::acos(.5), 1.047198, 'acos(.5)');
23 }
24
25 SKIP: {
26 if (! defined &POSIX::2008::acosh) { skip 'acosh() UNAVAILABLE', 3; }
27 cmp_ok(POSIX::2008::acosh(1.0), '==', 0.0, 'acosh(1) == 0');
28 _cmp_between(2.292431, POSIX::2008::acosh(5.0), 2.292432, 'acosh(5)');
29 }
30
31 SKIP: {
32 if (! defined &POSIX::2008::asin) { skip 'asin() UNAVAILABLE', 4; }
33 cmp_ok(POSIX::2008::asin(0.0), '==', 0.0, 'asin(0) == 0');
34 cmp_ok(POSIX::2008::asin(-.75), '==', -POSIX::2008::asin(.75), 'asin(-x) == -asin(x)');
35 _cmp_between(0.523598, POSIX::2008::asin(.5), 0.523599, 'asin(.5)');
36 }
37
38 SKIP: {
39 if (! defined &POSIX::2008::asinh) { skip 'asinh() UNAVAILABLE', 4; }
40 cmp_ok(POSIX::2008::asinh(0.0), '==', 0.0, 'asinh(0) == 0');
41 cmp_ok(POSIX::2008::asinh(-.75), '==', -POSIX::2008::asinh(.75), 'asinh(-x) == -asinh(x)');
42 _cmp_between(0.481211, POSIX::2008::asinh(.5), 0.481212, 'asinh(.5)');
43 }
44
45 SKIP: {
46 if (! defined &POSIX::2008::atan) { skip 'atan() UNAVAILABLE', 4; }
47 cmp_ok(POSIX::2008::atan(0.0), '==', 0.0, 'atan(0) == 0');
48 cmp_ok(POSIX::2008::atan(-.5), '==', -POSIX::2008::atan(.5), 'atan(-x) == -atan(x)');
49 _cmp_between(0.463647, POSIX::2008::atan(.5), 0.463648, 'atan(.5)');
50 }
51
52 SKIP: {
53 if (! defined &POSIX::2008::atanh) { skip 'atanh() UNAVAILABLE', 4; }
54 cmp_ok(POSIX::2008::atanh(0.0), '==', 0.0, 'atanh(0) == 0');
55 cmp_ok(POSIX::2008::atanh(-.75), '==', -POSIX::2008::atanh(.75), 'atanh(-x) == -atanh(x)');
56 _cmp_between(0.549306, POSIX::2008::atanh(.5), 0.549307, 'atanh(.5)');
57 }
58
59 SKIP: {
60 if (! defined &POSIX::2008::atan2) { skip 'atan2() UNAVAILABLE', 5; }
61 cmp_ok(POSIX::2008::atan2(0.0, 1.0), '==', 0.0, 'atan2(0, 1) == 0');
62 cmp_ok(POSIX::2008::atan2(-3.0, 4.0), '==', -POSIX::2008::atan2(3.0, 4.0), 'atan2(-y, x) == -atan2(y, x)');
63 cmp_ok(POSIX::2008::atan2(1.0, 2.0), '==', POSIX::2008::atan(.5), 'atan2(1, 2) == atan(.5)');
64 _cmp_between(0.463647, POSIX::2008::atan2(1.0, 2.0), 0.463648, 'atan2(1, 2)');
65 }
66
67 SKIP: {
68 if (! defined &POSIX::2008::cbrt) { skip 'cbrt() UNAVAILABLE', 3; }
69 cmp_ok(POSIX::2008::cbrt(8.0), '==', 2.0, 'cbrt(8) == 2');
70 _cmp_between(0.793700, POSIX::2008::cbrt(.5), 0.793701, 'cbrt(.5)');
71 }
72
73 SKIP: {
74 if (! defined &POSIX::2008::ceil) { skip 'ceil() UNAVAILABLE', 1; }
75 cmp_ok(POSIX::2008::ceil(3.141592653), '==', 4.0, 'ceil(pi) == 4');
76 }
77
78 SKIP: {
79 if (! defined &POSIX::2008::cos) { skip 'cos() UNAVAILABLE', 4; }
80 cmp_ok(POSIX::2008::cos(0.0), '==', 1.0, 'cos(0) == 1');
81 cmp_ok(POSIX::2008::cos(-.75), '==', POSIX::2008::cos(0.75), 'cos(-x) == cos(x)');
82 _cmp_between(0.877582, POSIX::2008::cos(.5), 0.877583, 'cos(.5)');
83 }
84
85 SKIP: {
86 if (! defined &POSIX::2008::cosh) { skip 'cosh() UNAVAILABLE', 4; }
87 cmp_ok(POSIX::2008::cosh(0.0), '==', 1.0, 'cosh(0) == 1');
88 cmp_ok(POSIX::2008::cosh(-.75), '==', POSIX::2008::cosh(0.75), 'cosh(-x) == cosh(x)');
89 _cmp_between(1.127625, POSIX::2008::cosh(.5), 1.127626, 'cosh(.5)');
90 }
91
92 SKIP: {
93 if (! defined &POSIX::2008::erf) { skip 'erf() UNAVAILABLE', 4; }
94 cmp_ok(POSIX::2008::erf(0.0), '==', 0.0, 'erf(0) == 0');
95 cmp_ok(POSIX::2008::erf(-.75), '==', -POSIX::2008::erf(0.75), 'erf(-x) == -erf(x)');
96 _cmp_between(0.520499, POSIX::2008::erf(.5), 0.520500, 'erf(.5)');
97 }
98
99 SKIP: {
100 if (! defined &POSIX::2008::erfc) { skip 'erfc() UNAVAILABLE', 3; }
101 cmp_ok(POSIX::2008::erfc(0.0), '==', 1.0, 'erfc(0) == 1');
102 _cmp_between(0.479500, POSIX::2008::erfc(.5), 0.479501, 'erfc(.5)');
103 }
104
105 SKIP: {
106 if (! defined &POSIX::2008::exp) { skip 'exp() UNAVAILABLE', 3; }
107 cmp_ok(POSIX::2008::exp(0.0), '==', 1.0, 'exp(0) == 1');
108 _cmp_between(1.648721, POSIX::2008::exp(.5), 1.648722, 'exp(.5)');
109 }
110
111 SKIP: {
112 if (! defined &POSIX::2008::exp2) { skip 'exp2() UNAVAILABLE', 4; }
113 cmp_ok(POSIX::2008::exp2(0.0), '==', 1.0, 'exp2(0) == 1');
114 cmp_ok(POSIX::2008::exp2(1.0), '==', 2.0, 'exp2(1) == 2');
115 _cmp_between(1.414213, POSIX::2008::exp2(.5), 1.414214, 'exp2(.5)');
116 }
117
118 SKIP: {
119 if (! defined &POSIX::2008::expm1) { skip 'expm1() UNAVAILABLE', 3; }
120 cmp_ok(POSIX::2008::expm1(0.0), '==', 0.0, 'expm1(0) == 0');
121 _cmp_between(0.648721, POSIX::2008::expm1(.5), 0.648722, 'expm1(.5)');
122 }
123
124 SKIP: {
125 if (! defined &POSIX::2008::fabs) { skip 'fabs() UNAVAILABLE', 3; }
126 cmp_ok(POSIX::2008::fabs(0.0), '==', 0.0, 'fabs(0) == 0');
127 cmp_ok(POSIX::2008::fabs(-0.2), '==', POSIX::2008::fabs(0.2), 'fabs(-.2) == fabs(.2)');
128 cmp_ok(POSIX::2008::fabs(-0.5), '==', 0.5, 'fabs(-.5) == .5');
129 }
130
131 SKIP: {
132 if (! defined &POSIX::2008::fdim) { skip 'fdim() UNAVAILABLE', 2; }
133 cmp_ok(POSIX::2008::fdim(73., 37.), '==', 36., 'fdim(73, 37) == 36');
134 cmp_ok(POSIX::2008::fdim(37., 73.), '==', 0., 'fdim(37, 73) == 0');
135 }
136
137 SKIP: {
138 if (! defined &POSIX::2008::floor) { skip 'floor() UNAVAILABLE', 2; }
139 cmp_ok(POSIX::2008::floor(3.141592653), '==', 3.0, 'floor(pi) == 3');
140 cmp_ok(POSIX::2008::floor(-3.141592653), '==', -4.0, 'floor(-pi) == -4');
141 }
142
143 SKIP: {
144 if (! defined &POSIX::2008::fma) { skip 'fma() UNAVAILABLE', 2; }
145 cmp_ok(POSIX::2008::fma(2., 3., 4.), '==', 10., 'fma(2, 3, 4) == 10');
146 cmp_ok(POSIX::2008::fma(2., -3., 4.), '==', -2., 'fma(2, -3, 4) == -2');
147 }
148
149 SKIP: {
150 if (! defined &POSIX::2008::fmax) { skip 'fmax() UNAVAILABLE', 2; }
151 cmp_ok(POSIX::2008::fmax(73., 37.), '==', 73., 'fmax(73, 37) == 73');
152 cmp_ok(POSIX::2008::fmax(-42., -21.), '==', -21., 'fmax(-42, -21) == -21');
153 }
154
155 SKIP: {
156 if (! defined &POSIX::2008::fmin) { skip 'fmin() UNAVAILABLE', 2; }
157 cmp_ok(POSIX::2008::fmin(73., 37.), '==', 37., 'fmin(73, 37) == 37');
158 cmp_ok(POSIX::2008::fmin(-42., -21.), '==', -42., 'fmin(-42, -21) == -42');
159 }
160
161 SKIP: {
162 if (! defined &POSIX::2008::fmod) { skip 'fmod() UNAVAILABLE', 1; }
163 cmp_ok(POSIX::2008::fmod(4.0, 1.75), '==', 0.5, 'fmod(4, 1.75) == 0.5');
164 }
165
166 SKIP: {
167 if (! defined &POSIX::2008::frexp) { skip 'frexp() UNAVAILABLE', 1; }
168 is_deeply([POSIX::2008::frexp(2.5)], [0.625, 2], 'frexp(2.5) == (0.625, 2)');
169 }
170
171 SKIP: {
172 if (! defined &POSIX::2008::hypot) { skip 'hypot() UNAVAILABLE', 1; }
173 cmp_ok(POSIX::2008::hypot(3.0, 4.0), '==', 5.0, 'hypot(3, 4) == 5');
174 }
175
176 SKIP: {
177 if (! defined &POSIX::2008::ilogb) { skip 'ilogb() UNAVAILABLE', 2; }
178 cmp_ok(POSIX::2008::ilogb(1023.), '==', 9, 'ilogb(1023) == 9');
179 cmp_ok(POSIX::2008::ilogb(1024.), '==', 10, 'ilogb(1024) == 10');
180 }
181
182 SKIP: {
183 if (! defined &POSIX::2008::j0) { skip 'j0() UNAVAILABLE', 3; }
184 cmp_ok(POSIX::2008::j0(0.), '==', 1., 'j0(0) == 1');
185 _cmp_between(0.765197, POSIX::2008::j0(1.), 0.765198, 'j0(1)');
186 }
187
188 SKIP: {
189 if (! defined &POSIX::2008::j1) { skip 'j1() UNAVAILABLE', 3; }
190 cmp_ok(POSIX::2008::j1(0.), '==', 0., 'j1(0) == 0');
191 _cmp_between(0.440050, POSIX::2008::j1(1.), 0.440051, 'j1(1)');
192 }
193
194 SKIP: {
195 if (! defined &POSIX::2008::jn) { skip 'jn() UNAVAILABLE', 4; }
196 cmp_ok(POSIX::2008::jn(0, 1.), '==', POSIX::2008::j0(1.), 'jn(0, 1) == j0(1)');
197 cmp_ok(POSIX::2008::jn(1, 1.), '==', POSIX::2008::j1(1.), 'jn(1, 1) == j1(1)');
198 _cmp_between(0.114903, POSIX::2008::jn(2, 1.), 0.114904, 'jn(2, 1)');
199 }
200
201 SKIP: {
202 if (! defined &POSIX::2008::ldexp) { skip 'ldexp() UNAVAILABLE', 2; }
203 cmp_ok(POSIX::2008::ldexp(3., 0), '==', 3., 'ldexp(3, 0) == 3');
204 cmp_ok(POSIX::2008::ldexp(3., 4), '==', 48., 'ldexp(3, 4) == 48');
205 }
206
207 SKIP: {
208 if (! defined &POSIX::2008::lgamma) { skip 'lgamma() UNAVAILABLE', 2; }
209 cmp_ok(POSIX::2008::lgamma(1.), '==', 0., 'lgamma(1) == 0');
210 cmp_ok(POSIX::2008::lgamma(2.), '==', 0., 'lgamma(2) == 0');
211 }
212
213 SKIP: {
214 if (! defined &POSIX::2008::log) { skip 'log() UNAVAILABLE', 3; }
215 cmp_ok(POSIX::2008::log(1.), '==', 0., 'log(1) == 0');
216 _cmp_between(1.098612, POSIX::2008::log(3.), 1.098613, 'log(3)');
217 }
218
219 SKIP: {
220 if (! defined &POSIX::2008::log10) { skip 'log10() UNAVAILABLE', 3; }
221 cmp_ok(POSIX::2008::log10(1.), '==', 0., 'log10(1) == 0');
222 _cmp_between(0.477121, POSIX::2008::log10(3.), 0.477122, 'log10(3)');
223 }
224
225 SKIP: {
226 if (! defined &POSIX::2008::log1p) { skip 'log1p() UNAVAILABLE', 3; }
227 cmp_ok(POSIX::2008::log1p(0.), '==', 0., 'log1p(0) == 0');
228 _cmp_between(1.098612, POSIX::2008::log1p(2.), 1.098613, 'log1p(3)');
229 }
230
231 SKIP: {
232 if (! defined &POSIX::2008::log2) { skip 'log2() UNAVAILABLE', 3; }
233 cmp_ok(POSIX::2008::log2(1.), '==', 0., 'log2(1) == 0');
234 _cmp_between(1.584962, POSIX::2008::log2(3.), 1.584963, 'log2(3)');
235 }
236
237 SKIP: {
238 if (! defined &POSIX::2008::logb) { skip 'logb() UNAVAILABLE', 2; }
239 cmp_ok(POSIX::2008::logb(1023.), '==', 9., 'logb(1023) == 9');
240 cmp_ok(POSIX::2008::logb(1024.), '==', 10., 'logb(1024) == 10');
241 }
242
243 SKIP: {
244 if (! defined &POSIX::2008::lrint) { skip 'lrint() UNAVAILABLE', 2; }
245 cmp_ok(POSIX::2008::lrint(.8), '==', 1., 'lrint(.8) == 1');
246 cmp_ok(POSIX::2008::lrint(1.2), '==', 1., 'lrint(1.2) == 1');
247 }
248
249 SKIP: {
250 if (! defined &POSIX::2008::lround) { skip 'lround() UNAVAILABLE', 2; }
251 cmp_ok(POSIX::2008::lround(.8), '==', 1, 'lround(.8) == 1');
252 cmp_ok(POSIX::2008::lround(1.2), '==', 1, 'lround(1.2) == 1');
253 }
254
255 SKIP: {
256 if (! defined &POSIX::2008::nearbyint) { skip 'nearbyint() UNAVAILABLE', 2; }
257 cmp_ok(POSIX::2008::nearbyint(.8), '==', 1., 'nearbyint(.8) == 1');
258 cmp_ok(POSIX::2008::nearbyint(1.2), '==', 1., 'nearbyint(1.2) == 1');
259 }
260
261 SKIP: {
262 if (! defined &POSIX::2008::nextafter) { skip 'nextafter() UNAVAILABLE', 4; }
263 _cmp_between(1.0, POSIX::2008::nextafter(1., 2.), 1.1, 'nextafter(1, 2)');
264 _cmp_between(0.9, POSIX::2008::nextafter(1., 0.), 1.0, 'nextafter(1, 2)');
265 }
266
267 SKIP: {
268 if (! defined &POSIX::2008::nexttoward) { skip 'nexttoward() UNAVAILABLE', 4; }
269 _cmp_between(1.0, POSIX::2008::nexttoward(1., 2.), 1.1, 'nexttoward(1, 2)');
270 _cmp_between(0.9, POSIX::2008::nexttoward(1., 0.), 1.0, 'nexttoward(1, 2)');
271 }
272
273 SKIP: {
274 if (! defined &POSIX::2008::pow) { skip 'pow() UNAVAILABLE', 4; }
275 cmp_ok(POSIX::2008::pow(0., 1.2), '==', 0., 'pow(0, 1.2) == 0');
276 cmp_ok(POSIX::2008::pow(1.2, 0.), '==', 1., 'pow(1.2, 0) == 1');
277 _cmp_between(1.157031, POSIX::2008::pow(1.2, 0.8), 1.157032, 'pow(1.2, 0.8)');
278 }
279
280 SKIP: {
281 if (! defined &POSIX::2008::remainder) { skip 'remainder() UNAVAILABLE', 1; }
282 cmp_ok(POSIX::2008::remainder(4.0, 1.75), '==', 0.5, 'remainder(4, 1.75) == 0.5');
283 }
284
285 SKIP: {
286 if (! defined &POSIX::2008::rint) { skip 'rint() UNAVAILABLE', 2; }
287 cmp_ok(POSIX::2008::rint(.8), '==', 1., 'rint(.8) == 1');
288 cmp_ok(POSIX::2008::rint(1.2), '==', 1., 'rint(1.2) == 1');
289 }
290
291 SKIP: {
292 if (! defined &POSIX::2008::round) { skip 'round() UNAVAILABLE', 2; }
293 cmp_ok(POSIX::2008::round(.8), '==', 1., 'round(.8) == 1');
294 cmp_ok(POSIX::2008::round(1.2), '==', 1., 'round(1.2) == 1');
295 }
296
297 SKIP: {
298 if (! defined &POSIX::2008::sin) { skip 'sin() UNAVAILABLE', 4; }
299 cmp_ok(POSIX::2008::sin(0.0), '==', 0.0, 'sin(0) == 0');
300 cmp_ok(POSIX::2008::sin(-.75), '==', -POSIX::2008::sin(.75), 'sin(-x) == -sin(x)');
301 _cmp_between(0.479425, POSIX::2008::sin(.5), 0.479426, 'sin(.5)');
302 }
303
304 SKIP: {
305 if (! defined &POSIX::2008::sinh) { skip 'sinh() UNAVAILABLE', 4; }
306 cmp_ok(POSIX::2008::sinh(0.0), '==', 0.0, 'sinh(0) == 0');
307 cmp_ok(POSIX::2008::sinh(-.75), '==', -POSIX::2008::sinh(.75), 'sinh(-x) == -sinh(x)');
308 _cmp_between(0.521095, POSIX::2008::sinh(.5), 0.521096, 'sinh(.5)');
309 }
310
311 SKIP: {
312 if (! defined &POSIX::2008::sqrt) { skip 'sqrt() UNAVAILABLE', 3; }
313 cmp_ok(POSIX::2008::sqrt(9.0), '==', 3.0, 'sqrt(9) == 3');
314 _cmp_between(0.707106, POSIX::2008::sqrt(.5), 0.707107, 'sqrt(.5)');
315 }
316
317 SKIP: {
318 if (! defined &POSIX::2008::tan) { skip 'tan() UNAVAILABLE', 4; }
319 cmp_ok(POSIX::2008::tan(0.0), '==', 0.0, 'tan(0) == 0');
320 cmp_ok(POSIX::2008::tan(-.75), '==', -POSIX::2008::tan(.75), 'tan(-x) == -tan(x)');
321 _cmp_between(0.546302, POSIX::2008::tan(.5), 0.546303, 'tan(.5)');
322 }
323
324 SKIP: {
325 if (! defined &POSIX::2008::tanh) { skip 'tanh() UNAVAILABLE', 4; }
326 cmp_ok(POSIX::2008::tanh(0.0), '==', 0.0, 'tanh(0) == 0');
327 cmp_ok(POSIX::2008::tanh(-.75), '==', -POSIX::2008::tanh(.75), 'tanh(-x) == -tanh(x)');
328 _cmp_between(0.462117, POSIX::2008::tanh(.5), 0.462118, 'tanh(.5)');
329 }
330
331 SKIP: {
332 if (! defined &POSIX::2008::tgamma) { skip 'tgamma() UNAVAILABLE', 3; }
333 _cmp_between(1.225416, POSIX::2008::tgamma(.75), 1.225417, 'tgamma(3/4)');
334 cmp_ok(POSIX::2008::tgamma(4.), '==', 6., 'tgamma(4) == 6');
335 }
336
337 SKIP: {
338 if (! defined &POSIX::2008::trunc) { skip 'trunc() UNAVAILABLE', 2; }
339 cmp_ok(POSIX::2008::trunc(3.141592653), '==', 3.0, 'trunc(pi) == 3');
340 cmp_ok(POSIX::2008::trunc(-3.141592653), '==', -3.0, 'trunc(-pi) == -3');
341 }
342
343 SKIP: {
344 if (! defined &POSIX::2008::y0) { skip 'y0() UNAVAILABLE', 2; }
345 _cmp_between(-0.444519, POSIX::2008::y0(0.5), -0.444518, 'y0(0.5)');
346 }
347
348 SKIP: {
349 if (! defined &POSIX::2008::y1) { skip 'y1() UNAVAILABLE', 2; }
350 _cmp_between(-1.471473, POSIX::2008::y1(0.5), -1.471472, 'y1(0.5)');
351 }
352
353 SKIP: {
354 if (! defined &POSIX::2008::yn) { skip 'yn() UNAVAILABLE', 4; }
355 cmp_ok(POSIX::2008::yn(0, .5), '==', POSIX::2008::y0(.5), 'yn(0, .5) == y0(.5)');
356 cmp_ok(POSIX::2008::yn(1, .5), '==', POSIX::2008::y1(.5), 'yn(1, .5) == y1(.5)');
357 _cmp_between(-5.441371, POSIX::2008::yn(2, .5), -5.441370, 'yn(2, .5)');
358 }