Namespaces
Variants
Views
Actions

log1p

From cppreference.com
< c‎ | numeric‎ | math
 
 
 
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99)
(C99)
(C99)(C99)(C99)
Exponential functions
(C99)
(C99)
log1p
(C99)
(C99)
Power functions
(C99)
(C99)
Trigonometric and hyperbolic functions
(C99)
(C99)
(C99)
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Nearest integer floating point operations
(C99)(C99)(C99)
(C99)
(C99)(C99)(C99)
Floating point manipulation functions
(C99)(C99)
(C99)
(C99)
Classification
(C99)
(C99)
(C99)
Macro constants
 
Defined in header <math.h>
float       log1pf( float arg );
(since C99)
double      log1p( double arg );
(since C99)
long double log1pl( long double arg );
(since C99)

Computes the natural (base e) logarithm of 1+arg. This function is more precise than the expression log(1+arg) if arg is close to zero.

Contents

[edit] Parameters

arg - floating point value

[edit] Return value

ln(1+arg)

Domain error occurs if arg is negative. NAN is returned in that case.

Range error occurs if arg is 0. -HUGE_VAL is returned in that case.

[edit] Example

#include <stdio.h>
#include <math.h>
#include <errno.h>
#include <fenv.h>
#include <string.h>
 
#pragma STDC FENV_ACCESS ON
 
void show_fe_exceptions(void)
{
    printf("current exceptions raised: ");
    if(fetestexcept(FE_DIVBYZERO))     printf(" FE_DIVBYZERO");
    if(fetestexcept(FE_INEXACT))       printf(" FE_INEXACT");
    if(fetestexcept(FE_INVALID))       printf(" FE_INVALID");
    if(fetestexcept(FE_OVERFLOW))      printf(" FE_OVERFLOW");
    if(fetestexcept(FE_UNDERFLOW))     printf(" FE_UNDERFLOW");
    if(fetestexcept(FE_ALL_EXCEPT)==0) printf(" none");
    printf("\n");
}
 
int main(void)
{
    printf("log1p(1.718282) = %f\n",    log1p(1.718282));
    printf("expm1(1.0)      = %f\n",    expm1(1.0));        /* inverse of log1p */
    printf("log1p(+0.0)     = %+.1f\n", log1p(+0.0));
    printf("log1p(-0.0)     = %+.1f\n", log1p(-0.0));
    printf("log1p(INFINITY) = %f\n",    log1p(INFINITY));
    printf("log1p(NAN)      = %f\n",    log1p(NAN));
    printf("\n");
 
    /* Expect log1p(x) to be more accurate than log(x+1) for small magnitude x. */
    printf("log(1e-16+1.0)  = %g\n", log(1e-16+1.0));   /* 0     */
    printf("log1p(1e-16)    = %g\n", log1p(1e-16));     /* 1e-16 */
    printf("\n");
 
    /* no domain error                    */
    /* "invalid" floating-point exception */
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("log1p(-2.0)     = %f\n", log1p(-2.0));
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
    printf("\n");
 
    /* no pole error                             */
    /* "divide-by-zero" floating-point exception */
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("log1p(-1.0)     = %f\n", log1p(-1.0));
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
 
    return 0;
}

Possible output:

log1p(1.718282) = 1.000000
expm1(1.0)      = 1.718282
log1p(+0.0)     = +0.0
log1p(-0.0)     = -0.0
log1p(INFINITY) = inf
log1p(NAN)      = nan
 
log(1e-16+1.0)  = 0
log1p(1e-16)    = 1e-16
log1p(-2.0)     = -nan
 
log1p(-2.0)     = -nan
Success
current exceptions raised:  FE_INVALID
 
log1p(-1.0)     = -inf
Success
current exceptions raised:  FE_DIVBYZERO

[edit] See also

computes natural (base e) logarithm (to base e) (ln(x))
(function) [edit]
computes common (base 10) logarithm (log10(x))
(function) [edit]