| 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 | 4 | ▶ | use LaTeXML::Global; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 4 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 4 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | 4 | ▶ | use LaTeXML::Common::Object; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | 4 | ▶ | use LaTeXML::Core::Token; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | 4 | ▶ | use base qw(LaTeXML::Common::Object); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | 4 | ▶ | use base qw(Exporter); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 20 | 4 | our @EXPORT = (qw(&Number)); | |||||||||||||
| 21 | |||||||||||||||
| 22 | #====================================================================== | ||||||||||||||
| 23 | # Exported constructor. | ||||||||||||||
| 24 | |||||||||||||||
| 25 | sub Number { | ||||||||||||||
| 26 | 0 | ▶ | my ($number) = @_; | ||||||||||||
|
Subroutine
Number: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 27 | 0 | return LaTeXML::Common::Number->new($number); } | |||||||||||||
| 28 | |||||||||||||||
| 29 | #====================================================================== | ||||||||||||||
| 30 | |||||||||||||||
| 31 | sub new { | ||||||||||||||
| 32 | 0 | ▶ | my ($class, $number) = @_; | ||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||
| 33 | 0 | ▶ | $number = ToString($number) if ref $number; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 34 | 0 | ▶ | return bless [$number || "0"], $class; } | ||||||||||||
|
Condition0%0/2
$number || '0'
MC/DC0%0/1
$number || '0'
$number
Truth table0%0/2
A
A= $number
| |||||||||||||||
| 35 | |||||||||||||||
| 36 | sub valueOf { | ||||||||||||||
| 37 | 0 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
valueOf: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 38 | 0 | return $$self[0]; } | |||||||||||||
| 39 | |||||||||||||||
| 40 | sub toString { | ||||||||||||||
| 41 | 0 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
toString: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 42 | 0 | return $$self[0]; } | |||||||||||||
| 43 | |||||||||||||||
| 44 | 4 | 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 | 4 | my $EPSILON = 1.0; | |||||||||||||
| 48 | 4 | ▶ | while (1.0 + $EPSILON / 2 != 1) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 49 | 208 | $EPSILON /= 2.0; } | |||||||||||||
| 50 | |||||||||||||||
| 51 | # Round $number to $prec decimals (0...6) | ||||||||||||||
| 52 | # attempting to do so portably. | ||||||||||||||
| 53 | sub roundto { | ||||||||||||||
| 54 | 0 | ▶ | my ($number, $prec) = @_; | ||||||||||||
|
Subroutine
roundto: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||
| 55 | 0 | ▶ | $prec = 2 unless defined $prec; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 56 | 0 | ▶ | $prec = 0 if $prec < 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 57 | 0 | ▶ | $prec = 5 if $prec > 5; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 58 | 0 | my $scale = $SCALES[$prec]; | |||||||||||||
| 59 | # scale to integer, w/some slop in case arbitrarily close to an integer... | ||||||||||||||
| 60 | 0 | my $n = $number * $scale * (1 + 100 * $EPSILON); | |||||||||||||
| 61 | 0 | ▶ | return int($n < -$EPSILON ? $n - 0.5 : ($n > $EPSILON ? $n + 0.5 : 0.0)) / $scale; } | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 62 | |||||||||||||||
| 63 | sub ptValue { | ||||||||||||||
| 64 | 0 | ▶ | my ($self, $prec) = @_; | ||||||||||||
|
Subroutine
ptValue: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 65 | 0 | return roundto($$self[0] / 65536, $prec); } | |||||||||||||
| 66 | |||||||||||||||
| 67 | sub pxValue { | ||||||||||||||
| 68 | 0 | ▶ | my ($self, $prec) = @_; | ||||||||||||
|
Subroutine
pxValue: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 69 | 0 | ▶ | return roundto($$self[0] / 65536 * ($STATE->lookupValue('DPI') || 100 / 72.27), $prec); } | ||||||||||||
|
Condition0%0/2
$LaTeXML::Common::Number::STATE->lookupValue('DPI') || 1.383700013837
MC/DC0%0/1
$LaTeXML::Common::Number::STATE->lookupValue('DPI') || 1.383700013837
$LaTeXML::Common::Number::STATE->lookupValue('DPI')
Truth table0%0/2
A
A= $LaTeXML::Common::Number::STATE->lookupValue('DPI')
| |||||||||||||||
| 70 | |||||||||||||||
| 71 | sub unlist { | ||||||||||||||
| 72 | 0 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
unlist: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 73 | 0 | return $self; } | |||||||||||||
| 74 | |||||||||||||||
| 75 | sub revert { | ||||||||||||||
| 76 | 0 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
revert: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 77 | 0 | return ExplodeText($self->toString); } | |||||||||||||
| 78 | |||||||||||||||
| 79 | sub smaller { | ||||||||||||||
| 80 | 0 | ▶ | my ($self, $other) = @_; | ||||||||||||
|
Subroutine
smaller: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 81 | 0 | ▶ | return ($self->valueOf < $other->valueOf) ? $self : $other; } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 82 | |||||||||||||||
| 83 | sub larger { | ||||||||||||||
| 84 | 0 | ▶ | my ($self, $other) = @_; | ||||||||||||
|
Subroutine
larger: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 85 | 0 | ▶ | return ($self->valueOf > $other->valueOf) ? $self : $other; } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 86 | |||||||||||||||
| 87 | sub absolute { | ||||||||||||||
| 88 | 0 | ▶ | my ($self, $other) = @_; | ||||||||||||
|
Subroutine
absolute: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 89 | 0 | return (ref $self)->new(abs($self->valueOf)); } | |||||||||||||
| 90 | |||||||||||||||
| 91 | sub sign { | ||||||||||||||
| 92 | 0 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
sign: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||
| 93 | 0 | ▶ | return ($self->valueOf < 0) ? -1 : (($self->valueOf > 0) ? 1 : 0); } | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 94 | |||||||||||||||
| 95 | sub negate { | ||||||||||||||
| 96 | 0 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
negate: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 97 | 0 | return (ref $self)->new(-$self->valueOf); } | |||||||||||||
| 98 | |||||||||||||||
| 99 | sub add { | ||||||||||||||
| 100 | 0 | ▶ | my ($self, $other) = @_; | ||||||||||||
|
Subroutine
add: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 101 | 0 | return (ref $self)->new($self->valueOf + $other->valueOf); } | |||||||||||||
| 102 | |||||||||||||||
| 103 | sub subtract { | ||||||||||||||
| 104 | 0 | ▶ | my ($self, $other) = @_; | ||||||||||||
|
Subroutine
subtract: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 105 | 0 | return (ref $self)->new($self->valueOf - $other->valueOf); } | |||||||||||||
| 106 | |||||||||||||||
| 107 | # arg 2 is a number | ||||||||||||||
| 108 | sub multiply { | ||||||||||||||
| 109 | 0 | ▶ | my ($self, $other) = @_; | ||||||||||||
|
Subroutine
multiply: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 110 | 0 | ▶ | return (ref $self)->new(int($self->valueOf * (ref $other ? $other->valueOf : $other))); } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 111 | |||||||||||||||
| 112 | # Truncating division | ||||||||||||||
| 113 | sub divide { | ||||||||||||||
| 114 | 0 | ▶ | my ($self, $other) = @_; | ||||||||||||
|
Subroutine
divide: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 115 | 0 | ▶ | return (ref $self)->new(int($self->valueOf / (ref $other ? $other->valueOf : $other))); } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 116 | |||||||||||||||
| 117 | # Rounding division | ||||||||||||||
| 118 | sub divideround { | ||||||||||||||
| 119 | 0 | ▶ | my ($self, $other) = @_; | ||||||||||||
|
Subroutine
divideround: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 120 | 0 | ▶ | return (ref $self)->new(int(0.5 + $self->valueOf / (ref $other ? $other->valueOf : $other))); } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 121 | |||||||||||||||
| 122 | sub stringify { | ||||||||||||||
| 123 | 0 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
stringify: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 124 | 0 | return "Number[" . $$self[0] . "]"; } | |||||||||||||
| 125 | |||||||||||||||
| 126 | #====================================================================== | ||||||||||||||
| 127 | 4 | 1; | |||||||||||||
| 128 | |||||||||||||||
| 129 | __END__ | ||||||||||||||