blib/lib/File/Rsync/Mirror/Recentfile/FakeBigFloat.pm

filter: Statementstmt 93.4% 100 / 107 Branchbran 80.0% 48 / 60 Conditioncond 70.5% 12 / 17 MC/DCmcdc 54.5% 6 / 11 Subroutinesub 100% 15 / 15 Podpod n/a totaltotal 86.1% 181 / 210 CC 40 SCAR 24.0
CC
40
Cov
87%
_bigfloatcmp
19.3
_increase_a_bit_tail
13.1
_increase_a_bit
9.8
CRAP 11.0
« blib/lib/File/Rsync/Mirror/Recentfile/Done.pm ↑ Summary bin/rrr-news »
1 package File::Rsync::Mirror::Recentfile::FakeBigFloat;
2  
3 # use warnings;
4 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use Data::Float qw(nextup);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # _bigfloat
8 sub _bigfloatcmp ($$);
9 sub _bigfloatge ($$);
10 sub _bigfloatgt ($$);
11 sub _bigfloatle ($$);
12 sub _bigfloatlt ($$);
13 sub _bigfloatmax ($$);
14 sub _bigfloatmin ($$);
15 sub _increase_a_bit ($;$);
16 sub _increase_a_bit_tail ($$);
17 sub _my_sprintf_float ($);
18  
19 =encoding utf-8
20  
21 =head1 NAME
22  
23 File::Rsync::Mirror::Recentfile::FakeBigFloat - pseudo bigfloat support
24  
25 =cut
26  
27 14 ▶use version; our $VERSION = qv('0.0.8');
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 14 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 14 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
31 14 our %EXPORT_TAGS;
32 14 our @EXPORT_OK = qw(
33 _bigfloatcmp
34 _bigfloatge
35 _bigfloatgt
36 _bigfloatle
37 _bigfloatlt
38 _bigfloatmax
39 _bigfloatmin
40 _increase_a_bit
41 );
42 14 $EXPORT_TAGS{all} = \@EXPORT_OK;
43  
44 =head1 SYNOPSIS
45  
46 use File::Rsync::Mirror::Recentfile::FakeBigFloat qw(:all);
47  
48 =head1 ONLY INTERNAL FUNCTIONS
49  
50 These functions are not part of a public interface and can be
51 changed and go away any time without prior notice.
52  
53 =head1 DESCRIPTION
54  
55 We treat strings that look like floating point numbers. If the native
56 floating point support is good enough we use it. If it isn't we make
57 sure no two unequal numbers are treated the same and vice versa. Only
58 comparison operators are supported, no other math.
59  
60 =head1 EXPORT
61  
62 All functions are exported in the C<:all> tag.
63  
64 =head2 _bigfloatcmp ( $l, $r )
65  
66 Cmp function for floating point numbers that have a larger significand
67 than can be handled by native perl floats.
68  
69 =cut
70 40332 ▶sub _bigfloatcmp ($$) {
Subroutine _bigfloatcmp: called CC=16 SCAR=29.6
71 # my($l,$r) = @_;
72 40332 ▶ unless (defined $_[0] and defined $_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[0] and defined $_[1]) 0 40332 40332
Condition33%1/3
defined $_[0] and defined $_[1]
!ll&&!rl&&r
0040332
MC/DC0%0/2
defined $_[0] and defined $_[1]
defined $_[0] defined $_[1]
Truth table33%1/3
A and B
A= defined $_[0]B= defined $_[1]
ABresult
0X0
100
111
73 0 require Carp;
74 0 for ($_[0],$_[1]) {
75 0 ▶ $_ = defined $_ ? $_ : "UNDEF";
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
76 }
77 0 Carp::confess("_bigfloatcmp called with l[$_[0]]r[$_[1]]: but both must be defined");
78 }
79 # unequal is much more frequent than equal but let's get rid of these
80 40332 ▶ return 0 if $_[0] eq $_[1];
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq $_[1] 4536 35796 40332
81 35796 my $can_rely_on_native = 0;
82 35796 ▶ if ($_[0] =~ /\./ || $_[1] =~ /\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /\./ or $_[1] =~ /\./) { } 35740 56 35796
Condition100%3/3
$_[0] =~ /\./ or $_[1] =~ /\./
l!l&&r!l&&!r
357301056
MC/DC100%2/2
$_[0] =~ /\./ or $_[1] =~ /\./
$_[0] =~ /\./ $_[1] =~ /\./
Truth table100%3/3
A or B
A= $_[0] =~ /\./B= $_[1] =~ /\./
ABresult
1X1
011
000
83 # if one is a float, both must be, otherwise perl gets it wrong (see test)
84 35740 for ($_[0], $_[1]){
85 71480 ▶ $_ .= ".0" unless /\./;
Branch: 1 branch
BranchTrueFalseTotal
unless /\./ 231 71249 71480
86 }
87 35740 ▶ return 1 if $_[0] - $_[1] > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] - $_[1] > 1 5244 30496 35740
88 30496 ▶ return -1 if $_[0] - $_[1] < -1;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] - $_[1] < -1 4379 26117 30496
89 } else {
90 56 $can_rely_on_native = 1; # can we?
91 }
92 #### XXX bug in some perls, we cannot trust native comparison on floating point values:
93 #### see Todo file entry on 2009-03-15
94 26173 my $native = $_[0] <=> $_[1];
95 26173 ▶ return $native if $can_rely_on_native && $native != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $can_rely_on_native and $native != 0 56 26117 26173
Condition66%2/3
$can_rely_on_native and $native != 0
!ll&&!rl&&r
26117056
MC/DC50%1/2
$can_rely_on_native and $native != 0
$can_rely_on_native $native != 0
Truth table66%2/3
A and B
A= $can_rely_on_nativeB= $native != 0
ABresult
0X0
100
111
96 26117 ▶ $_[0] =~ s/^/0/ while index($_[0],".") < index($_[1],".");
Branch: 1 branch
BranchTrueFalseTotal
index($_[0], ".") < index($_[1], ".") and do { $_[0] =~ s/^/0/ } 7 26117 26124
97 26117 ▶ $_[1] =~ s/^/0/ while index($_[1],".") < index($_[0],".");
Branch: 1 branch
BranchTrueFalseTotal
index($_[1], ".") < index($_[0], ".") and do { $_[1] =~ s/^/0/ } 0 26117 26117
98 26117 $_[0] cmp $_[1];
99 }
100  
101 =head2 _bigfloatge ( $l, $r )
102  
103 Same for ge
104  
105 =cut
106 27855 ▶sub _bigfloatge ($$) {
Subroutine _bigfloatge: called CC=1 SCAR=0.0
107 27855 _bigfloatcmp($_[0],$_[1]) >= 0;
108 }
109  
110 =head2 _bigfloatgt ( $l, $r )
111  
112 Same for gt
113  
114 =cut
115 4262 ▶sub _bigfloatgt ($$) {
Subroutine _bigfloatgt: called CC=1 SCAR=0.0
116 4262 _bigfloatcmp($_[0],$_[1]) > 0;
117 }
118  
119 =head2 _bigfloatle ( $l, $r )
120  
121 Same for lt
122  
123 =cut
124 305 ▶sub _bigfloatle ($$) {
Subroutine _bigfloatle: called CC=1 SCAR=0.0
125 305 _bigfloatcmp($_[0],$_[1]) <= 0;
126 }
127  
128 =head2 _bigfloatlt ( $l, $r )
129  
130 Same for lt
131  
132 =cut
133 7682 ▶sub _bigfloatlt ($$) {
Subroutine _bigfloatlt: called CC=1 SCAR=0.0
134 7682 _bigfloatcmp($_[0],$_[1]) < 0;
135 }
136  
137 =head2 _bigfloatmax ( $l, $r )
138  
139 Same for max (of two arguments)
140  
141 =cut
142 201 ▶sub _bigfloatmax ($$) {
Subroutine _bigfloatmax: called CC=2 SCAR=6.9
143 201 my($l,$r) = @_;
144 201 ▶ return _bigfloatcmp($l,$r) >= 0 ? $l : $r;
Branch: 1 branch
BranchTrueFalseTotal
&_bigfloatcmp($l, $r) >= 0 ? : 195 6 201
145 }
146  
147 =head2 _bigfloatmin ( $l, $r )
148  
149 Same for min (of two arguments)
150  
151 =cut
152 27 ▶sub _bigfloatmin ($$) {
Subroutine _bigfloatmin: called CC=2 SCAR=6.9
153 27 my($l,$r) = @_;
154 27 ▶ return _bigfloatcmp($l,$r) <= 0 ? $l : $r;
Branch: 1 branch
BranchTrueFalseTotal
&_bigfloatcmp($l, $r) <= 0 ? : 5 22 27
155 }
156  
157 =head2 $big = _increase_a_bit ( $l, $r )
158  
159 =head2 $big = _increase_a_bit ( $n )
160  
161 The first form calculates a string that is between the two numbers,
162 closer to $l to prevent rounding effects towards $r. The second form
163 calculates the second number itself based on nextup() in
164 L<Data::Float>.
165  
166 =cut
167 75 ▶sub _my_sprintf_float ($) {
Subroutine _my_sprintf_float: called CC=3 SCAR=11.0
168 75 my($x) = @_;
169 75 my $r;
170 75 require Config;
171 75 ▶ my $nvsize = $Config::Config{nvsize} || 8;
Condition50%1/2
$Config::Config{'nvsize'} || 8
l!l
750
MC/DC0%0/1
$Config::Config{'nvsize'} || 8
$Config::Config{'nvsize'}
Truth table50%1/2
A
A= $Config::Config{'nvsize'}
Aresult
11
00
172 75 my $lom = 2*$nvsize; # "length of mantissa": nextup needs more digits
173 75 NORMALIZE: while () {
174 153 my $sprintf = "%." . $lom . "f";
175 153 $r = sprintf $sprintf, $x;
176 153 ▶ if ($r =~ /\.\d+0$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r =~ /\.\d+0$/) { } 75 78 153
177 75 last NORMALIZE;
178 } else {
179 78 $lom *= 2;
180 }
181 }
182 75 $r =~ s/(\d)0+$/$1/;
183 75 return $r;
184 }
185 39 ▶sub _increase_a_bit ($;$) {
Subroutine _increase_a_bit: called CC=9 SCAR=22.9
186 39 my($l,$r) = @_;
187 39 ▶ unless (defined $l) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $l) 0 39 39
188 0 die "Alert: _increase_a_bit called with undefined first argument";
189 }
190 39 ▶ if (defined $r){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r) { } 3 36 39
191 3 ▶ if ($r eq $l){
Branch: 2 branches
BranchTrueFalseTotal
if ($r eq $l) { } 0 3 3
elsif ($r > int($l) + 1) { } 0 3 3
192 0 die "Alert: _increase_a_bit called with identical arguments";
193 } elsif ($r > int($l)+1) {
194 0 $r = int($l)+1;
195 }
196 } else {
197 36 $r = _my_sprintf_float(Data::Float::nextup($l));
198 }
199 39 my $ret;
200 39 ▶ if ($l == $r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l == $r) { } 0 39 39
201 } else {
202 # native try
203 39 my $try = _my_sprintf_float(($l+$r)/2);
204 39 ▶ if (_bigfloatlt($l,$try) && _bigfloatlt($try,$r) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_bigfloatlt($l, $try) and &_bigfloatlt($try, $r)) 8 31 39
Condition100%3/3
&_bigfloatlt($l, $try) and &_bigfloatlt($try, $r)
!ll&&!rl&&r
10218
MC/DC100%2/2
&_bigfloatlt($l, $try) and &_bigfloatlt($try, $r)
&_bigfloatlt($l, $try) &_bigfloatlt($try, $r)
Truth table100%3/3
A and B
A= &_bigfloatlt($l, $try)B= &_bigfloatlt($try, $r)
ABresult
0X0
100
111
205 8 $ret = $try;
206 }
207 }
208 39 ▶ return $ret if $ret;
Branch: 1 branch
BranchTrueFalseTotal
if $ret 8 31 39
209 31 return _increase_a_bit_tail($l,$r);
210 }
211 31 ▶sub _increase_a_bit_tail ($$) {
Subroutine _increase_a_bit_tail: called CC=13 SCAR=25.7
212 31 my($l,$r) = @_;
213 31 my $ret;
214 31 for ($l, $r){
215 62 ▶ $_ .= ".0" unless /\./;
Branch: 1 branch
BranchTrueFalseTotal
unless /\./ 1 61 62
216 }
217 31 ▶ $l =~ s/^/0/ while index($l,".") < index($r,".");
Branch: 1 branch
BranchTrueFalseTotal
index($l, ".") < index($r, ".") and do { $l =~ s/^/0/ } 0 31 31
218 31 ▶ $r =~ s/^/0/ while index($r,".") < index($l,".");
Branch: 1 branch
BranchTrueFalseTotal
index($r, ".") < index($l, ".") and do { $r =~ s/^/0/ } 0 31 31
219 31 ▶ $l .= "0" while length($l) < length($r);
Branch: 1 branch
BranchTrueFalseTotal
length $l < length $r and do { $l .= '0' } 388 31 419
220 31 ▶ $r .= "0" while length($r) < length($l);
Branch: 1 branch
BranchTrueFalseTotal
length $r < length $l and do { $r .= '0' } 0 31 31
221 31 my $diffdigit;
222 31 ▶ DIG: for (my $i = 0; $i < length($l); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $l) 551 0 551
223 551 ▶ if (substr($l,$i,1) ne substr($r,$i,1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($l, $i, 1) ne substr($r, $i, 1)) 31 520 551
224 31 $diffdigit = $i;
225 31 last DIG;
226 }
227 }
228 31 $ret = substr($l,0,$diffdigit);
229 31 my $sl = substr($l,$diffdigit); # significant l
230 31 my $sr = substr($r,$diffdigit);
231 31 ▶ if ($ret =~ /\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret =~ /\./) 30 1 31
232 30 $sl .= ".0";
233 30 $sr .= ".0";
234 }
235 31 my $srlength = length $sr;
236 31 my $srmantissa = $srlength - index($sr,".");
237 # we want 1+$srlength because if l ends in 99999 and r in 00000,
238 # we need one digit more
239 31 my $fformat = sprintf "%%0%d.%df", 1+$srlength, $srmantissa;
240 31 my $appe = sprintf $fformat, ($sl+$sr)/2;
241 31 $appe =~ s/(\d)0+$/$1/;
242 31 ▶ if ($ret =~ /\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret =~ /\./) 30 1 31
243 30 $appe =~ s/\.//;
244 }
245 31 $ret .= $appe;
246 31 CHOP: while () {
247 477 my $try = substr($ret,0,length($ret)-1);
248 477 ▶ if (_bigfloatlt($l,$try) && _bigfloatlt($try,$r)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_bigfloatlt($l, $try) and &_bigfloatlt($try, $r)) { } 446 31 477
Condition66%2/3
&_bigfloatlt($l, $try) and &_bigfloatlt($try, $r)
!ll&&!rl&&r
310446
MC/DC50%1/2
&_bigfloatlt($l, $try) and &_bigfloatlt($try, $r)
&_bigfloatlt($l, $try) &_bigfloatlt($try, $r)
Truth table66%2/3
A and B
A= &_bigfloatlt($l, $try)B= &_bigfloatlt($try, $r)
ABresult
0X0
100
111
249 446 $ret = $try;
250 } else {
251 31 last CHOP;
252 }
253 }
254 31 return $ret;
255 }
256  
257 =head1 COPYRIGHT & LICENSE
258  
259 Copyright 2008, 2009 Andreas König.
260  
261 This program is free software; you can redistribute it and/or modify it
262 under the same terms as Perl itself.
263  
264 =cut
265  
266 14 1; # End of File::Rsync::Mirror::Recentfile
267  
268 # Local Variables:
269 # mode: cperl
270 # cperl-indent-level: 4
271 # End:
« blib/lib/File/Rsync/Mirror/Recentfile/Done.pm ↑ Summary bin/rrr-news »