| 1 | package # hide from PAUSE | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | sanity::BaseCalc; | ||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | our $AUTHORITY = 'cpan:BBYRD'; # AUTHORITY | |||||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | our $VERSION = '1.03'; # VERSION | |||||||||||||||||||||||||||||||||||||||||||||
| 6 | # ABSTRACT: DO NOT USE! | ||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Math::BigInt 1.78; # 1.78 = round_mode => common | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Math::BigFloat; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | # configure some basic big number stuff | ||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 3 | Math::BigInt ->config({ | |||||||||||||||||||||||||||||||||||||||||||||
| 15 | upgrade => 'Math::BigFloat', | ||||||||||||||||||||||||||||||||||||||||||||||
| 16 | round_mode => 'common', | ||||||||||||||||||||||||||||||||||||||||||||||
| 17 | trap_nan => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 18 | trap_inf => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 19 | }); | ||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 3 | Math::BigFloat->config({ | |||||||||||||||||||||||||||||||||||||||||||||
| 21 | round_mode => 'common', | ||||||||||||||||||||||||||||||||||||||||||||||
| 22 | trap_nan => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 23 | trap_inf => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 24 | }); | ||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 4 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 4 | my ($pack, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 28 | 4 | my $self = bless {}, $pack; | |||||||||||||||||||||||||||||||||||||||||||||
| 29 | 4 | ▶ | $self->{neg_char} = $opts{neg_char} || '-'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$opts{'neg_char'} || '-'
MC/DC0%0/1
$opts{'neg_char'} || '-'
$opts{'neg_char'}
Truth table50%1/2
A
A= $opts{'neg_char'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 4 | ▶ | $self->{radix_char} = $opts{radix_char} || '.'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$opts{'radix_char'} || '.'
MC/DC0%0/1
$opts{'radix_char'} || '.'
$opts{'radix_char'}
Truth table50%1/2
A
A= $opts{'radix_char'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 4 | ▶ | $opts{digits} = $_[1] if (@_ == 2); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 4 | $self->digits($opts{digits}); | |||||||||||||||||||||||||||||||||||||||||||||
| 33 | 4 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 4 | ▶ | sub digits { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
digits: called CC=6 SCAR=19.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | return @{$self->{digits}} unless (@_); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||
| 40 | # Set the value | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 4 | ▶ | if (ref $_[0] eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 4 | $self->{digits} = [ @{ shift() } ]; | |||||||||||||||||||||||||||||||||||||||||||||
| 43 | 4 | delete $self->{digitset_name}; | |||||||||||||||||||||||||||||||||||||||||||||
| 44 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | my $name = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | my %digitsets = $self->_digitsets; | |||||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | croak "Unrecognized digit set '$name'" unless exists $digitsets{$name}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | $self->{digits} = $digitsets{$name}; | |||||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | $self->{digitset_name} = $name; | |||||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 4 | ▶ | $self->{neg_char} = '' if (grep { $_ eq $self->{neg_char} } @{$self->{digits}}); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 4 | ▶ | $self->{radix_char} = '' if (grep { $_ eq $self->{radix_char} } @{$self->{digits}}); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 4 | $self->{digit_strength} = log(scalar @{$self->{digits}}) / log(10); | |||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||
| 55 | # Build the translation table back to numbers | ||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 4 | delete $self->{trans}; | |||||||||||||||||||||||||||||||||||||||||||||
| 57 | 4 | @{$self->{trans}}{@{$self->{digits}}} = 0..$#{$self->{digits}}; | |||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 4 | return @{$self->{digits}}; | |||||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | sub _digitsets { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_digitsets: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | return ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | 'bin' => [0,1], | |||||||||||||||||||||||||||||||||||||||||||||
| 66 | 'hex' => [0..9,'a'..'f'], | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 'HEX' => [0..9,'A'..'F'], | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 'oct' => [0..7], | ||||||||||||||||||||||||||||||||||||||||||||||
| 69 | '64' => ['A'..'Z','a'..'z',0..9,'+','/'], | ||||||||||||||||||||||||||||||||||||||||||||||
| 70 | '62' => [0..9,'a'..'z','A'..'Z'], | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | sub from_base { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
from_base: not called CC=16 SCAR=56.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | my ($self, $str) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | my ($nc, $fc) = @$self{qw(neg_char radix_char)}; | |||||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | return $self->_from_accurate_return( Math::BigFloat->new( $self->from_base($str) )->bneg() ) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$nc and $str =~ s/^\Q$nc\E//
MC/DC0%0/2
$nc and $str =~ s/^\Q$nc\E//
$nc
$str =~ s/^\Q$nc\E//
Truth table0%0/3
A and B
A= $ncB= $str =~ s/^\Q$nc\E//
| |||||||||||||||||||||||||||||||||||||||||||||||
| 78 | if $nc && $str =~ s/^\Q$nc\E//; # Handle negative numbers | ||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||
| 80 | # number clean up + decimal checks | ||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | my $base = @{$self->{digits}}; | |||||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | my $zero = $self->{digits}[0]; | |||||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | my $is_dec = ($fc && $str =~ /\Q$fc\E/); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$fc && $str =~ /\Q$fc\E/
MC/DC0%0/2
$fc && $str =~ /\Q$fc\E/
$fc
$str =~ /\Q$fc\E/
Truth table0%0/3
A && B
A= $fcB= $str =~ /\Q$fc\E/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | $str =~ s/^\Q$zero\E+//; | |||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | $str =~ s/\Q$zero\E+$// if ($is_dec); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||
| 87 | # num of digits + big number support | ||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | my $poten_digits = int(length($str) * $self->{digit_strength}) + 16; | |||||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | Math::BigFloat->accuracy($poten_digits + 16); | |||||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | my $result = Math::BigFloat->new(0); | |||||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | $result = $result->as_int() unless $is_dec; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | # short-circuits | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | unless ($is_dec || !$self->{digitset_name}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$is_dec or not $self->{"digitset_name"}
MC/DC0%0/2
$is_dec or not $self->{"digitset_name"}
$is_dec
not $self->{"digitset_name"}
Truth table0%0/3
A or not(B)
A= $is_decB= not $self->{"digitset_name"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | $result = $result->from_hex(lc "0x$str") if ($self->{digitset_name} =~ /^hex$/i); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | $result = $result->from_bin( "0b$str") if ($self->{digitset_name} eq 'bin'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | $result = $result->from_oct(lc "0$str") if ($self->{digitset_name} eq 'oct'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | if ($result == 0) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 101 | # num of digits (power) | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | my $i = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | $i = length($str)- 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 104 | # decimal digits (yes, this removes the radix point, but $i captures the "digit location" information.) | ||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | $i = length($1) - 1 if ($fc && $str =~ s/^(.*)\Q$fc\E(.*)$/$1$2/); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$fc and $str =~ s/^(.*)\Q$fc\E(.*)$/$1$2/
MC/DC0%0/2
$fc and $str =~ s/^(.*)\Q$fc\E(.*)$/$1$2/
$fc
$str =~ s/^(.*)\Q$fc\E(.*)$/$1$2/
Truth table0%0/3
A and B
A= $fcB= $str =~ s/^(.*)\Q$fc\E(.*)$/$1$2/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | while ( $str =~ s/^(.)// ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | my $v = $self->{trans}{$1}; | |||||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | croak "Invalid character $1 in string!" unless defined $v; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | my $exp = Math::BigInt->new($base); | |||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | $result = $exp->bpow($i)->bmul($v)->badd($result); | |||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | $i--; # may go into the negative for non-ints | |||||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | return $self->_from_accurate_return($result); | |||||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | sub _from_accurate_return { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_from_accurate_return: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | my ($self, $result) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||
| 123 | # never lose the accuracy | ||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | my $rscalar = $result->numify(); | |||||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | my $rstring = $result->bstr(); | |||||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | $rstring =~ s/0+$// if ($rstring =~ /\./); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | $rstring =~ s/\.$//; # float to whole number | |||||||||||||||||||||||||||||||||||||||||||||
| 128 | # (the user can choose to put the string in a Math object if s/he so wishes) | ||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | return $rstring eq ($rscalar + 0 . '') ? $result->numify() : $rstring; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | sub to_base { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
to_base: not called CC=20 SCAR=60.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | my ($self, $num) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||||||
| 135 | # decimal checks | ||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | my $base = scalar @{$self->{digits}}; | |||||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | my $is_dec = ($num =~ /\./) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | $is_dec = 0 unless $self->{radix_char}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | my $zero = $self->{digits}[0]; | |||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||
| 141 | # num of digits + big number support | ||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | my $poten_digits = length($num); | |||||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | Math::BigFloat->accuracy($poten_digits + 16); | |||||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | $num = Math::BigFloat->new($num); | |||||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | $num = $num->as_int() unless $is_dec && $self->{radix_char}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$is_dec and $self->{"radix_char"}
MC/DC0%0/2
$is_dec and $self->{"radix_char"}
$is_dec
$self->{"radix_char"}
Truth table0%0/3
A and B
A= $is_decB= $self->{"radix_char"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||||||
| 147 | # (hold off on this check until after the big number support) | ||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | ▶ | return $self->{neg_char}.$self->to_base( $num->bneg ) if $num < 0; # Handle negative numbers | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||
| 150 | # short-circuits | ||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | return $zero if ($num == 0); # this confuses log, so let's just get rid of this quick | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | unless ($is_dec || !$self->{digitset_name}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$is_dec or not $self->{"digitset_name"}
MC/DC0%0/2
$is_dec or not $self->{"digitset_name"}
$is_dec
not $self->{"digitset_name"}
Truth table0%0/3
A or not(B)
A= $is_decB= not $self->{"digitset_name"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | return substr(lc $num->as_hex(), 2) if ($self->{digitset_name} eq 'hex'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | ▶ | return substr(uc $num->as_hex(), 2) if ($self->{digitset_name} eq 'HEX'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | return substr( $num->as_bin(), 2) if ($self->{digitset_name} eq 'bin'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | ▶ | return substr( $num->as_oct(), 1) if ($self->{digitset_name} eq 'oct'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||
| 159 | # get the largest power of Z (the highest digit) | ||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | my $i = $num->copy()->blog( | |||||||||||||||||||||||||||||||||||||||||||||
| 161 | $base, | ||||||||||||||||||||||||||||||||||||||||||||||
| 162 | int($num->length() / 9) + 2 # (an accuracy that is a little over the potential # of integer digits within log) | ||||||||||||||||||||||||||||||||||||||||||||||
| 163 | )->bfloor()->numify(); | ||||||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | my $result = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 166 | # BigFloat's accuracy should counter this, but the $i check is | ||||||||||||||||||||||||||||||||||||||||||||||
| 167 | # to make sure we don't get into an irrational/cyclic number loop | ||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | ▶ | while (($num != 0 || $i >= 0) && $i > -1024) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$num != 0 || $i >= 0 and $i > -1024
$num != 0 or $i >= 0
MC/DC0%0/3
$num != 0 || $i >= 0 and $i > -1024
$num != 0
$i >= 0
$i > -1024
Truth table0%0/5
A or B and C
A= $num != 0B= $i >= 0C= $i > -1024
| |||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | my $exp = Math::BigFloat->new($base); | |||||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | ▶ | $exp = $i < 0 ? $exp->bpow($i) : $exp->as_int->bpow($i); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | my $v = $num->copy()->bdiv($exp)->bfloor(); | |||||||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | $num -= $v * $exp; # this method is safer for fractionals | |||||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | ▶ | $result .= $self->{radix_char} if ($i == -1); # decimal point | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | $result .= $self->{digits}[$v]; | |||||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | $i--; # may go into the negative for non-ints | |||||||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||||||
| 180 | # Final cleanup | ||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | ▶ | return $zero unless length $result; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | $result =~ s/^\Q$zero\E+//; | |||||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | ▶ | $result =~ s/\Q$zero\E+$// if ($is_dec); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | return $result; | |||||||||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||
| 191 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||