JE/Object/Number/maxvalue.pl untested

filter: Statementstmt 0.0% 0 / 86 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 127 CC 5 SCAR 23.0
CC
5
Cov
0%
mult_pow2
20.0
pow2
6.0
BEGIN
2.0
CRAP 10.0
1 # This is basically Data::Float 0.009 with everything stripped out that is
2 # not needed.
3  
4 # From Data::Float 0.009:
5 # Copyright (C) 2006, 2007, 2008 Andrew Main (Zefram) <zefram fysh.org>
6  
7 use warnings;
8 use strict;
9  
10 package JE::Object::Number::_maxvalue;
11  
12 use Carp 'croak';
13  
14 #
15 # mult_pow2() multiplies a specified value by a specified power of two.
16 # This is done using repeated multiplication, and can cope with cases
17 # where the power of two cannot be directly represented as a floating
18 # point value. (E.g., 0x1.b2p-900 can be multiplied by 2^1500 to get
19 # to 0x1.b2p+600; the input and output values can be represented in
20 # IEEE double, but 2^1500 cannot.) Overflow and underflow can occur.
21 #
22 # @powtwo is an array such that powtwo[i] = 2^2^i. Its elements are
23 # used in the repeated multiplication in mult_pow2. Similarly,
24 # @powhalf is such that powhalf[i] = 2^-2^i. Reading the exponent
25 # in binary indicates which elements of @powtwo/@powhalf to multiply
26 # by, except that it may indicate elements that don't exist, either
27 # because they're not representable or because the arrays haven't
28 # been filled yet. mult_pow2() will use the last element of the array
29 # repeatedly in this case. Thus array elements after the first are
30 # only an optimisation, and do not change behaviour.
31 #
32  
33 my @powtwo = (2.0);
34 my @powhalf = (0.5);
35  
36 sub mult_pow2($$) {
37 my($value, $exp) = @_;
38 return $_[0] if $value == 0.0;
39 my $powa = \@powtwo;
40 if($exp < 0) {
41 $powa = \@powhalf;
42 $exp = -$exp;
43 }
44 for(my $i = 0; $i != $#$powa && $exp != 0; $i++) {
45 $value *= $powa->[$i] if $exp & 1;
46 $exp >>= 1;
47 }
48 $value *= $powa->[-1] while $exp--;
49 return $value;
50 }
51  
52 #
53 # Range of finite exponent values.
54 #
55  
56 my $min_finite_exp;
57 my $max_finite_exp;
58 my $max_finite_pow2;
59 my $min_finite;
60  
61 my @directions = (
62 {
63 expsign => -1,
64 powa => \@powhalf,
65 xexp => \$min_finite_exp,
66 xpower => \$min_finite,
67 },
68 {
69 expsign => +1,
70 powa => \@powtwo,
71 xexp => \$max_finite_exp,
72 xpower => \$max_finite_pow2,
73 },
74 );
75  
76 while(!$directions[0]->{done} || !$directions[1]->{done}) {
77 foreach my $direction (@directions) {
78 next if $direction->{done};
79 my $lastpow = $direction->{powa}->[-1];
80 my $nextpow = $lastpow * $lastpow;
81 unless(mult_pow2($nextpow, -$direction->{expsign} *
82 (1 << (@{$direction->{powa}} - 1)))
83 == $lastpow) {
84 $direction->{done} = 1;
85 next;
86 }
87 push @{$direction->{powa}}, $nextpow;
88 }
89 }
90  
91 foreach my $direction (@directions) {
92 my $expsign = $direction->{expsign};
93 my $xexp = 1 << (@{$direction->{powa}} - 1);
94 my $extremum = $direction->{powa}->[-1];
95 for(my $addexp = $xexp; $addexp >>= 1; ) {
96 my $nx = mult_pow2($extremum, $expsign*$addexp);
97 if(mult_pow2($nx, -$expsign*$addexp) == $extremum) {
98 $xexp += $addexp;
99 $extremum = $nx;
100 }
101 }
102 ${$direction->{xexp}} = $expsign * $xexp;
103 ${$direction->{xpower}} = $extremum;
104 }
105  
106 #
107 # pow2() generates a power of two from scratch. It complains if given
108 # an exponent that would make an unrepresentable value.
109 #
110  
111 sub pow2($) {
112 my($exp) = @_;
113 croak "exponent $exp out of range [$min_finite_exp, $max_finite_exp]"
114 unless $exp >= $min_finite_exp && $exp <= $max_finite_exp;
115 return mult_pow2(1.0, $exp);
116 }
117  
118 #
119 # Significand size.
120 #
121  
122 my($significand_bits, $significand_step);
123 {
124 my $i;
125 for($i = 1; ; $i++) {
126 my $tryeps = $powhalf[$i];
127 last unless (1.0 + $tryeps) - 1.0 == $tryeps;
128 }
129 $i--;
130 $significand_bits = 1 << $i;
131 $significand_step = $powhalf[$i];
132 while($i--) {
133 my $tryeps = $significand_step * $powhalf[$i];
134 if((1.0 + $tryeps) - 1.0 == $tryeps) {
135 $significand_bits += 1 << $i;
136 $significand_step = $tryeps;
137 }
138 }
139 }
140  
141 my $max_finite = $max_finite_pow2 -
142 pow2($max_finite_exp - $significand_bits - 1);
143 $max_finite += $max_finite;
144  
145 $JE::Object::Number::max_finite = $max_finite;
146 $JE::Object::Number::min_finite = $min_finite;
147  
148 package main; # Is it safe to delete a package from inside it?
149  
150 require Symbol;
151 Symbol::delete_package('JE::Object::Number::_maxvalue');