| 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
| |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | return bless [int($number) || "0"], $class; } # truncate, not round | ||||||||||||||||||||||||||
|
Condition0%0/2
int $number || '0'
MC/DC0%0/1
int $number || '0'
int $number
Truth table0%0/2
A
A= int $number
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | $prec = 0 if $prec < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | $prec = 5 if $prec > 5; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
(ref $other ? $other->valueOf : $other) || $LaTeXML::Common::Number::EPSILON
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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
(ref $other ? $other->valueOf : $other) || $LaTeXML::Common::Number::EPSILON
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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||