This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Thwart IRIX long doubles and sloppy pow() (in Perl, **);
[perl5.git] / t / op / pow.t
CommitLineData
58d76dfd
JH
1#!./perl -w
2# Now they'll be wanting biff! and zap! tests too.
3
4BEGIN {
5 chdir 't' if -d 't';
6 @INC = '../lib';
7 require './test.pl';
8}
9
10# This calcualtion ought to be within 0.001 of the right answer.
11my $bits_in_uv = int (0.001 + log (~0+1) / log 2);
12
13# 3**30 < 2**48, don't trust things outside that range on a Cray
14# Likewise other 3 should not overflow 48 bits if I did my sums right.
15my @pow = ([3,30,1e-14], [4,32,0], [5,20,1e-14], [2.5, 10,,1e-14], [-2, 69,0]);
16my $tests;
17$tests += $_->[1] foreach @pow;
18
19plan tests => 1 + $bits_in_uv + $tests;
20
21# Ought to be 32, 64, 36 or something like that.
22
23my $remainder = $bits_in_uv & 3;
24
25cmp_ok ($remainder, '==', 0, 'Sanity check bits in UV calculation')
26 or printf "# ~0 is %d (0x%d) which gives $bits_in_uv bits\n", ~0, ~0;
27
28# These are a lot of brute force tests to see how accurate $m ** $n is.
29# Unfortunately rather a lot of perl programs expect 2 ** $n to be integer
30# perfect, forgetting that it's a call to floating point pow() which never
31# claims to deliver perfection.
32foreach my $n (0..$bits_in_uv - 1) {
33 my $exp = 2 ** $n;
34 my $int = 1 << $n;
35 cmp_ok ($exp, '==', $int, "2 ** $n vs 1 << $n");
36}
37
38foreach my $pow (@pow) {
39 my ($base, $max, $range) = @$pow;
40 my $fp = 1;
41 foreach my $n (0..$max-1) {
42 my $exp = $base ** $n;
43 within ($exp, $fp, $range, "$base ** $n [$exp] vs $base * $base * ...");
44 $fp *= $base;
45 }
46}