Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid string operations on numbers #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 15 additions & 23 deletions lib/Format/Util/Numbers.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ our @EXPORT_OK = qw/commas to_monetary_number_format roundnear roundcommon finan

use Carp qw(cluck);
use Scalar::Util qw(looks_like_number);
use POSIX qw(ceil);
use POSIX qw(ceil floor);
use YAML::XS;
use File::ShareDir;
use Math::BigFloat lib => 'Calc';
Expand Down Expand Up @@ -72,7 +72,6 @@ Round a number near the precision of the supplied one.
# TYPE:
# CURRENCY: PRECISION
my $precisions = YAML::XS::LoadFile($ENV{FORMAT_UTIL_PRECISION} // File::ShareDir::dist_file('Format-Util', 'precision.yml'));
my $floating_point_regex = qr/^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/;

=head2 commas

Expand Down Expand Up @@ -180,12 +179,9 @@ sub formatnumber {

# return val if any one of value, currency or type is invalid
return $val
if ((
not defined $val
or $val !~ $floating_point_regex
)
or not defined $precisions->{$type // 'unknown-type'}
or not defined $precisions->{$type}->{$currency // 'unknown-type'});
if not looks_like_number($val)
or not defined $precisions->{$type // 'unknown-type'}
or not defined $precisions->{$type}->{$currency // 'unknown-type'};

return sprintf('%0.0' . $precisions->{$type}->{$currency} . 'f', $val);
}
Expand Down Expand Up @@ -222,14 +218,11 @@ sub financialrounding {

# return val if any one of value, currency or type is invalid
return $val
if ((
not defined $val
or $val !~ $floating_point_regex
)
if not looks_like_number($val)
or not defined $precisions->{$type // 'unknown-type'}
or not defined $precisions->{$type}->{$currency // 'unknown-type'});
or not defined $precisions->{$type}->{$currency // 'unknown-type'};

return _round_to_precison($precisions->{$type}->{$currency}, $val);
return _round_to_precision($precisions->{$type}->{$currency}, $val);
}

=head2 roundcommon
Expand Down Expand Up @@ -267,16 +260,15 @@ sub roundcommon {
my ($precision, $val) = @_;

return $val
if ((
not defined $val
or $val !~ $floating_point_regex
)
or (not defined $precision or $precision !~ /^(?:1(?:[eE][-]?[0-9]+)?|0(?:\.0*1)?)$/ or $precision == 0));
if not looks_like_number($val)
or not looks_like_number($precision)
or $precision <= 0
or $precision > 1;

# get the number of decimal places needed by BigFloat
$precision = log(1 / $precision) / log(10);
$precision = floor(0.5 + log(1 / $precision) / log(10));

return _round_to_precison($precision, $val);
return _round_to_precision($precision, $val);
}

=head2 get_precision_config
Expand Down Expand Up @@ -314,11 +306,11 @@ sub get_min_unit {
}

# common sub used by roundcommon and financialrounding
sub _round_to_precison {
sub _round_to_precision {
my ($precision, $val) = @_;

my $x = Math::BigFloat->bzero();
$x->badd($val)->bfround('-' . $precision, 'common');
$x->badd($val)->bfround(-$precision, 'common');

return $x->bstr();
}
Expand Down
1 change: 0 additions & 1 deletion t/Numbers.t
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ subtest 'roundcommon' => sub {
cmp_ok(roundcommon(1, 345.56789), '==', 346, 'Ones is correct.');
cmp_ok(roundcommon(0.1, 345.56789), '==', 345.6, 'Correct rounding for 1 decimal place');
cmp_ok(roundcommon(0.01, 345.56789), '==', 345.57, 'Hundredths rounding is correct.');
cmp_ok(roundcommon(0.02, 345.56789), '==', 345.56789, 'Two hundredths rounding is not supported.');
cmp_ok(roundcommon(10, 345.56789), '==', 345.56789, 'Not supported, only supported integer is 1');
is(roundcommon(0, undef), undef, 'Rounding undef yields undef.');
cmp_ok(roundcommon(1e-2, 10.456), '==', 10.46, 'Rounding with exponential precision');
Expand Down