Name
log1p
Synopsis
Calculates the logarithm of one plus a number
#include <math.h> doublelog1p
( doublex
); floatlog1pf
( floatx
); long doublelog1pl
( long doublex
);
The log1p()
functions
calculate the natural logarithm of the sum of one plus the argument
x
, or loge(1 +
x
). The function is designed to yield a
more accurate result than the expression log(
x
+ 1)
, especially when the value of the
argument is close to zero.
The natural logarithm is defined only for positive numbers. If
x
is less than -1, a domain error occurs;
if x
is equal to -1, a range error may
occur (or not, depending on the implementation).
Example
// atanh(x) is defined as 0.5 * ( log(x+1) − log(−x+1). // Rounding errors can result in different results for different methods. puts(" x atanh(x) atanh(x) − 0.5*(log1p(x) − log1p(−x))\n" "---------------------------------------------------------------"); for ( double x = -0.8; x < 1.0; x += 0.4) { double y = atanh(x); printf("%6.2f %15f %20E\n", x, y, y − 0.5*(log1p
(x) −log1p
(-x)) ); }
This code produces the following output:
x atanh(x) atanh(x) − 0.5*(log1p(x) − log1p(−x)) --------------------------------------------------------------- -0.80 -1.098612 -1.376937E-17 -0.40 -0.423649 -1.843144E-18 0.00 0.000000 0.000000E+00 0.40 0.423649 7.589415E-19 0.80 1.098612 -4.640385E-17
Get C in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.