| 1 | # /=====================================================================\ # | ||||||||||||
| 2 | # | LaTeXML::Common::Float | # | ||||||||||||
| 3 | # | Representation of floating point objects | # | ||||||||||||
| 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::Float; | ||||||||||||
| 13 | 5 | ▶ | use LaTeXML::Global; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 5 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | 5 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | 5 | ▶ | use base qw(LaTeXML::Common::Number); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | 5 | ▶ | use base qw(Exporter); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | 5 | our @EXPORT = (qw(&Float floatformat)); | |||||||||||
| 19 | |||||||||||||
| 20 | #====================================================================== | ||||||||||||
| 21 | # Exported constructor. | ||||||||||||
| 22 | |||||||||||||
| 23 | 0 | ▶ | sub Float { | ||||||||||
|
Subroutine
Float: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 24 | 0 | my ($number) = @_; | |||||||||||
| 25 | 0 | return LaTeXML::Common::Float->new($number); } | |||||||||||
| 26 | |||||||||||||
| 27 | 0 | ▶ | sub new { | ||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 28 | 0 | my ($class, $number) = @_; | |||||||||||
| 29 | 0 | ▶ | $number = ToString($number) if ref $number; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 30 | 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
| |||||||||||||
| 31 | |||||||||||||
| 32 | #====================================================================== | ||||||||||||
| 33 | |||||||||||||
| 34 | # Strictly speaking, Float isn't part of TeX, but it's handy. | ||||||||||||
| 35 | |||||||||||||
| 36 | 0 | ▶ | sub toString { | ||||||||||
|
Subroutine
toString: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 37 | 0 | my ($self) = @_; | |||||||||||
| 38 | 0 | return floatformat($$self[0]); } | |||||||||||
| 39 | |||||||||||||
| 40 | 0 | ▶ | sub multiply { | ||||||||||
|
Subroutine
multiply: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 41 | 0 | my ($self, $other) = @_; | |||||||||||
| 42 | 0 | ▶ | return (ref $self)->new($self->valueOf * (ref $other ? $other->valueOf : $other)); } | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 43 | |||||||||||||
| 44 | 0 | ▶ | sub stringify { | ||||||||||
|
Subroutine
stringify: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 45 | 0 | my ($self) = @_; | |||||||||||
| 46 | 0 | return "Float[" . $$self[0] . "]"; } | |||||||||||
| 47 | |||||||||||||
| 48 | # Utility for formatting sane numbers. | ||||||||||||
| 49 | 0 | ▶ | sub floatformat { | ||||||||||
|
Subroutine
floatformat: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 50 | 0 | my ($n) = @_; | |||||||||||
| 51 | 0 | my $s = sprintf("%.5f", $n); | |||||||||||
| 52 | 0 | ▶ | $s =~ s/0+$// if $s =~ /\./; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 53 | # $s =~ s/\.$//; | ||||||||||||
| 54 | 0 | $s =~ s/\.$/.0/; # Seems TeX prints .0 which in odd corner cases, people use? | |||||||||||
| 55 | 0 | return $s; } | |||||||||||
| 56 | |||||||||||||
| 57 | #====================================================================== | ||||||||||||
| 58 | 5 | 1; | |||||||||||
| 59 | |||||||||||||
| 60 | __END__ | ||||||||||||