Math-Round-SignificantFigures
view release on metacpan or search on metacpan
# NAME
Math::Round::SignificantFigures - Perl package for rounding numbers to a specified number of Significant Figures
# SYNOPSIS
use Math::Round::SignificantFigures qw{roundsigfigs};
print roundsigfigs(555.555, 3), "\n";
# DESCRIPTION
Math::Round::SignificantFigures supplies functions that will round numbers based on significant figures.
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.
# FUNCTIONS
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
## roundsigfigs, roundsigdigs
Rounds a number given the number and a number of significant figures.
## floorsigfigs, floorsigdigs
Rounds a number toward -inf given the number and a number of significant figures.
## ceilsigfigs, ceilsigdigs
Rounds a number toward +inf given the number and a number of significant figures.
lib/Math/Round/SignificantFigures.pm view on Meta::CPAN
package Math::Round::SignificantFigures;
use strict;
use warnings;
use POSIX qw{ceil floor log10};
require Exporter;
our $VERSION = '0.02';
our @ISA = qw(Exporter);
my @figs = qw{roundsigfigs ceilsigfigs floorsigfigs};
my @digs = qw{roundsigdigs ceilsigdigs floorsigdigs};
our %EXPORT_TAGS = (
figs => \@figs,
digs => \@digs,
all => [@figs, @digs],
);
our @EXPORT_OK = (@figs, @digs);
our @EXPORT = qw{};
=head1 NAME
Math::Round::SignificantFigures - Perl package for rounding numbers to a specified number of Significant Figures
=head1 SYNOPSIS
use Math::Round::SignificantFigures qw{roundsigfigs};
print roundsigfigs(555.555, 3), "\n";
=head1 DESCRIPTION
Math::Round::SignificantFigures supplies functions that will round numbers based on significant figures.
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.
=head1 FUNCTIONS
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
=cut
#head2 _floor_or_ceil_by_significant_digits
#
#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 publ...
#
#cut
sub _floor_or_ceil_by_significant_figures {
lib/Math/Round/SignificantFigures.pm view on Meta::CPAN
my $half = $num < 0 ? -0.5 : 0.5;
my $d = ceil(log10(abs($num)));
my $power = $sigfigs - $d;
my $magnitude = 10 ** $power;
my $shifted = $ceiling > 0 ? ceil($num * $magnitude)
: $ceiling < 0 ? floor($num * $magnitude)
: int($num * $magnitude + $half); #round
return $shifted / $magnitude;
}
=head2 roundsigfigs, roundsigdigs
Rounds a number given the number and a number of significant figures.
=cut
sub roundsigfigs {
my $num = shift;
my $sigfigs = shift;
return _floor_or_ceil_by_significant_figures($num, $sigfigs, 0);
}
sub roundsigdigs {roundsigfigs(@_)};
=head2 floorsigfigs, floorsigdigs
Rounds a number toward -inf given the number and a number of significant figures.
=cut
sub floorsigfigs {
my $num = shift;
my $sigfigs = shift;
t/001_export_all.t view on Meta::CPAN
use strict;
use warnings;
use Test::More tests => 13;
use Math::Round::SignificantFigures qw{:all};
is(roundsigfigs(555.555, 1), 600);
is(ceilsigfigs(555.555, 1), 600);
is(floorsigfigs(555.555, 1), 500);
is(roundsigdigs(555.555, 1), 600);
is(ceilsigdigs(555.555, 1), 600);
is(floorsigdigs(555.555, 1), 500);
is(roundsigdigs( 12.5555, 3), 12.6);
is(roundsigdigs(-12.5555, 3), -12.6);
is(ceilsigdigs( 12.5555, 3), 12.6);
is(ceilsigdigs(-12.5555, 3), -12.5);
is(floorsigdigs( 12.5555, 3), 12.5);
is(floorsigdigs(-12.5555, 3), -12.6);
is(ceilsigfigs(42.34523, 1), 50, "why I wrote this package");
t/002_export_figs.t view on Meta::CPAN
use strict;
use warnings;
use Test::More tests => 9;
use Math::Round::SignificantFigures qw{:figs};
is(roundsigfigs(555.555, 1), 600);
is(ceilsigfigs(555.555, 1), 600);
is(floorsigfigs(555.555, 1), 500);
is(eval{roundsigdigs(555.555, 1)}, undef);
like($@, qr/Undefined subroutine/);
is(eval{floorsigdigs(555.555, 1)}, undef);
like($@, qr/Undefined subroutine/);
is(eval{ceilsigdigs(555.555, 1)}, undef);
like($@, qr/Undefined subroutine/);
t/003_export_digs.t view on Meta::CPAN
use strict;
use warnings;
use Test::More tests => 9;
use Math::Round::SignificantFigures qw{:digs};
is(roundsigdigs(555.555, 1), 600);
is(ceilsigdigs(555.555, 1), 600);
is(floorsigdigs(555.555, 1), 500);
is(eval{roundsigfigs(555.555, 1)}, undef);
like($@, qr/Undefined subroutine/);
is(eval{floorsigfigs(555.555, 1)}, undef);
like($@, qr/Undefined subroutine/);
is(eval{ceilsigfigs(555.555, 1)}, undef);
like($@, qr/Undefined subroutine/);
t/004_math_sigdig.t view on Meta::CPAN
use strict;
use warnings;
use Test::More tests => 4;
use Math::Round::SignificantFigures qw{roundsigdigs};
diag("Tests from Math::SigDig");
is(roundsigdigs(12.3456789 ), 12.3);
is(roundsigdigs(12.3456789 ,4), 12.35);
is(roundsigdigs(-12.345e-6789,2), -12e-6789);
is(roundsigdigs(12.00456789 ,4), 12.00);
t/004_wikipedia.t view on Meta::CPAN
use strict;
use warnings;
use Test::More tests => 17;
use Math::Round::SignificantFigures qw{:figs};
diag("Tests from Wikipedia");
is(roundsigfigs(12.345, 6), 12.345);
is(roundsigfigs(12.345, 5), 12.345);
is(roundsigfigs(12.345, 4), 12.35);
is( ceilsigfigs(12.345, 4), 12.35);
is(floorsigfigs(12.345, 4), 12.34);
is(roundsigfigs(12.345, 3), 12.3);
is(roundsigfigs(12.345, 2), 12);
is(roundsigfigs(12.345, 1), 10);
is(roundsigfigs(0.012345, 7), 0.01234500);
is(roundsigfigs(0.012345, 6), 0.0123450);
is(roundsigfigs(0.012345, 5), 0.012345);
is(roundsigfigs(0.012345, 4), 0.01235);
is( ceilsigfigs(0.012345, 4), 0.01235);
is(floorsigfigs(0.012345, 4), 0.01234);
is(roundsigfigs(0.012345, 3), 0.0123);
is(roundsigfigs(0.012345, 2), 0.012);
is(roundsigfigs(0.012345, 1), 0.01);
( run in 2.849 seconds using v1.01-cache-2.11-cpan-71847e10f99 )