From 95de0f40d6059cc62c7d3fb8295e12a74c128a95 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Thu, 05 Jan 2017 17:24:35 -0500 Subject: drhtz(), drhfz(): Properly handle negative significands --- (limited to 'lib') diff --git a/lib/Math/Decimal/FastPP.pm b/lib/Math/Decimal/FastPP.pm index 9723013..d140132 100644 --- a/lib/Math/Decimal/FastPP.pm +++ b/lib/Math/Decimal/FastPP.pm @@ -166,7 +166,11 @@ sub drhtz { my ($ai, $af, $ad) = $_[0] =~ m/^(.*)\.(.{$_[1]})(.*)$/ or return; my $as = $ai . $af; - ++$as if $ad > "5" . "0" x (length($ad) - 1); + if ($as >= 0) { + ++$as if $ad > "5" . "0" x (length($ad) - 1); + } else { + --$as if $ad > "5" . "0" x (length($ad) - 1); + } $as = sprintf("%0$_[1]i", $as); # The substr() code is 400% faster than this regular expression code. #$as =~ s/(.{$_[1]})$/.$1/; @@ -196,7 +200,11 @@ sub drhfz { my ($ai, $af, $ad) = $_[0] =~ m/^(.*)\.(.{$_[1]})(.*)$/ or return; my $as = $ai . $af; - ++$as if $ad >= "5" . "0" x (length($ad) - 1); + if ($as >= 0) { + ++$as if $ad >= "5" . "0" x (length($ad) - 1); + } else { + --$as if $ad >= "5" . "0" x (length($ad) - 1); + } $as = sprintf("%0$_[1]i", $as); # The substr() code is 400% faster than this regular expression code. #$as =~ s/(.{$_[1]})$/.$1/; -- cgit v0.9.1