Subject: | Improve accuracy of log10() |
Currently, the implementation of log10() gives the following result:
$ perl -MMath::Fortran=log10 -wle 'print int log10(1000)'
2
Here is a better implementation of log10() which ajusts the result to higher accuracy:
sub log10 {
my $x = shift;
my $log_of_ten = log(10);
my $y = log($x) / $log_of_ten;
$y -= (10**$y - $x) / ($x * $log_of_ten);
return $y;
}
The new implementation gives
$ perl -MMath::Fortran=log10 -wle 'print int log10(1000)'
3