Subject: | Improve accuracy of log10() |
Currently, the implementation of log10() gives the following result:
$ perl -MMath::Utils=log10 -wle 'print int log10(1000)'
2
Here is a better implementation of log10() which adjusts the intermediate
result to higher accuracy:
sub log10 {
my @y = ();
my $log10 = log(10);
for my $x (@_) {
my $y = log($x) / $log10;
$y -= (10**$y - $x) / ($x * $log10);
push @y, $y;
}
return @y if wantarray;
return $y[0];
}
The new implementation gives
$ perl -MMath::Utils=log10 -wle 'print int log10(1000)'
3