This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
The skip count was all wrong.
[perl5.git] / ext / POSIX / t / math.t
CommitLineData
867bef19
SP
1#!perl -w
2
3use strict;
4
5use POSIX;
1a77755a 6use Test::More;
867bef19 7
1a917639
JH
8use Config;
9
b7b1e41b 10# These tests are mainly to make sure that these arithmetic functions
867bef19
SP
11# exist and are accessible. They are not meant to be an exhaustive
12# test for the interface.
13
1a77755a
NC
14sub between {
15 my ($low, $have, $high, $desc) = @_;
16 local $Test::Builder::Level = $Test::Builder::Level + 1;
17
18 cmp_ok($have, '>=', $low, $desc);
19 cmp_ok($have, '<=', $high, $desc);
20}
21
867bef19 22is(acos(1), 0, "Basic acos(1) test");
1a77755a
NC
23between(3.14, acos(-1), 3.15, 'acos(-1)');
24between(1.57, acos(0), 1.58, 'acos(0)');
867bef19 25is(asin(0), 0, "Basic asin(0) test");
1a77755a
NC
26cmp_ok(asin(1), '>', 1.57, "Basic asin(1) test");
27cmp_ok(asin(-1), '<', -1.57, "Basic asin(-1) test");
28cmp_ok(asin(1), '==', -asin(-1), 'asin(1) == -asin(-1)');
867bef19 29is(atan(0), 0, "Basic atan(0) test");
1a77755a
NC
30between(0.785, atan(1), 0.786, 'atan(1)');
31between(-0.786, atan(-1), -0.785, 'atan(-1)');
32cmp_ok(atan(1), '==', -atan(-1), 'atan(1) == -atan(-1)');
867bef19 33is(cosh(0), 1, "Basic cosh(0) test");
1a77755a
NC
34between(1.54, cosh(1), 1.55, 'cosh(1)');
35between(1.54, cosh(-1), 1.55, 'cosh(-1)');
36is(cosh(1), cosh(-1), 'cosh(1) == cosh(-1)');
867bef19 37is(floor(1.23441242), 1, "Basic floor(1.23441242) test");
1a77755a 38is(floor(-1.23441242), -2, "Basic floor(-1.23441242) test");
867bef19
SP
39is(fmod(3.5, 2.0), 1.5, "Basic fmod(3.5, 2.0) test");
40is(join(" ", frexp(1)), "0.5 1", "Basic frexp(1) test");
41is(ldexp(0,1), 0, "Basic ldexp(0,1) test");
42is(log10(1), 0, "Basic log10(1) test");
43is(log10(10), 1, "Basic log10(10) test");
44is(join(" ", modf(1.76)), "0.76 1", "Basic modf(1.76) test");
45is(sinh(0), 0, "Basic sinh(0) test");
1a77755a
NC
46between(1.17, sinh(1), 1.18, 'sinh(1)');
47between(-1.18, sinh(-1), -1.17, 'sinh(-1)');
867bef19 48is(tan(0), 0, "Basic tan(0) test");
1a77755a
NC
49between(1.55, tan(1), 1.56, 'tan(1)');
50between(1.55, tan(1), 1.56, 'tan(-1)');
51cmp_ok(tan(1), '==', -tan(-1), 'tan(1) == -tan(-1)');
867bef19 52is(tanh(0), 0, "Basic tanh(0) test");
1a77755a
NC
53between(0.76, tanh(1), 0.77, 'tanh(1)');
54between(-0.77, tanh(-1), -0.76, 'tanh(-1)');
55cmp_ok(tanh(1), '==', -tanh(-1), 'tanh(1) == -tanh(-1)');
56
1a917639 57SKIP: {
43ce44e9
JH
58 my $C99_SKIP = 63;
59
249502ae 60 unless ($Config{d_acosh}) {
43ce44e9 61 skip "no acosh, suspecting no C99 math", $C99_SKIP;
1a917639 62 }
bfce4ab3 63 if ($^O =~ /Win32|VMS/) {
43ce44e9 64 skip "running in $^O, C99 math support uneven", $C99_SKIP;
bfce4ab3 65 }
46a884f4
JH
66 cmp_ok(abs(M_SQRT2 - 1.4142135623731), '<', 1e-9, "M_SQRT2");
67 cmp_ok(abs(M_E - 2.71828182845905), '<', 1e-9, "M_E");
39b5f1c4 68 cmp_ok(abs(M_PI - 3.14159265358979), '<', 1e-9, "M_PI");
e64e4e04 69 cmp_ok(abs(acosh(2) - 1.31695789692482), '<', 1e-9, "acosh");
39b5f1c4 70 cmp_ok(abs(asinh(1) - 0.881373587019543), '<', 1e-9, "asinh");
e64e4e04 71 cmp_ok(abs(atanh(0.5) - 0.549306144334055), '<', 1e-9, "atanh");
39b5f1c4
JH
72 cmp_ok(abs(cbrt(8) - 2), '<', 1e-9, "cbrt");
73 cmp_ok(abs(cbrt(-27) - -3), '<', 1e-9, "cbrt");
e0972548 74 cmp_ok(abs(copysign(3.14, -2) - -3.14), '<', 1e-9, "copysign");
39b5f1c4
JH
75 cmp_ok(abs(expm1(2) - 6.38905609893065), '<', 1e-9, "expm1");
76 cmp_ok(abs(expm1(1e-6) - 1.00000050000017e-06), '<', 1e-9, "expm1");
77 is(fdim(12, 34), 0, "fdim 12 34");
78 is(fdim(34, 12), 22, "fdim 34 12");
79 is(fmax(12, 34), 34, "fmax 12 34");
80 is(fmin(12, 34), 12, "fmin 12 34");
f0589851
JH
81 is(fpclassify(1), FP_NORMAL, "fpclassify 1");
82 is(fpclassify(0), FP_ZERO, "fpclassify 0");
83 is(fpclassify(INFINITY), FP_INFINITE, "fpclassify INFINITY");
84 is(fpclassify(NAN), FP_NAN, "fpclassify NAN");
39b5f1c4 85 is(hypot(3, 4), 5, "hypot 3 4");
e0972548 86 cmp_ok(abs(hypot(-2, 1) - sqrt(5)), '<', 1e-9, "hypot -1 2");
39b5f1c4
JH
87 is(ilogb(255), 7, "ilogb 255");
88 is(ilogb(256), 8, "ilogb 256");
f0589851
JH
89 ok(isfinite(1), "isfinite 1");
90 ok(!isfinite(Inf), "isfinite Inf");
91 ok(!isfinite(NaN), "isfinite NaN");
92 ok(isinf(INFINITY), "isinf INFINITY");
93 ok(isinf(Inf), "isinf Inf");
94 ok(!isinf(NaN), "isinf NaN");
95 ok(!isinf(42), "isinf 42");
96 ok(isnan(NAN), "isnan NAN");
97 ok(isnan(NaN), "isnan NaN");
98 ok(!isnan(Inf), "isnan Inf");
99 ok(!isnan(42), "isnan Inf");
e64e4e04 100 cmp_ok(nan(), '!=', nan(), 'nan');
39b5f1c4
JH
101 cmp_ok(abs(log1p(2) - 1.09861228866811), '<', 1e-9, "log1p");
102 cmp_ok(abs(log1p(1e-6) - 9.99999500000333e-07), '<', 1e-9, "log1p");
103 cmp_ok(abs(log2(8) - 3), '<', 1e-9, "log2");
f0589851
JH
104 is(signbit(2), 0, "signbit 2"); # zero
105 ok(signbit(-2), "signbit -2"); # non-zero
249502ae
JH
106 is(round(2.25), 2, "round 2.25");
107 is(round(-2.25), -2, "round -2.25");
108 is(round(2.5), 3, "round 2.5");
109 is(round(-2.5), -3, "round -2.5");
110 is(round(2.75), 3, "round 2.75");
111 is(round(-2.75), -3, "round 2.75");
112 is(trunc(2.25), 2, "trunc 2.25");
113 is(trunc(-2.25), -2, "trunc -2.25");
114 is(trunc(2.5), 2, "trunc 2.5");
115 is(trunc(-2.5), -2, "trunc -2.5");
116 is(trunc(2.75), 2, "trunc 2.75");
117 is(trunc(-2.75), -2, "trunc -2.75");
3e2e323f
JH
118 ok(isless(1, 2), "isless 1 2");
119 ok(!isless(2, 1), "isless 2 1");
120 ok(!isless(1, 1), "isless 1 1");
121 ok(!isless(1, NaN), "isless 1 NaN");
122 ok(isgreater(2, 1), "isgreater 2 1");
123 ok(islessequal(1, 1), "islessequal 1 1");
124 ok(isunordered(1, NaN), "isunordered 1 NaN");
cc06cdb1 125 cmp_ok(abs(erf(1) - 0.842700792949715), '<', 1.5e-7, "erf 1");
ffbadafb 126 cmp_ok(abs(erfc(1) - 0.157299207050285), '<', 1.5e-7, "erfc 1");
2e9cdb62
JH
127 cmp_ok(abs(tgamma(9) - 40320), '<', 1.5e-7, "tgamma 9");
128 cmp_ok(abs(lgamma(9) - 10.6046029027452), '<', 1.5e-7, "lgamma 9");
43ce44e9
JH
129
130 # If adding more tests here, update also the $C99_SKIP
131 # at the beginning of this SKIP block.
132} # SKIP
1a917639 133
1a77755a 134done_testing();