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 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 "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(1+y) is 1/(1+y)
thus log(1+y+e) is close to log(1+y) + e/(1+y) ]
Both functions seem to work, though(my_log1p) seems
somewhat more accurae, and faster.
Any comment on the accuracy or portability ?
In particular, is it guaranteed that a conformant
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 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 "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(1+y) is 1/(1+y)
thus log(1+y+e) is close to log(1+y) + e/(1+y) ]
Both functions seem to work, though(my_log1p) seems
somewhat more accurae, and faster.
Any comment on the accuracy or portability ?
In particular, is it guaranteed that a conformant
implementation will NOT optimize things, defeating
the attempt to get accurate results ?
If no, is there a way to insure this ?
François Grieu