14 if ($^O eq 'aix' && $Config{uselongdouble}) {
15 # FWIW: NaN actually seems to be working decently,
16 # but Inf is completely broken (e.g. Inf + 0 -> NaN).
17 skip_all "$^O with long doubles does not have sane inf/nan";
22 my $NInf = "-Inf" + 0;
25 my @PInf = ("Inf", "inf", "INF", "+Inf",
26 "Infinity", "INFINITE",
28 my @NInf = map { "-$_" } grep { ! /^\+/ } @PInf;
30 my @NaN = ("NAN", "nan", "qnan", "SNAN", "NanQ", "NANS",
31 "1.#QNAN", "+1#SNAN", "-1.#NAN", "1#IND",
32 "NaN123", "NAN(123)", "nan%",
33 "nanonano"); # RIP, Robin Williams.
35 my @printf_fmt = qw(e f g a d u o i b x p);
36 my @packi_fmt = qw(c C s S l L i I n N v V j J w W U);
37 my @packf_fmt = qw(f d F);
38 my @packs_fmt = qw(a4 A4 Z5 b20 B20 h10 H10 u);
40 if ($Config{ivsize} == 8) {
41 push @packi_fmt, qw(q Q);
44 if ($Config{uselongdouble} && $Config{nvsize} > $Config{doublesize}) {
50 cmp_ok($PInf, '>', 0, "positive infinity");
51 cmp_ok($NInf, '<', 0, "negative infinity");
53 cmp_ok($PInf, '>', $NInf, "positive > negative");
54 cmp_ok($NInf, '==', -$PInf, "negative == -positive");
55 cmp_ok(-$NInf, '==', $PInf, "--negative == positive");
57 is($PInf, "Inf", "$PInf value stringifies as Inf");
58 is($NInf, "-Inf", "$NInf value stringifies as -Inf");
60 cmp_ok($PInf + 0, '==', $PInf, "+Inf + zero is +Inf");
61 cmp_ok($NInf + 0, '==', $NInf, "-Inf + zero is -Inf");
63 cmp_ok($PInf + 1, '==', $PInf, "+Inf + one is +Inf");
64 cmp_ok($NInf + 1, '==', $NInf, "-Inf + one is -Inf");
66 cmp_ok($PInf + $PInf, '==', $PInf, "+Inf + Inf is +Inf");
67 cmp_ok($NInf + $NInf, '==', $NInf, "-Inf - Inf is -Inf");
69 cmp_ok($PInf * 2, '==', $PInf, "twice Inf is Inf");
70 cmp_ok($PInf / 2, '==', $PInf, "half of Inf is Inf");
72 cmp_ok($PInf * $PInf, '==', $PInf, "-Inf * +Inf is +Inf");
73 cmp_ok($NInf * $NInf, '==', $PInf, "-Inf * -Inf is +Inf");
75 is(sprintf("%g", $PInf), "Inf", "$PInf sprintf %g is Inf");
76 is(sprintf("%a", $PInf), "Inf", "$PInf sprintf %a is Inf");
78 for my $f (@printf_fmt) {
79 is(sprintf("%$f", $PInf), "Inf", "$PInf sprintf %$f is Inf");
82 ok(!defined eval { $a = sprintf("%c", $PInf)}, "sprintf %c +Inf undef");
83 like($@, qr/Cannot printf/, "$PInf sprintf fails");
84 ok(!defined eval { $a = sprintf("%c", "Inf")},
85 "stringy sprintf %c +Inf undef");
86 like($@, qr/Cannot printf/, "stringy $PInf sprintf fails");
88 ok(!defined eval { $a = chr($PInf) }, "chr(+Inf) undef");
89 like($@, qr/Cannot chr/, "+Inf chr() fails");
90 ok(!defined eval { $a = chr("Inf") }, "chr(stringy +Inf) undef");
91 like($@, qr/Cannot chr/, "stringy +Inf chr() fails");
93 ok(!defined eval { $a = sprintf("%c", $NInf)}, "sprintf %c -Inf undef");
94 like($@, qr/Cannot printf/, "$NInf sprintf fails");
95 ok(!defined eval { $a = sprintf("%c", "-Inf")},
96 "sprintf %c stringy -Inf undef");
97 like($@, qr/Cannot printf/, "stringy $NInf sprintf fails");
99 ok(!defined eval { $a = chr($NInf) }, "chr(-Inf) undef");
100 like($@, qr/Cannot chr/, "-Inf chr() fails");
101 ok(!defined eval { $a = chr("-Inf") }, "chr(stringy -Inf) undef");
102 like($@, qr/Cannot chr/, "stringy -Inf chr() fails");
104 for my $f (@packi_fmt) {
105 ok(!defined eval { $a = pack($f, $PInf) }, "pack $f +Inf undef");
106 like($@, $f eq 'w' ? qr/Cannot compress Inf/: qr/Cannot pack Inf/,
107 "+Inf pack $f fails");
108 ok(!defined eval { $a = pack($f, "Inf") },
109 "pack $f stringy +Inf undef");
110 like($@, $f eq 'w' ? qr/Cannot compress Inf/: qr/Cannot pack Inf/,
111 "stringy +Inf pack $f fails");
112 ok(!defined eval { $a = pack($f, $NInf) }, "pack $f -Inf undef");
113 like($@, $f eq 'w' ? qr/Cannot compress -Inf/: qr/Cannot pack -Inf/,
114 "-Inf pack $f fails");
115 ok(!defined eval { $a = pack($f, "-Inf") },
116 "pack $f stringy -Inf undef");
117 like($@, $f eq 'w' ? qr/Cannot compress -Inf/: qr/Cannot pack -Inf/,
118 "stringy -Inf pack $f fails");
121 for my $f (@packf_fmt) {
122 ok(defined eval { $a = pack($f, $PInf) }, "pack $f +Inf defined");
123 eval { $b = unpack($f, $a) };
124 cmp_ok($b, '==', $PInf, "pack $f +Inf equals $PInf");
126 ok(defined eval { $a = pack($f, $NInf) }, "pack $f -Inf defined");
127 eval { $b = unpack($f, $a) };
128 cmp_ok($b, '==', $NInf, "pack $f -Inf equals $NInf");
131 for my $f (@packs_fmt) {
132 ok(defined eval { $a = pack($f, $PInf) }, "pack $f +Inf defined");
133 is($a, pack($f, "Inf"), "pack $f +Inf same as 'Inf'");
135 ok(defined eval { $a = pack($f, $NInf) }, "pack $f -Inf defined");
136 is($a, pack($f, "-Inf"), "pack $f -Inf same as 'Inf'");
139 is eval { unpack "p", pack 'p', $PInf }, "Inf", "pack p +Inf";
140 is eval { unpack "P3", pack 'P', $PInf }, "Inf", "pack P +Inf";
141 is eval { unpack "p", pack 'p', $NInf }, "-Inf", "pack p -Inf";
142 is eval { unpack "P4", pack 'P', $NInf }, "-Inf", "pack P -Inf";
145 cmp_ok($i + 0 , '==', $PInf, "$i is +Inf");
146 cmp_ok($i, '>', 0, "$i is positive");
147 is("@{[$i+0]}", "Inf", "$i value stringifies as Inf");
151 cmp_ok($i + 0, '==', $NInf, "$i is -Inf");
152 cmp_ok($i, '<', 0, "$i is negative");
153 is("@{[$i+0]}", "-Inf", "$i value stringifies as -Inf");
156 is($PInf + $PInf, $PInf, "+Inf plus +Inf is +Inf");
157 is($NInf + $NInf, $NInf, "-Inf plus -Inf is -Inf");
159 is(1/$PInf, 0, "one per +Inf is zero");
160 is(1/$NInf, 0, "one per -Inf is zero");
162 my ($PInfPP, $PInfMM) = ($PInf, $PInf);
163 my ($NInfPP, $NInfMM) = ($NInf, $NInf);;
168 is($PInfPP, $PInf, "+Inf++ is +Inf");
169 is($PInfMM, $PInf, "+Inf-- is +Inf");
170 is($NInfPP, $NInf, "-Inf++ is -Inf");
171 is($NInfMM, $NInf, "-Inf-- is -Inf");
173 ok($PInf, "+Inf is true");
174 ok($NInf, "-Inf is true");
176 is(abs($PInf), $PInf, "abs(+Inf) is +Inf");
177 is(abs($NInf), $PInf, "abs(-Inf) is +Inf");
179 # One could argue of NaN as the result.
180 is(int($PInf), $PInf, "int(+Inf) is +Inf");
181 is(int($NInf), $NInf, "int(-Inf) is -Inf");
183 is(sqrt($PInf), $PInf, "sqrt(+Inf) is +Inf");
184 # sqrt $NInf doesn't work because negative is caught
186 is(exp($PInf), $PInf, "exp(+Inf) is +Inf");
187 is(exp($NInf), 0, "exp(-Inf) is zero");
191 skip "if +Inf == 0 cannot log(+Inf)", 1;
193 is(log($PInf), $PInf, "log(+Inf) is +Inf");
195 # log $NInf doesn't work because negative is caught
197 is(rand($PInf), $PInf, "rand(+Inf) is +Inf");
198 is(rand($NInf), $NInf, "rand(-Inf) is -Inf");
200 # XXX Bit operations?
204 # ~+Inf == 0? or NaN?
210 # Or just declare insanity and die?
213 my $here = "$^O $Config{osvers}";
214 if ($here =~ /^hpux 10/) {
215 skip "$here: pow doesn't generate Inf", 1;
217 is(9**9**9, $PInf, "9**9**9 is Inf");
221 my @FInf = qw(Info Infiniti Infinityz);
222 if ($Config{usequadmath}) {
223 skip "quadmath strtoflt128() accepts false infinities", scalar @FInf;
225 # Silence "isn't numeric in addition", that's kind of the point.
228 cmp_ok("$i" + 0, '==', 0, "false infinity $i");
234 cmp_ok($NaN, '!=', $NaN, "NaN is NaN numerically (by not being NaN)");
235 ok($NaN eq $NaN, "NaN is NaN stringifically");
237 is("$NaN", "NaN", "$NaN value stringifies as NaN");
239 is("+NaN" + 0, "NaN", "+NaN is NaN");
240 is("-NaN" + 0, "NaN", "-NaN is NaN");
242 is($NaN + 0, $NaN, "NaN + zero is NaN");
244 is($NaN + 1, $NaN, "NaN + one is NaN");
246 is($NaN * 2, $NaN, "twice NaN is NaN");
247 is($NaN / 2, $NaN, "half of NaN is NaN");
249 is($NaN * $NaN, $NaN, "NaN * NaN is NaN");
250 is($NaN / $NaN, $NaN, "NaN / NaN is NaN");
252 for my $f (@printf_fmt) {
253 is(sprintf("%$f", $NaN), "NaN", "$NaN sprintf %$f is NaN");
256 ok(!defined eval { $a = sprintf("%c", $NaN)}, "sprintf %c NaN undef");
257 like($@, qr/Cannot printf/, "$NaN sprintf fails");
258 ok(!defined eval { $a = sprintf("%c", "NaN")},
259 "sprintf %c stringy NaN undef");
260 like($@, qr/Cannot printf/, "stringy $NaN sprintf fails");
262 ok(!defined eval { $a = chr($NaN) }, "chr NaN undef");
263 like($@, qr/Cannot chr/, "NaN chr() fails");
264 ok(!defined eval { $a = chr("NaN") }, "chr stringy NaN undef");
265 like($@, qr/Cannot chr/, "stringy NaN chr() fails");
267 for my $f (@packi_fmt) {
268 ok(!defined eval { $a = pack($f, $NaN) }, "pack $f NaN undef");
269 like($@, $f eq 'w' ? qr/Cannot compress NaN/: qr/Cannot pack NaN/,
270 "NaN pack $f fails");
271 ok(!defined eval { $a = pack($f, "NaN") },
272 "pack $f stringy NaN undef");
273 like($@, $f eq 'w' ? qr/Cannot compress NaN/: qr/Cannot pack NaN/,
274 "stringy NaN pack $f fails");
277 for my $f (@packf_fmt) {
278 ok(defined eval { $a = pack($f, $NaN) }, "pack $f NaN defined");
279 eval { $b = unpack($f, $a) };
280 cmp_ok($b, '!=', $b, "pack $f NaN not-equals $NaN");
283 for my $f (@packs_fmt) {
284 ok(defined eval { $a = pack($f, $NaN) }, "pack $f NaN defined");
285 is($a, pack($f, "NaN"), "pack $f NaN same as 'NaN'");
288 is eval { unpack "p", pack 'p', $NaN }, "NaN", "pack p +NaN";
289 is eval { unpack "P3", pack 'P', $NaN }, "NaN", "pack P +NaN";
292 cmp_ok($i + 0, '!=', $i + 0, "$i is NaN numerically (by not being NaN)");
293 is("@{[$i+0]}", "NaN", "$i value stringifies as NaN");
296 ok(!($NaN < 0), "NaN is not lt zero");
297 ok(!($NaN == 0), "NaN is not == zero");
298 ok(!($NaN > 0), "NaN is not gt zero");
300 ok(!($NaN < $NaN), "NaN is not lt NaN");
301 ok(!($NaN > $NaN), "NaN is not gt NaN");
303 # is() okay with $NaN because it uses eq.
304 is($NaN * 0, $NaN, "NaN times zero is NaN");
305 is($NaN * 2, $NaN, "NaN times two is NaN");
307 my ($NaNPP, $NaNMM) = ($NaN, $NaN);
310 is($NaNPP, $NaN, "+NaN++ is NaN");
311 is($NaNMM, $NaN, "+NaN-- is NaN");
313 # You might find this surprising (isn't NaN kind of like of undef?)
314 # but this is how it is.
315 ok($NaN, "NaN is true");
317 is(abs($NaN), $NaN, "abs(NaN) is NaN");
318 is(int($NaN), $NaN, "int(NaN) is NaN");
319 is(sqrt($NaN), $NaN, "sqrt(NaN) is NaN");
320 is(exp($NaN), $NaN, "exp(NaN) is NaN");
324 skip "if +NaN == 0 cannot log(+NaN)", 1;
326 is(log($NaN), $NaN, "log(NaN) is NaN");
329 is(sin($NaN), $NaN, "sin(NaN) is NaN");
330 is(rand($NaN), $NaN, "rand(NaN) is NaN");
333 my $here = "$^O $Config{osvers}";
334 if ($here =~ /^hpux 10/) {
335 skip "$here: pow doesn't generate Inf, so sin(Inf) won't happen", 1;
337 is(sin(9**9**9), $NaN, "sin(9**9**9) is NaN");
341 my @FNaN = qw(NaX XNAN Ind Inx);
342 # Silence "isn't numeric in addition", that's kind of the point.
345 cmp_ok("$i" + 0, '==', 0, "false nan $i");
349 # === Tests combining Inf and NaN ===
351 # is() okay with $NaN because it uses eq.
352 is($PInf * 0, $NaN, "Inf times zero is NaN");
353 is($PInf * $NaN, $NaN, "Inf times NaN is NaN");
354 is($PInf + $NaN, $NaN, "Inf plus NaN is NaN");
355 is($PInf - $PInf, $NaN, "Inf minus inf is NaN");
356 is($PInf / $PInf, $NaN, "Inf div inf is NaN");
357 is($PInf % $PInf, $NaN, "Inf mod inf is NaN");
359 ok(!($NaN < $PInf), "NaN is not lt +Inf");
360 ok(!($NaN == $PInf), "NaN is not eq +Inf");
361 ok(!($NaN > $PInf), "NaN is not gt +Inf");
363 ok(!($NaN < $NInf), "NaN is not lt -Inf");
364 ok(!($NaN == $NInf), "NaN is not eq -Inf");
365 ok(!($NaN > $NInf), "NaN is not gt -Inf");
367 is(sin($PInf), $NaN, "sin(+Inf) is NaN");
369 # === Overflows and Underflows ===
371 # 1e9999 (and 1e-9999) are large (and small) enough for even
372 # IEEE quadruple precision (magnitude 10**4932, and 10**-4932).
374 cmp_ok(1e9999, '==', $PInf, "overflow to +Inf (compile time)");
375 cmp_ok('1e9999', '==', $PInf, "overflow to +Inf (runtime)");
376 cmp_ok(-1e9999, '==', $NInf, "overflow to -Inf (compile time)");
377 cmp_ok('-1e9999', '==', $NInf, "overflow to -Inf (runtime)");
378 cmp_ok(1e-9999, '==', 0, "underflow to 0 (compile time) from pos");
379 cmp_ok('1e-9999', '==', 0, "underflow to 0 (runtime) from pos");
380 cmp_ok(-1e-9999, '==', 0, "underflow to 0 (compile time) from neg");
381 cmp_ok('-1e-9999', '==', 0, "underflow to 0 (runtime) from neg");