xref: /openbsd-src/gnu/usr.bin/perl/ext/POSIX/t/math.t (revision d59bb9942320b767f2a19aaa7690c8c6e30b724c)
1#!perl -w
2
3use strict;
4
5use POSIX ':math_h_c99';
6use POSIX ':nan_payload';
7use POSIX 'lround';
8use Test::More;
9
10use Config;
11
12# These tests are mainly to make sure that these arithmetic functions
13# exist and are accessible.  They are not meant to be an exhaustive
14# test for the interface.
15
16sub between {
17    my ($low, $have, $high, $desc) = @_;
18    local $Test::Builder::Level = $Test::Builder::Level + 1;
19
20    cmp_ok($have, '>=', $low, $desc);
21    cmp_ok($have, '<=', $high, $desc);
22}
23
24is(acos(1), 0, "Basic acos(1) test");
25between(3.14, acos(-1), 3.15, 'acos(-1)');
26between(1.57, acos(0), 1.58, 'acos(0)');
27is(asin(0), 0, "Basic asin(0) test");
28cmp_ok(asin(1), '>', 1.57, "Basic asin(1) test");
29cmp_ok(asin(-1), '<', -1.57, "Basic asin(-1) test");
30cmp_ok(asin(1), '==', -asin(-1), 'asin(1) == -asin(-1)');
31is(atan(0), 0, "Basic atan(0) test");
32between(0.785, atan(1), 0.786, 'atan(1)');
33between(-0.786, atan(-1), -0.785, 'atan(-1)');
34cmp_ok(atan(1), '==', -atan(-1), 'atan(1) == -atan(-1)');
35is(cosh(0), 1, "Basic cosh(0) test");
36between(1.54, cosh(1), 1.55, 'cosh(1)');
37between(1.54, cosh(-1), 1.55, 'cosh(-1)');
38is(cosh(1), cosh(-1), 'cosh(1) == cosh(-1)');
39is(floor(1.23441242), 1, "Basic floor(1.23441242) test");
40is(floor(-1.23441242), -2, "Basic floor(-1.23441242) test");
41is(fmod(3.5, 2.0), 1.5, "Basic fmod(3.5, 2.0) test");
42is(join(" ", frexp(1)), "0.5 1",  "Basic frexp(1) test");
43is(ldexp(0,1), 0, "Basic ldexp(0,1) test");
44is(log10(1), 0, "Basic log10(1) test");
45is(log10(10), 1, "Basic log10(10) test");
46is(join(" ", modf(1.76)), "0.76 1", "Basic modf(1.76) test");
47is(sinh(0), 0, "Basic sinh(0) test");
48between(1.17, sinh(1), 1.18, 'sinh(1)');
49between(-1.18, sinh(-1), -1.17, 'sinh(-1)');
50is(tan(0), 0, "Basic tan(0) test");
51between(1.55, tan(1), 1.56, 'tan(1)');
52between(1.55, tan(1), 1.56, 'tan(-1)');
53cmp_ok(tan(1), '==', -tan(-1), 'tan(1) == -tan(-1)');
54is(tanh(0), 0, "Basic tanh(0) test");
55between(0.76, tanh(1), 0.77, 'tanh(1)');
56between(-0.77, tanh(-1), -0.76, 'tanh(-1)');
57cmp_ok(tanh(1), '==', -tanh(-1), 'tanh(1) == -tanh(-1)');
58
59SKIP: {
60    skip "no fpclassify", 4 unless $Config{d_fpclassify};
61    is(fpclassify(1), FP_NORMAL, "fpclassify 1");
62    is(fpclassify(0), FP_ZERO, "fpclassify 0");
63    is(fpclassify(INFINITY), FP_INFINITE, "fpclassify INFINITY");
64    is(fpclassify(NAN), FP_NAN, "fpclassify NAN");
65}
66
67sub near {
68    my ($got, $want, $msg, $eps) = @_;
69    $eps ||= 1e-6;
70    cmp_ok(abs($got - $want), '<', $eps, $msg);
71}
72
73SKIP: {
74    unless ($Config{d_acosh}) {
75        skip "no acosh, suspecting no C99 math";
76    }
77    if ($^O =~ /Win32|VMS/) {
78        skip "running in $^O, C99 math support uneven";
79    }
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);
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");
95    is(hypot(3, 4), 5, "hypot 3 4");
96    near(hypot(-2, 1), sqrt(5), "hypot -1 2", 1e-9);
97    is(ilogb(255), 7, "ilogb 255");
98    is(ilogb(256), 8, "ilogb 256");
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");
110    cmp_ok(nan(), '!=', nan(), 'nan');
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);
114    is(signbit(2), 0, "signbit 2"); # zero
115    ok(signbit(-2), "signbit -2"); # non-zero
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(lround(-2.75), -3, "lround -0.25");
123    is(signbit(lround(-0.25)), 0, "lround -0.25 -> +0"); # unlike round()
124    is(trunc(2.25), 2, "trunc 2.25");
125    is(trunc(-2.25), -2, "trunc -2.25");
126    is(trunc(2.5), 2, "trunc 2.5");
127    is(trunc(-2.5), -2, "trunc -2.5");
128    is(trunc(2.75), 2, "trunc 2.75");
129    is(trunc(-2.75), -2, "trunc -2.75");
130    ok(isless(1, 2), "isless 1 2");
131    ok(!isless(2, 1), "isless 2 1");
132    ok(!isless(1, 1), "isless 1 1");
133    ok(!isless(1, NaN), "isless 1 NaN");
134    ok(isgreater(2, 1), "isgreater 2 1");
135    ok(islessequal(1, 1), "islessequal 1 1");
136    ok(isunordered(1, NaN), "isunordered 1 NaN");
137
138    near(erf(0.5), 0.520499877813047, "erf 0.5", 1.5e-7);
139    near(erf(1), 0.842700792949715, "erf 1", 1.5e-7);
140    near(erf(9), 1, "erf 9", 1.5e-7);
141    near(erfc(0.5), 0.479500122186953, "erfc 0.5", 1.5e-7);
142    near(erfc(1), 0.157299207050285, "erfc 1", 1.5e-7);
143    near(erfc(9), 0, "erfc 9", 1.5e-7);
144
145    # tgamma(n) = (n - 1)!
146    # lgamma(n) = log(tgamma(n))
147    near(tgamma(5), 24, "tgamma 5", 1.5e-7);
148    near(tgamma(5.5), 52.3427777845535, "tgamma 5.5", 1.5e-7);
149    near(tgamma(9), 40320, "tgamma 9", 1.5e-7);
150    near(lgamma(5), 3.17805383034795, "lgamma 4", 1.5e-7);
151    near(lgamma(5.5), 3.95781396761872, "lgamma 5.5", 1.5e-7);
152    near(lgamma(9), 10.6046029027452, "lgamma 9", 1.5e-7);
153
154    # These don't work on old mips/hppa platforms because == Inf (or == -Inf).
155    # ok(isnan(setpayload(0)), "setpayload zero");
156    # is(getpayload(setpayload(0)), 0, "setpayload + getpayload (zero)");
157    #
158    # These don't work on most platforms because == Inf (or == -Inf).
159    # ok(isnan(setpayloadsig(0)), "setpayload zero");
160    # is(getpayload(setpayloadsig(0)), 0, "setpayload + getpayload (zero)");
161
162    # Verify that the payload set be setpayload()
163    # (1) still is a nan
164    # (2) but the payload can be retrieved
165    # (3) but is not signaling
166    my $x = 0;
167    setpayload($x, 0x12345);
168    ok(isnan($x), "setpayload + isnan");
169    is(getpayload($x), 0x12345, "setpayload + getpayload");
170    ok(!issignaling($x), "setpayload + issignaling");
171
172    # Verify that the signaling payload set be setpayloadsig()
173    # (1) still is a nan
174    # (2) but the payload can be retrieved
175    # (3) and is signaling
176    setpayloadsig($x, 0x12345);
177    ok(isnan($x), "setpayloadsig + isnan");
178    is(getpayload($x), 0x12345, "setpayloadsig + getpayload");
179  SKIP: {
180      # https://rt.perl.org/Ticket/Display.html?id=125710
181      # In the 32-bit x86 ABI cannot preserve the signaling bit
182      # (the x87 simply does not preserve that).  But using the
183      # 80-bit extended format aka long double, the bit is preserved.
184      # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57484
185      my $could_be_x86_32 =
186          # This is a really weak test: there are other 32-bit
187          # little-endian platforms than just Intel (some embedded
188          # processors, for example), but we use this just for not
189          # bothering with the test if things look iffy.
190          # We could, say, $Config{ccsymbols} =~ /\b__[xi][3-7]86=1\b/,
191          # but that feels quite shaky.
192          $Config{byteorder} =~ /1234/ &&
193          $Config{longdblkind} == 3 &&
194          $Config{ptrsize} == 4;
195      skip($^O, 1) if $could_be_x86_32 && !$Config{uselongdouble};
196      ok(issignaling($x), "setpayloadsig + issignaling");
197    }
198
199    # Try a payload more than one byte.
200    is(getpayload(nan(0x12345)), 0x12345, "nan + getpayload");
201
202    # Try payloads of 2^k, most importantly at and beyond 2^32.  These
203    # tests will fail if NV is just 32-bit float, but that Should Not
204    # Happen (tm).
205    is(getpayload(nan(2**31)), 2**31, "nan + getpayload 2**31");
206    is(getpayload(nan(2**32)), 2**32, "nan + getpayload 2**32");
207    is(getpayload(nan(2**33)), 2**33, "nan + getpayload 2**33");
208
209    # Payloads just lower than 2^k.
210    is(getpayload(nan(2**31-1)), 2**31-1, "nan + getpayload 2**31-1");
211    is(getpayload(nan(2**32-1)), 2**32-1, "nan + getpayload 2**32-1");
212
213    # Payloads not divisible by two (and larger than 2**32).
214
215    SKIP: {
216        # solaris gets 10460353202 from getpayload() when it should
217        # get 10460353203 (the 3**21). Things go wrong already in
218        # the nan() payload setting: [0x2, 0x6f7c52b4] (ivsize=4)
219        # instead [0x2, 0x6f7c52b3].  Then at getpayload() things
220        # go wrong again, now in other direction: with the (wrong)
221        # [0x2, 0x6f7c52b4] encoded in the nan we should decode into
222        # 10460353204, but we get 10460353202.  It doesn't seem to
223        # help even if we use 'unsigned long long' instead of UV/U32
224        # in the POSIX.xs:S_setpayload/S_getpayload.
225        #
226        # casting bug?  fmod() bug?  Though also broken with
227        # -Duselongdouble + fmodl(), so maybe Solaris cc bug
228        # in general?
229        #
230        # Ironically, the large prime seems to work even in Solaris,
231        # probably just by blind luck.
232        skip($^O, 1) if $^O eq 'solaris';
233        is(getpayload(nan(3**21)), 3**21, "nan + getpayload 3**21");
234    }
235    is(getpayload(nan(4294967311)), 4294967311, "nan + getpayload prime");
236
237    # Truncates towards zero.
238    is(getpayload(nan(1234.567)), 1234, "nan (trunc) + getpayload");
239
240    # Not signaling.
241    ok(!issignaling(0), "issignaling zero");
242    ok(!issignaling(+Inf), "issignaling +Inf");
243    ok(!issignaling(-Inf), "issignaling -Inf");
244    ok(!issignaling(NaN), "issignaling NaN");
245} # SKIP
246
247done_testing();
248