lib/LaTeXML/Common/Number.pm

filter: Statementstmt 38.8% 28 / 72 Branchbran 6.6% 2 / 30 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 26.9% 7 / 26 Podpod 78.9% 15 / 19 totaltotal 32.5% 52 / 160 CC 18 SCAR 23.9
CC
18
Cov
27%
roundto
42.0
new
12.0
sign
12.0
CRAP 11.0
« lib/LaTeXML/Post/Collector.pm ↑ Summary lib/LaTeXML/Post/Manifest.pm »
1 # /=====================================================================\ #
2 # | LaTeXML::Common::Number | #
3 # | Representation of numbers | #
4 # |=====================================================================| #
5 # | Part of LaTeXML: | #
6 # | Public domain software, produced as part of work done by the | #
7 # | United States Government & not subject to copyright in the US. | #
8 # |---------------------------------------------------------------------| #
9 # | Bruce Miller <bruce.miller@nist.gov> #_# | #
10 # | http://dlmf.nist.gov/LaTeXML/ (o o) | #
11 # \=========================================================ooo==U==ooo=/ #
12 package LaTeXML::Common::Number;
13 6 ▶use LaTeXML::Global;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use LaTeXML::Common::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use LaTeXML::Core::Token;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶use base qw(LaTeXML::Common::Object);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 6 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 6 our @EXPORT = (qw(&Number &roundto &kround));
21  
22 #======================================================================
23 # Exported constructor.
24  
25 0 ▶sub Number {
Subroutine Number: not called CC=1 SCAR=6.9
Pod documented
26 0 my ($number) = @_;
27 0 return LaTeXML::Common::Number->new($number); }
28  
29 #======================================================================
30  
31 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod undocumented
32 0 my ($class, $number) = @_;
33 0 ▶ $number = ToString($number) if ref $number;
Branch: 1 branch
BranchTrueFalseTotal
if ref $number 0 0 0
34 0 ▶ return bless [int($number) || "0"], $class; } # truncate, not round
Condition0%0/2
int $number || '0'
l!l
00
MC/DC0%0/1
int $number || '0'
int $number
Truth table0%0/2
A
A= int $number
Aresult
11
00
35  
36 0 ▶sub valueOf {
Subroutine valueOf: not called CC=1 SCAR=6.9
Pod documented
37 0 my ($self) = @_;
38 0 return $$self[0]; }
39  
40 0 ▶sub toString {
Subroutine toString: not called CC=1 SCAR=6.9
Pod documented
41 0 my ($self) = @_;
42 0 return $$self[0]; }
43  
44 6 my @SCALES = (1, 10, 100, 1000, 10000, 100000);
45  
46 # smallest number that makes a difference added to 1 in Perl's float format.
47 6 our $EPSILON = 1.0;
48 6 ▶while (1.0 + $EPSILON / 2 != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 + $LaTeXML::Common::Number::EPSILON / 2 != 1) 312 6 318
49 312 $EPSILON /= 2.0; }
50 6 our $ROUNDING_HALF = 0.5 * (1 - $EPSILON);
51  
52 # Round $number to $prec decimals (0...6)
53 # attempting to do so portably.
54 0 ▶sub roundto {
Subroutine roundto: not called CC=6 SCAR=37.4
Pod undocumented
55 0 my ($number, $prec) = @_;
56 0 ▶ $prec = 2 unless defined $prec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prec 0 0 0
57 0 ▶ $prec = 0 if $prec < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $prec < 0 0 0 0
58 0 ▶ $prec = 5 if $prec > 5;
Branch: 1 branch
BranchTrueFalseTotal
if $prec > 5 0 0 0
59 0 my $scale = $SCALES[$prec];
60 # scale to integer, w/some slop in case arbitrarily close to an integer...
61 0 my $n = $number * $scale * (1 + 100 * $EPSILON);
62 0 ▶ return int($n < -$EPSILON ? $n - 0.5 : ($n > $EPSILON ? $n + 0.5 : 0.0)) / $scale; }
Branch: 2 branches
BranchTrueFalseTotal
$n < -$LaTeXML::Common::Number::EPSILON ? : 0 0 0
$n > $LaTeXML::Common::Number::EPSILON ? : 0 0 0
63  
64 # An attempt at rounding floats to integers (like scaled points),
65 # in a (hopefully) Knuthian manner (like round_decimals \S102 in Tex The Program)
66 0 ▶sub kround {
Subroutine kround: not called CC=2 SCAR=17.9
Pod undocumented
67 0 my ($number) = @_;
68 0 ▶ return int($number < 0 ? $number - $ROUNDING_HALF : $number + $ROUNDING_HALF); }
Branch: 1 branch
BranchTrueFalseTotal
$number < 0 ? : 0 0 0
69  
70 0 ▶sub unlist {
Subroutine unlist: not called CC=1 SCAR=6.9
Pod documented
71 0 my ($self) = @_;
72 0 return $self; }
73  
74 0 ▶sub revert {
Subroutine revert: not called CC=1 SCAR=6.9
Pod undocumented
75 0 my ($self) = @_;
76 0 return ExplodeText($self->toString); }
77  
78 0 ▶sub smaller {
Subroutine smaller: not called CC=2 SCAR=17.9
Pod documented
79 0 my ($self, $other) = @_;
80 0 ▶ return ($self->valueOf < $other->valueOf) ? $self : $other; }
Branch: 1 branch
BranchTrueFalseTotal
$self->valueOf < $other->valueOf ? : 0 0 0
81  
82 0 ▶sub larger {
Subroutine larger: not called CC=2 SCAR=17.9
Pod documented
83 0 my ($self, $other) = @_;
84 0 ▶ return ($self->valueOf > $other->valueOf) ? $self : $other; }
Branch: 1 branch
BranchTrueFalseTotal
$self->valueOf > $other->valueOf ? : 0 0 0
85  
86 0 ▶sub absolute {
Subroutine absolute: not called CC=1 SCAR=6.9
Pod documented
87 0 my ($self, $other) = @_;
88 0 return (ref $self)->new(abs($self->valueOf)); }
89  
90 0 ▶sub sign {
Subroutine sign: not called CC=3 SCAR=24.8
Pod documented
91 0 my ($self) = @_;
92 0 ▶ return ($self->valueOf < 0) ? -1 : (($self->valueOf > 0) ? 1 : 0); }
Branch: 2 branches
BranchTrueFalseTotal
$self->valueOf < 0 ? : 0 0 0
$self->valueOf > 0 ? : 0 0 0
93  
94 0 ▶sub negate {
Subroutine negate: not called CC=1 SCAR=6.9
Pod documented
95 0 my ($self) = @_;
96 0 return (ref $self)->new(-$self->valueOf); }
97  
98 0 ▶sub add {
Subroutine add: not called CC=1 SCAR=6.9
Pod documented
99 0 my ($self, $other) = @_;
100 0 return (ref $self)->new($self->valueOf + $other->valueOf); }
101  
102 0 ▶sub subtract {
Subroutine subtract: not called CC=1 SCAR=6.9
Pod documented
103 0 my ($self, $other) = @_;
104 0 return (ref $self)->new($self->valueOf - $other->valueOf); }
105  
106 # arg 2 is a number
107 0 ▶sub multiply {
Subroutine multiply: not called CC=2 SCAR=17.9
Pod documented
108 0 my ($self, $other) = @_;
109 0 ▶ return (ref $self)->new(int($self->valueOf * (ref $other ? $other->valueOf : $other))); }
Branch: 1 branch
BranchTrueFalseTotal
ref $other ? : 0 0 0
110  
111 # Truncating division
112 0 ▶sub divide {
Subroutine divide: not called CC=3 SCAR=24.8
Pod documented
113 0 my ($self, $other) = @_;
114 0 ▶ return (ref $self)->new(int($self->valueOf / ((ref $other ? $other->valueOf : $other) || $EPSILON))); }
Branch: 1 branch
BranchTrueFalseTotal
ref $other ? : 0 0 0
Condition0%0/3
(ref $other ? $other->valueOf : $other) || $LaTeXML::Common::Number::EPSILON
l!l&&r!l&&!r
000
MC/DC0%0/2
(ref $other ? $other->valueOf : $other) || $LaTeXML::Common::Number::EPSILON
(ref $other ? $other->valueOf : $other) $LaTeXML::Common::Number::EPSILON
Truth table0%0/3
A || B
A= (ref $other ? $other->valueOf : $other)B= $LaTeXML::Common::Number::EPSILON
ABresult
1X1
011
000
115  
116 # Rounding division
117 0 ▶sub divideround {
Subroutine divideround: not called CC=3 SCAR=24.8
Pod documented
118 0 my ($self, $other) = @_;
119 0 ▶ return (ref $self)->new(int(0.5 + $self->valueOf / ((ref $other ? $other->valueOf : $other) || $EPSILON))); }
Branch: 1 branch
BranchTrueFalseTotal
ref $other ? : 0 0 0
Condition0%0/3
(ref $other ? $other->valueOf : $other) || $LaTeXML::Common::Number::EPSILON
l!l&&r!l&&!r
000
MC/DC0%0/2
(ref $other ? $other->valueOf : $other) || $LaTeXML::Common::Number::EPSILON
(ref $other ? $other->valueOf : $other) $LaTeXML::Common::Number::EPSILON
Truth table0%0/3
A || B
A= (ref $other ? $other->valueOf : $other)B= $LaTeXML::Common::Number::EPSILON
ABresult
1X1
011
000
120  
121 0 ▶sub stringify {
Subroutine stringify: not called CC=1 SCAR=6.9
Pod documented
122 0 my ($self) = @_;
123 0 return "Number[" . $$self[0] . "]"; }
124  
125 #======================================================================
126 6 1;
127  
128 __END__
« lib/LaTeXML/Post/Collector.pm ↑ Summary lib/LaTeXML/Post/Manifest.pm »