This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Remove vmsish pragma from one-liners in Test::Simple's exit.t.
[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: {
249502ae 58 unless ($Config{d_acosh}) {
46a884f4 59 skip "no acosh, suspecting no C99 math", 30;
1a917639 60 }
bfce4ab3 61 if ($^O =~ /Win32|VMS/) {
46a884f4 62 skip "running in $^O, C99 math support uneven", 30;
bfce4ab3 63 }
46a884f4
JH
64 cmp_ok(abs(M_SQRT2 - 1.4142135623731), '<', 1e-9, "M_SQRT2");
65 cmp_ok(abs(M_E - 2.71828182845905), '<', 1e-9, "M_E");
39b5f1c4 66 cmp_ok(abs(M_PI - 3.14159265358979), '<', 1e-9, "M_PI");
e64e4e04 67 cmp_ok(abs(acosh(2) - 1.31695789692482), '<', 1e-9, "acosh");
39b5f1c4 68 cmp_ok(abs(asinh(1) - 0.881373587019543), '<', 1e-9, "asinh");
e64e4e04 69 cmp_ok(abs(atanh(0.5) - 0.549306144334055), '<', 1e-9, "atanh");
39b5f1c4
JH
70 cmp_ok(abs(cbrt(8) - 2), '<', 1e-9, "cbrt");
71 cmp_ok(abs(cbrt(-27) - -3), '<', 1e-9, "cbrt");
e0972548 72 cmp_ok(abs(copysign(3.14, -2) - -3.14), '<', 1e-9, "copysign");
39b5f1c4
JH
73 cmp_ok(abs(expm1(2) - 6.38905609893065), '<', 1e-9, "expm1");
74 cmp_ok(abs(expm1(1e-6) - 1.00000050000017e-06), '<', 1e-9, "expm1");
75 is(fdim(12, 34), 0, "fdim 12 34");
76 is(fdim(34, 12), 22, "fdim 34 12");
77 is(fmax(12, 34), 34, "fmax 12 34");
78 is(fmin(12, 34), 12, "fmin 12 34");
249502ae 79 SKIP: {
249502ae
JH
80 is(fpclassify(1), FP_NORMAL, "fpclassify 1");
81 is(fpclassify(0), FP_ZERO, "fpclassify 0");
5716d070 82 is(fpclassify(INFINITY), FP_INFINITE, "fpclassify INFINITY");
249502ae 83 is(fpclassify(NAN), FP_NAN, "fpclassify NAN");
1a917639 84 }
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");
249502ae 89 SKIP: {
0d71ffa7 90 skip "no isfinite", 3;
d867902f
JH
91 ok(isfinite(1), "isfinite 1");
92 ok(!isfinite(Inf), "isfinite Inf");
93 ok(!isfinite(NaN), "isfinite NaN");
1a917639 94 }
249502ae 95 SKIP: {
d867902f
JH
96 ok(isinf(INFINITY), "isinf INFINITY");
97 ok(isinf(Inf), "isinf Inf");
98 ok(!isinf(NaN), "isinf NaN");
99 ok(!isinf(42), "isinf 42");
1a917639 100 }
249502ae 101 SKIP: {
d867902f
JH
102 ok(isnan(NAN), "isnan NAN");
103 ok(isnan(NaN), "isnan NaN");
104 ok(!isnan(Inf), "isnan Inf");
105 ok(!isnan(42), "isnan Inf");
249502ae 106 }
e64e4e04 107 cmp_ok(nan(), '!=', nan(), 'nan');
39b5f1c4
JH
108 cmp_ok(abs(log1p(2) - 1.09861228866811), '<', 1e-9, "log1p");
109 cmp_ok(abs(log1p(1e-6) - 9.99999500000333e-07), '<', 1e-9, "log1p");
110 cmp_ok(abs(log2(8) - 3), '<', 1e-9, "log2");
249502ae 111 SKIP: {
5716d070
JH
112 is(signbit(2), 0, "signbit 2"); # zero
113 ok(signbit(-2), "signbit -2"); # non-zero
1a917639 114 }
249502ae
JH
115 is(round(2.25), 2, "round 2.25");
116 is(round(-2.25), -2, "round -2.25");
117 is(round(2.5), 3, "round 2.5");
118 is(round(-2.5), -3, "round -2.5");
119 is(round(2.75), 3, "round 2.75");
120 is(round(-2.75), -3, "round 2.75");
121 is(trunc(2.25), 2, "trunc 2.25");
122 is(trunc(-2.25), -2, "trunc -2.25");
123 is(trunc(2.5), 2, "trunc 2.5");
124 is(trunc(-2.5), -2, "trunc -2.5");
125 is(trunc(2.75), 2, "trunc 2.75");
126 is(trunc(-2.75), -2, "trunc -2.75");
3e2e323f
JH
127 ok(isless(1, 2), "isless 1 2");
128 ok(!isless(2, 1), "isless 2 1");
129 ok(!isless(1, 1), "isless 1 1");
130 ok(!isless(1, NaN), "isless 1 NaN");
131 ok(isgreater(2, 1), "isgreater 2 1");
132 ok(islessequal(1, 1), "islessequal 1 1");
133 ok(isunordered(1, NaN), "isunordered 1 NaN");
cc06cdb1 134 cmp_ok(abs(erf(1) - 0.842700792949715), '<', 1.5e-7, "erf 1");
ffbadafb 135 cmp_ok(abs(erfc(1) - 0.157299207050285), '<', 1.5e-7, "erfc 1");
2e9cdb62
JH
136 cmp_ok(abs(tgamma(9) - 40320), '<', 1.5e-7, "tgamma 9");
137 cmp_ok(abs(lgamma(9) - 10.6046029027452), '<', 1.5e-7, "lgamma 9");
1a917639 138}
1a917639 139
1a77755a 140done_testing();