Skip to content

Commit e53d880

Browse files
parissebadriweb
authored andcommitted
libc: improve accuracy of log1pf. Fix #515
Improved log1p version, using Pade approximant, 4 multiplications and one division, relative error is bounded by about 2^-21=5e-7. From #516 (comment)
1 parent d7760ec commit e53d880

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/libc/log1p.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
#include <math.h>
22

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

0 commit comments

Comments
 (0)