This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
infnan: Notes on the nan payload.
[perl5.git] / ext / POSIX / t / math.t
CommitLineData
867bef19
SP
1#!perl -w
2
3use strict;
4
7658eeca 5use POSIX ':math_h_c99';
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
fa17b3a6
AP
57SKIP: {
58 skip "no fpclassify", 4 unless $Config{d_fpclassify};
59 is(fpclassify(1), FP_NORMAL, "fpclassify 1");
60 is(fpclassify(0), FP_ZERO, "fpclassify 0");
61 is(fpclassify(INFINITY), FP_INFINITE, "fpclassify INFINITY");
62 is(fpclassify(NAN), FP_NAN, "fpclassify NAN");
63}
64
8732b8db
JH
65sub near {
66 my ($got, $want, $msg, $eps) = @_;
67 $eps ||= 1e-6;
68 cmp_ok(abs($got - $want), '<', $eps, $msg);
69}
70
1a917639 71SKIP: {
fa17b3a6 72 my $C99_SKIP = 59;
43ce44e9 73
249502ae 74 unless ($Config{d_acosh}) {
43ce44e9 75 skip "no acosh, suspecting no C99 math", $C99_SKIP;
1a917639 76 }
bfce4ab3 77 if ($^O =~ /Win32|VMS/) {
43ce44e9 78 skip "running in $^O, C99 math support uneven", $C99_SKIP;
bfce4ab3 79 }
8732b8db
JH
80 near(M_SQRT2, 1.4142135623731, "M_SQRT2", 1e-9);
81 near(M_E, 2.71828182845905, "M_E", 1e-9);
82 near(M_PI, 3.14159265358979, "M_PI", 1e-9);
83 near(acosh(2), 1.31695789692482, "acosh", 1e-9);
84 near(asinh(1), 0.881373587019543, "asinh", 1e-9);
85 near(atanh(0.5), 0.549306144334055, "atanh", 1e-9);
86 near(cbrt(8), 2, "cbrt", 1e-9);
87 near(cbrt(-27), -3, "cbrt", 1e-9);
88 near(copysign(3.14, -2), -3.14, "copysign", 1e-9);
89 near(expm1(2), 6.38905609893065, "expm1", 1e-9);
90 near(expm1(1e-6), 1.00000050000017e-06, "expm1", 1e-9);
39b5f1c4
JH
91 is(fdim(12, 34), 0, "fdim 12 34");
92 is(fdim(34, 12), 22, "fdim 34 12");
93 is(fmax(12, 34), 34, "fmax 12 34");
94 is(fmin(12, 34), 12, "fmin 12 34");
39b5f1c4 95 is(hypot(3, 4), 5, "hypot 3 4");
8732b8db 96 near(hypot(-2, 1), sqrt(5), "hypot -1 2", 1e-9);
39b5f1c4
JH
97 is(ilogb(255), 7, "ilogb 255");
98 is(ilogb(256), 8, "ilogb 256");
f0589851
JH
99 ok(isfinite(1), "isfinite 1");
100 ok(!isfinite(Inf), "isfinite Inf");
101 ok(!isfinite(NaN), "isfinite NaN");
102 ok(isinf(INFINITY), "isinf INFINITY");
103 ok(isinf(Inf), "isinf Inf");
104 ok(!isinf(NaN), "isinf NaN");
105 ok(!isinf(42), "isinf 42");
106 ok(isnan(NAN), "isnan NAN");
107 ok(isnan(NaN), "isnan NaN");
108 ok(!isnan(Inf), "isnan Inf");
109 ok(!isnan(42), "isnan Inf");
e64e4e04 110 cmp_ok(nan(), '!=', nan(), 'nan');
8732b8db
JH
111 near(log1p(2), 1.09861228866811, "log1p", 1e-9);
112 near(log1p(1e-6), 9.99999500000333e-07, "log1p", 1e-9);
113 near(log2(8), 3, "log2", 1e-9);
f0589851
JH
114 is(signbit(2), 0, "signbit 2"); # zero
115 ok(signbit(-2), "signbit -2"); # non-zero
249502ae
JH
116 is(round(2.25), 2, "round 2.25");
117 is(round(-2.25), -2, "round -2.25");
118 is(round(2.5), 3, "round 2.5");
119 is(round(-2.5), -3, "round -2.5");
120 is(round(2.75), 3, "round 2.75");
121 is(round(-2.75), -3, "round 2.75");
122 is(trunc(2.25), 2, "trunc 2.25");
123 is(trunc(-2.25), -2, "trunc -2.25");
124 is(trunc(2.5), 2, "trunc 2.5");
125 is(trunc(-2.5), -2, "trunc -2.5");
126 is(trunc(2.75), 2, "trunc 2.75");
127 is(trunc(-2.75), -2, "trunc -2.75");
3e2e323f
JH
128 ok(isless(1, 2), "isless 1 2");
129 ok(!isless(2, 1), "isless 2 1");
130 ok(!isless(1, 1), "isless 1 1");
131 ok(!isless(1, NaN), "isless 1 NaN");
132 ok(isgreater(2, 1), "isgreater 2 1");
133 ok(islessequal(1, 1), "islessequal 1 1");
134 ok(isunordered(1, NaN), "isunordered 1 NaN");
8732b8db
JH
135 near(erf(1), 0.842700792949715, "erf 1", 1.5e-7);
136 near(erfc(1), 0.157299207050285, "erfc 1", 1.5e-7);
137 near(tgamma(9), 40320, "tgamma 9", 1.5e-7);
138 near(lgamma(9), 10.6046029027452, "lgamma 9", 1.5e-7);
43ce44e9
JH
139
140 # If adding more tests here, update also the $C99_SKIP
141 # at the beginning of this SKIP block.
142} # SKIP
1a917639 143
1a77755a 144done_testing();