| 1 | package Math::Round::SignificantFigures; | ||||||||||||||
| 2 | 5 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 5 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 5 | ▶ | use POSIX qw{ceil floor log10}; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 5 | require Exporter; | |||||||||||||
| 6 | |||||||||||||||
| 7 | 5 | our $VERSION = '0.02'; | |||||||||||||
| 8 | 5 | our @ISA = qw(Exporter); | |||||||||||||
| 9 | 5 | my @figs = qw{roundsigfigs ceilsigfigs floorsigfigs}; | |||||||||||||
| 10 | 5 | my @digs = qw{roundsigdigs ceilsigdigs floorsigdigs}; | |||||||||||||
| 11 | 5 | our %EXPORT_TAGS = ( | |||||||||||||
| 12 | figs => \@figs, | ||||||||||||||
| 13 | digs => \@digs, | ||||||||||||||
| 14 | all => [@figs, @digs], | ||||||||||||||
| 15 | ); | ||||||||||||||
| 16 | 5 | our @EXPORT_OK = (@figs, @digs); | |||||||||||||
| 17 | 5 | our @EXPORT = qw{}; | |||||||||||||
| 18 | |||||||||||||||
| 19 | =head1 NAME | ||||||||||||||
| 20 | |||||||||||||||
| 21 | Math::Round::SignificantFigures - Perl package for rounding numbers to a specified number of Significant Figures | ||||||||||||||
| 22 | |||||||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||||||
| 24 | |||||||||||||||
| 25 | use Math::Round::SignificantFigures qw{roundsigfigs}; | ||||||||||||||
| 26 | print roundsigfigs(555.555, 3), "\n"; | ||||||||||||||
| 27 | |||||||||||||||
| 28 | =head1 DESCRIPTION | ||||||||||||||
| 29 | |||||||||||||||
| 30 | |||||||||||||||
| 31 | Math::Round::SignificantFigures supplies functions that will round numbers based on significant figures. | ||||||||||||||
| 32 | |||||||||||||||
| 33 | This package spans the controversy whether people prefer to call significant figures or significant digits. You may export either or both but, I called the package significant figures since that is the page for Wikipedia. | ||||||||||||||
| 34 | |||||||||||||||
| 35 | =head1 FUNCTIONS | ||||||||||||||
| 36 | |||||||||||||||
| 37 | The exporter group :figs exports the roundsigfigs, ceilsigfigs, floorsigfigs functions. The exporter group :digs exports the roundsigdigs, ceilsigdigs, floorsigdigs functions. The exporter group :all exports all six functions | ||||||||||||||
| 38 | |||||||||||||||
| 39 | =cut | ||||||||||||||
| 40 | |||||||||||||||
| 41 | #head2 _floor_or_ceil_by_significant_digits | ||||||||||||||
| 42 | # | ||||||||||||||
| 43 | #The function _floor_or_ceil_by_significant_digits was mostly gleaned from https://stackoverflow.com/questions/202302/rounding-to-an-arbitrary-number-of-significant-digits. The function roundToSignificantFigures code sample appears to be in the public domain. | ||||||||||||||
| 44 | # | ||||||||||||||
| 45 | #cut | ||||||||||||||
| 46 | |||||||||||||||
| 47 | 40 | ▶ | sub _floor_or_ceil_by_significant_figures { | ||||||||||||
|
Subroutine
_floor_or_ceil_by_significant_figures: called CC=7 SCAR=19.5
| |||||||||||||||
| 48 | 40 | my $num = shift; | |||||||||||||
| 49 | 40 | ▶ | my $sigfigs = shift || 3; #undef default and zero does not make any sense | ||||||||||||
|
Condition100%2/2
shift() || 3
MC/DC100%1/1
shift() || 3
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||
| 50 | 40 | ▶ | my $ceiling = shift || 0; #-1 floor, 0 round, 1 ceil | ||||||||||||
|
Condition100%2/2
shift() || 0
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||
| 51 | 40 | ▶ | return $num if $num == 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 52 | 39 | ▶ | my $half = $num < 0 ? -0.5 : 0.5; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 53 | 39 | my $d = ceil(log10(abs($num))); | |||||||||||||
| 54 | 39 | my $power = $sigfigs - $d; | |||||||||||||
| 55 | 39 | my $magnitude = 10 ** $power; | |||||||||||||
| 56 | 39 | ▶ | my $shifted = $ceiling > 0 ? ceil($num * $magnitude) | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 57 | : $ceiling < 0 ? floor($num * $magnitude) | ||||||||||||||
| 58 | : int($num * $magnitude + $half); #round | ||||||||||||||
| 59 | 39 | return $shifted / $magnitude; | |||||||||||||
| 60 | } | ||||||||||||||
| 61 | |||||||||||||||
| 62 | =head2 roundsigfigs, roundsigdigs | ||||||||||||||
| 63 | |||||||||||||||
| 64 | Rounds a number given the number and a number of significant figures. | ||||||||||||||
| 65 | |||||||||||||||
| 66 | =cut | ||||||||||||||
| 67 | |||||||||||||||
| 68 | 23 | ▶ | sub roundsigfigs { | ||||||||||||
|
Subroutine
roundsigfigs: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 69 | 23 | my $num = shift; | |||||||||||||
| 70 | 23 | my $sigfigs = shift; | |||||||||||||
| 71 | 23 | return _floor_or_ceil_by_significant_figures($num, $sigfigs, 0); | |||||||||||||
| 72 | } | ||||||||||||||
| 73 | |||||||||||||||
| 74 | 8 | ▶ | sub roundsigdigs {roundsigfigs(@_)}; | ||||||||||||
|
Subroutine
roundsigdigs: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 75 | |||||||||||||||
| 76 | =head2 floorsigfigs, floorsigdigs | ||||||||||||||
| 77 | |||||||||||||||
| 78 | Rounds a number toward -inf given the number and a number of significant figures. | ||||||||||||||
| 79 | |||||||||||||||
| 80 | =cut | ||||||||||||||
| 81 | |||||||||||||||
| 82 | 8 | ▶ | sub floorsigfigs { | ||||||||||||
|
Subroutine
floorsigfigs: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 83 | 8 | my $num = shift; | |||||||||||||
| 84 | 8 | my $sigfigs = shift; | |||||||||||||
| 85 | 8 | return _floor_or_ceil_by_significant_figures($num, $sigfigs, -1); | |||||||||||||
| 86 | } | ||||||||||||||
| 87 | |||||||||||||||
| 88 | 4 | ▶ | sub floorsigdigs {floorsigfigs(@_)}; | ||||||||||||
|
Subroutine
floorsigdigs: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 89 | |||||||||||||||
| 90 | =head2 ceilsigfigs, ceilsigdigs | ||||||||||||||
| 91 | |||||||||||||||
| 92 | Rounds a number toward +inf given the number and a number of significant figures. | ||||||||||||||
| 93 | |||||||||||||||
| 94 | =cut | ||||||||||||||
| 95 | |||||||||||||||
| 96 | 9 | ▶ | sub ceilsigfigs { | ||||||||||||
|
Subroutine
ceilsigfigs: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 97 | 9 | my $num = shift; | |||||||||||||
| 98 | 9 | my $sigfigs = shift; | |||||||||||||
| 99 | 9 | return _floor_or_ceil_by_significant_figures($num, $sigfigs, 1); | |||||||||||||
| 100 | } | ||||||||||||||
| 101 | |||||||||||||||
| 102 | 4 | ▶ | sub ceilsigdigs {ceilsigfigs(@_)}; | ||||||||||||
|
Subroutine
ceilsigdigs: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 103 | |||||||||||||||
| 104 | =head1 SEE ALSO | ||||||||||||||
| 105 | |||||||||||||||
| 106 | L<Math::Round> supplies functions that will round numbers in different ways. | ||||||||||||||
| 107 | |||||||||||||||
| 108 | L<Math::SigDig> allows you to edit numbers to a significant number of digits. | ||||||||||||||
| 109 | |||||||||||||||
| 110 | L<https://en.wikipedia.org/wiki/Significant_figures#Rounding_to_significant_figures> | ||||||||||||||
| 111 | |||||||||||||||
| 112 | L<https://stackoverflow.com/questions/202302/rounding-to-an-arbitrary-number-of-significant-digits> | ||||||||||||||
| 113 | |||||||||||||||
| 114 | =head1 AUTHOR | ||||||||||||||
| 115 | |||||||||||||||
| 116 | Michael R. Davis, MRDVT | ||||||||||||||
| 117 | |||||||||||||||
| 118 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 119 | |||||||||||||||
| 120 | MIT LICENSE | ||||||||||||||
| 121 | |||||||||||||||
| 122 | Copyright (C) 2022 by Michael R. Davis | ||||||||||||||
| 123 | |||||||||||||||
| 124 | =cut | ||||||||||||||
| 125 | |||||||||||||||
| 126 | 5 | 1; | |||||||||||||