F
Francois Grieu
Many C platforms lack the C99 log1p(), which computes
log(1+x) accurately even when x is near 0.
I'm looking for a portable (C) alternative.
Somewhere I found code which can be re-expressed as:
double rep_log1p(double x)
{
return ((1+x)-1) ? log(1+x)*(x/((1+x)-1)) : x;
}
but I do not understand the math, and I am worried of
a possible "dead spot" somewhere.
I have derived
double my_log1p(double x)
{
return log(1+x)-(((1+x)-1)-x)/(1+x);
}
[the idea here is that if we define
y = 1+x after rounding
e = 1+x-y in the mathematical sense
the derivative of the function log(y) is 1/y
thus log(y+e) is close to log(y) + e/y ]
Both functions seem to work, though my_log1p() seems
somewhat more accurate, and faster.
Any comment on the accuracy or portability ?
In particular, is it guaranteed that a conformant C
implementation will NOT "optimize" things, defeating
the attempt to get accurate results ?
If no, is there a way to insure this ?
François Grieu
log(1+x) accurately even when x is near 0.
I'm looking for a portable (C) alternative.
Somewhere I found code which can be re-expressed as:
double rep_log1p(double x)
{
return ((1+x)-1) ? log(1+x)*(x/((1+x)-1)) : x;
}
but I do not understand the math, and I am worried of
a possible "dead spot" somewhere.
I have derived
double my_log1p(double x)
{
return log(1+x)-(((1+x)-1)-x)/(1+x);
}
[the idea here is that if we define
y = 1+x after rounding
e = 1+x-y in the mathematical sense
the derivative of the function log(y) is 1/y
thus log(y+e) is close to log(y) + e/y ]
Both functions seem to work, though my_log1p() seems
somewhat more accurate, and faster.
Any comment on the accuracy or portability ?
In particular, is it guaranteed that a conformant C
implementation will NOT "optimize" things, defeating
the attempt to get accurate results ?
If no, is there a way to insure this ?
François Grieu