Skip to content

Commit f066f8d

Browse files
committed
add a bit to a number before rounding to combatx machine rounding error
1 parent d16b4d0 commit f066f8d

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

macros/core/PGauxiliaryFunctions.pl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,16 @@ =head2 Round
175175
176176
Round(1.789,2) returns 1.79
177177
178+
Before rounding, the environment variable numZeroLevelTolDefault is added to a number (or subtracted
179+
from a number if that number is negative). This is done to combat machine rounding error where
180+
somthing like 1.5 has become 1.4999999999999997.
181+
178182
=cut
179183

180-
# Round contributed bt Mark Schmitt 3-6-03
181184
sub Round {
182-
if (@_ == 1) { $_[0] > 0 ? int $_[0] + 0.5 : int $_[0] - 0.5 }
183-
elsif (@_ == 2) { $_[0] > 0 ? Round($_[0] * 10**$_[1]) / 10**$_[1] : Round($_[0] * 10**$_[1]) / 10**$_[1] }
185+
my $x = $_[0] > 0 ? $_[0] + $envir{numZeroLevelTolDefault} : $_[0] - $envir{numZeroLevelTolDefault};
186+
if (@_ == 1) { $x > 0 ? int $x + 0.5 : int $x - 0.5 }
187+
elsif (@_ == 2) { $x > 0 ? Round($x * 10**$_[1]) / 10**$_[1] : Round($x * 10**$_[1]) / 10**$_[1] }
184188
}
185189

186190
=head2 lcm

0 commit comments

Comments
 (0)