blib/lib/Zuzu/Module/Math/BigNum.pm

filter: Statementstmt 28.4% 33 / 116 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 13.8% 5 / 36 Podpod 0.0% 0 / 1 totaltotal 16.3% 38 / 233 CC 36 SCAR 41.0
CC
36
Cov
18%
__ANON__
210.0
_coerce_bignum
110.0
__ANON__
20.0
CRAP 60.6
« lib/Zuzu/Tidy.pm ↑ Summary blib/lib/Zuzu/Module/Worker.pm »
1 package Zuzu::Module::Math::BigNum;
2  
3 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 our $VERSION = '0.007002';
6  
7 2 ▶use Math::BigFloat;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Math::BigInt;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Zuzu::Util::NativeHelpers qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
11 native_class
12 native_function
13 native_object
14 2 );
15  
16 0 ▶sub _coerce_bignum {
Subroutine _coerce_bignum: not called CC=10 SCAR=47.0
17 0 my ( $value ) = @_;
18  
19 0 ▶ if ( ref($value) and ref($value) eq 'Math::BigFloat' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value and ref $value eq "Math::BigFloat") 0 0 0
Condition0%0/3
ref $value and ref $value eq "Math::BigFloat"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $value and ref $value eq "Math::BigFloat"
ref $value ref $value eq "Math::BigFloat"
Truth table0%0/3
A and B
A= ref $valueB= ref $value eq "Math::BigFloat"
ABresult
0X0
100
111
20 0 return $value->copy;
21 }
22 0 ▶ if ( ref($value)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value and $value->isa("Zuzu::Value::Object") and $value->class->name eq "BigNum") 0 0 0
Condition0%0/6
ref $value and $value->isa("Zuzu::Value::Object") and $value->class->name eq "BigNum"
!ll&&!rl&&r
000
ref $value and $value->isa("Zuzu::Value::Object")
!ll&&!rl&&r
000
MC/DC0%0/3
ref $value and $value->isa("Zuzu::Value::Object") and $value->class->name eq "BigNum"
ref $value $value->isa("Zuzu::Value::Object") $value->class->name eq "BigNum"
Truth table0%0/4
A and B and C
A= ref $valueB= $value->isa("Zuzu::Value::Object")C= $value->class->name eq "BigNum"
ABCresult
0XX0
10X0
1100
1111
23 and $value->isa('Zuzu::Value::Object')
24 and $value->class->name eq 'BigNum'
25 ) {
26 0 my $raw = $value->slots->{__value};
27 0 ▶ return $raw->copy if ref($raw) and ref($raw) eq 'Math::BigFloat';
Branch: 1 branch
BranchTrueFalseTotal
if ref $raw and ref $raw eq "Math::BigFloat" 0 0 0
Condition0%0/3
ref $raw and ref $raw eq "Math::BigFloat"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $raw and ref $raw eq "Math::BigFloat"
ref $raw ref $raw eq "Math::BigFloat"
Truth table0%0/3
A and B
A= ref $rawB= ref $raw eq "Math::BigFloat"
ABresult
0X0
100
111
28 0 ▶ return Math::BigFloat->new( defined $raw ? "$raw" : '0' );
Branch: 1 branch
BranchTrueFalseTotal
defined $raw ? : 0 0 0
29 }
30  
31 0 ▶ return Math::BigFloat->new( defined $value ? "$value" : '0' );
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 0 0 0
32 }
33  
34 0 ▶sub _wrap {
Subroutine _wrap: not called CC=1 SCAR=6.9
35 0 my ( $class_obj, $value ) = @_;
36  
37 0 return native_object(
38 class => $class_obj,
39 slots => {
40 __value => _coerce_bignum( $value ),
41 },
42 const => {
43 __value => 1,
44 },
45 );
46 }
47  
48 0 ▶sub _self_value {
Subroutine _self_value: not called CC=1 SCAR=6.9
49 0 my ( $self ) = @_;
50 0 return _coerce_bignum( $self->slots->{__value} );
51 }
52  
53 0 ▶sub _to_plain_decimal {
Subroutine _to_plain_decimal: not called CC=2 SCAR=17.9
54 0 my ( $value ) = @_;
55 0 my $text = $value->bstr;
56  
57 0 ▶ if ( $text =~ /e/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /e/i) 0 0 0
58 0 $text = $value->copy->bnormalize->bstr;
59 }
60  
61 0 return $text;
62 }
63  
64 0 ▶sub _to_zuzu_string {
Subroutine _to_zuzu_string: not called CC=1 SCAR=6.9
65 0 my ( $value ) = @_;
66 0 my $text = 's' . _to_plain_decimal( $value );
67 0 substr( $text, 0, 1, '' );
68 0 return $text;
69 }
70  
71 2 ▶sub IMPORT {
Subroutine IMPORT: called CC=4 SCAR=26.3
Pod undocumented
72 2 my ( $class, $runtime ) = @_;
73  
74 2 my $bignum_class = native_class(
75 name => 'BigNum',
76 );
77  
78 $bignum_class->static_methods->{from_dec} = native_function(
79 name => 'from_dec',
80 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
81 0 my ( $klass, $raw ) = @_;
82 0 ▶ return _wrap( $klass, Math::BigFloat->new( defined $raw ? "$raw" : '0' ) );
Branch: 1 branch
BranchTrueFalseTotal
defined $raw ? : 0 0 0
83 },
84 2 );
85  
86 $bignum_class->static_methods->{from_hex} = native_function(
87 name => 'from_hex',
88 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
89 0 my ( $klass, $raw ) = @_;
90 0 ▶ my $text = defined $raw ? "$raw" : '0x0';
Branch: 1 branch
BranchTrueFalseTotal
defined $raw ? : 0 0 0
91 0 return _wrap( $klass, Math::BigFloat->from_hex( $text ) );
92 },
93 2 );
94  
95 $bignum_class->methods->{is_int} = native_function(
96 name => 'is_int',
97 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
98 0 my ( $self ) = @_;
99 0 ▶ return _self_value( $self )->is_int ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
_self_value($self)->is_int ? : 0 0 0
100 },
101 2 );
102  
103 2 my @cmp_names = qw( bcmp beq bne blt ble bgt bge );
104 2 for my $name ( @cmp_names ) {
105 $bignum_class->methods->{$name} = native_function(
106 name => $name,
107 0 ▶ native => sub {
Subroutine __ANON__: not called CC=14 SCAR=53.5
108 0 my ( $self, $other ) = @_;
109 0 my $cmp = _self_value( $self )->bcmp( _coerce_bignum( $other ) );
110 0 ▶ return 0 + $cmp if $name eq 'bcmp';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "bcmp" 0 0 0
111 0 ▶ return $cmp == 0 ? 1 : 0 if $name eq 'beq';
Branch: 2 branches
BranchTrueFalseTotal
if $name eq "beq" 0 0 0
$cmp == 0 ? : 0 0 0
112 0 ▶ return $cmp != 0 ? 1 : 0 if $name eq 'bne';
Branch: 2 branches
BranchTrueFalseTotal
if $name eq "bne" 0 0 0
$cmp != 0 ? : 0 0 0
113 0 ▶ return $cmp < 0 ? 1 : 0 if $name eq 'blt';
Branch: 2 branches
BranchTrueFalseTotal
if $name eq "blt" 0 0 0
$cmp < 0 ? : 0 0 0
114 0 ▶ return $cmp <= 0 ? 1 : 0 if $name eq 'ble';
Branch: 2 branches
BranchTrueFalseTotal
if $name eq "ble" 0 0 0
$cmp <= 0 ? : 0 0 0
115 0 ▶ return $cmp > 0 ? 1 : 0 if $name eq 'bgt';
Branch: 2 branches
BranchTrueFalseTotal
if $name eq "bgt" 0 0 0
$cmp > 0 ? : 0 0 0
116 0 ▶ return $cmp >= 0 ? 1 : 0 if $name eq 'bge';
Branch: 2 branches
BranchTrueFalseTotal
if $name eq "bge" 0 0 0
$cmp >= 0 ? : 0 0 0
117 0 return 0;
118 },
119 14 );
120 }
121  
122 my %unary = (
123 0 ▶ babs => sub { $_[0]->copy->babs },
Subroutine __ANON__: not called CC=1 SCAR=6.9
124 0 ▶ bneg => sub { $_[0]->copy->bneg },
Subroutine __ANON__: not called CC=1 SCAR=6.9
125 0 ▶ binv => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
126 0 my ( $value ) = @_;
127 0 ▶ return Math::BigFloat->new('0')
Branch: 1 branch
BranchTrueFalseTotal
if $value->is_zero 0 0 0
128 if $value->is_zero;
129 0 return Math::BigFloat->new('1')->bdiv( $value->copy, 128 );
130 },
131 0 ▶ bsin => sub { $_[0]->copy->bsin },
Subroutine __ANON__: not called CC=1 SCAR=6.9
132 0 ▶ bcos => sub { $_[0]->copy->bcos },
Subroutine __ANON__: not called CC=1 SCAR=6.9
133 0 ▶ btan => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
134 0 my ( $value ) = @_;
135 0 my $sin = $value->copy->bsin;
136 0 my $cos = $value->copy->bcos;
137 0 ▶ return Math::BigFloat->new('0')
Branch: 1 branch
BranchTrueFalseTotal
if $cos->is_zero 0 0 0
138 if $cos->is_zero;
139 0 return $sin->bdiv($cos);
140 },
141 0 ▶ bsqrt => sub { $_[0]->copy->bsqrt },
Subroutine __ANON__: not called CC=1 SCAR=6.9
142 0 ▶ bround => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
143 0 my ( $value ) = @_;
144 0 my $n = $value->numify;
145 0 ▶ my $rounded = $n >= 0 ? int( $n + 0.5 ) : int( $n - 0.5 );
Branch: 1 branch
BranchTrueFalseTotal
$n >= 0 ? : 0 0 0
146 0 return Math::BigFloat->new("$rounded");
147 },
148 0 ▶ bfloor => sub { $_[0]->copy->bfloor },
Subroutine __ANON__: not called CC=1 SCAR=6.9
149 0 ▶ bceil => sub { $_[0]->copy->bceil },
Subroutine __ANON__: not called CC=1 SCAR=6.9
150 2 );
151 2 for my $name ( sort keys %unary ) {
152 $bignum_class->methods->{$name} = native_function(
153 name => $name,
154 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
155 0 my ( $self ) = @_;
156 0 return _wrap( $bignum_class, $unary{$name}->( _self_value( $self ) ) );
157 },
158 20 );
159 }
160  
161 my %binary = (
162 0 ▶ badd => sub { $_[0]->copy->badd( $_[1] ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
163 0 ▶ bsub => sub { $_[0]->copy->bsub( $_[1] ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
164 0 ▶ bmul => sub { $_[0]->copy->bmul( $_[1] ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
165 0 ▶ bdiv => sub { $_[0]->copy->bdiv( $_[1] ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
166 0 ▶ bmod => sub { $_[0]->copy->bmod( $_[1] ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
167 0 ▶ bpow => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
168 0 my ( $lhs, $rhs ) = @_;
169 0 ▶ return $lhs->as_int->copy->bpow( $rhs->as_int )
Branch: 1 branch
BranchTrueFalseTotal
if $lhs->is_int and $rhs->is_int and $rhs >= 0 0 0 0
Condition0%0/6
$lhs->is_int and $rhs->is_int and $rhs >= 0
!ll&&!rl&&r
000
$lhs->is_int and $rhs->is_int
!ll&&!rl&&r
000
MC/DC0%0/3
$lhs->is_int and $rhs->is_int and $rhs >= 0
$lhs->is_int $rhs->is_int $rhs >= 0
Truth table0%0/4
A and B and C
A= $lhs->is_intB= $rhs->is_intC= $rhs >= 0
ABCresult
0XX0
10X0
1100
1111
170 if $lhs->is_int && $rhs->is_int && $rhs >= 0;
171 0 return $lhs->copy->bpow( $rhs );
172 },
173 2 );
174 2 for my $name ( sort keys %binary ) {
175 $bignum_class->methods->{$name} = native_function(
176 name => $name,
177 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
178 0 my ( $self, $other ) = @_;
179 0 my $lhs = _self_value( $self );
180 0 my $rhs = _coerce_bignum( $other );
181 0 return _wrap( $bignum_class, $binary{$name}->( $lhs, $rhs ) );
182 },
183 12 );
184 }
185  
186 $bignum_class->methods->{to_hex} = native_function(
187 name => 'to_hex',
188 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
189 0 my ( $self ) = @_;
190 0 my $value = _self_value( $self );
191 0 my $int = $value->copy->as_int;
192 0 my $text = $int->as_hex;
193 0 $text =~ s/\A\+//;
194 0 return $text;
195 },
196 2 );
197  
198 $bignum_class->methods->{to_dec} = native_function(
199 name => 'to_dec',
200 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
201 0 my ( $self ) = @_;
202 0 return _to_zuzu_string( _self_value( $self ) );
203 },
204 2 );
205  
206 $bignum_class->methods->{to_String} = native_function(
207 name => 'to_String',
208 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
209 0 my ( $self ) = @_;
210 0 return _to_zuzu_string( _self_value( $self ) );
211 },
212 2 );
213  
214 $bignum_class->methods->{to_Number} = native_function(
215 name => 'to_Number',
216 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
217 0 my ( $self ) = @_;
218 0 return 0 + _to_plain_decimal( _self_value( $self ) );
219 },
220 2 );
221  
222 return {
223 2 BigNum => $bignum_class,
224 };
225 }
226  
227 2 1;
228  
229 =pod
230  
231 =head1 COPYRIGHT AND LICENCE
232  
233 B<< Zuzu::Module::Math::BigNum >> is copyright Toby Inkster.
234  
235 It is free software; you may redistribute it and/or modify it under
236 the terms of either the Artistic License 1.0 or the GNU General Public
237 License version 2.
238  
239 =cut
« lib/Zuzu/Tidy.pm ↑ Summary blib/lib/Zuzu/Module/Worker.pm »