Namespaces
Variants
Views
Actions

log

From cppreference.com
< c‎ | numeric‎ | math
 
 
 
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99)
(C99)
(C99)(C99)(C99)
Exponential functions
(C99)
(C99)
log
(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       logf( float arg );
(since C99)
double      log( double arg );
long double logl( long double arg );
(since C99)

Computes the natural (base e) logarithm of arg.

Contents

[edit] Parameters

arg - floating point value

[edit] Return value

Natural logarithm of 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("log(2.718282) = %f\n", log(2.718282));
    printf("exp(1.0)      = %f\n", exp(1.0));        /* inverse of log */
    printf("log(1.0)      = %f\n", log(1.0));
    printf("log(INFINITY) = %f\n", log(INFINITY));
    printf("log(NAN)      = %f\n", log(NAN));
    printf("\n");
 
    /* domain error                       */
    /* "invalid" floating-point exception */
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("log(-1.0)     = %f\n", log(-1.0));
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
    printf("\n");
 
    /* pole error                                */
    /* "divide-by-zero" floating-point exception */
    /* same for log(-0.0)                        */
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("log(0.0)      = %f\n", log(0.0));
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
 
    return 0;
}

Possible output:

log(2.718282) = 1.000000
exp(1.0)      = 2.718282
log(1.0)      = 0.000000
log(NAN)      = nan
log(INFINITY) = inf
 
log(-1.0)     = nan
Numerical argument out of domain
current exceptions raised:  FE_INVALID
 
log(0.0)      = -inf
Numerical result out of range
current exceptions raised:  FE_DIVBYZERO

[edit] See also

returns e raised to the given power (ex)
(function) [edit]
computes common (base 10) logarithm (log10(x))
(function) [edit]
raises a number to the given power (xy)
(function) [edit]