Skip to content

Commit

Permalink
libc: improve accuracy of log1pf. Fix #515
Browse files Browse the repository at this point in the history
Improved log1p version, using Pade approximant,
4 multiplications and one division, relative error is bounded
by about 2^-21=5e-7.

From #516 (comment)
  • Loading branch information
parisseb authored and adriweb committed Nov 12, 2024
1 parent d7760ec commit e53d880
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/libc/log1p.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#include <math.h>

float log1pf(float x)
{
float log1pf(float x) {
if (fabs(x) <= 0.125) {
// pade(series(ln(1+x),x=0,6,polynom),x,5,3)
// (-57*x**2-90*x)/(x**3-21*x**2-102*x-90)
// relative error less than 1e-7
return x*(57*x+90)/(((21-x)*x+102)*x+90);
}
// relative error about 2^-21 if abs(x) is just above 0.125
return logf(1 + x);
}

Expand Down

0 comments on commit e53d880

Please sign in to comment.