log(), logf(), logl() — Calculate natural logarithm

Standards

Standards / Extensions C or C++ Dependencies

ISO C
POSIX.1
XPG4
XPG4.2
ISO/ANSI C++
C99
Single UNIX Specification, Version 3
C++ TR1 C99

both  

Format

#include <math.h>

double log(double x);
float log(float x);                  /* C++ only */
long double log(long double x);      /* C++ only */
float logf(float x);
long double logl(long double x);

General description

Calculates the natural logarithm (base e) of x, for x greater than 0.
Note: These functions work in both IEEE Binary Floating-Point and hexadecimal floating-point formats. See IEEE binary floating-point for more information about IEEE Binary Floating-Point.

Returned value

Returns the computed value.

If x is negative, the function sets errno to EDOM and returns -HUGE_VAL. If x is 0.0, the function returns -HUGE_VAL and sets errno to ERANGE. If the correct value would cause an underflow, 0 is returned and the value ERANGE is stored in errno.

Special behavior for IEEE: If x greater than 0, the function returns the natural logarithm (base e) of x.

If x is negative, the function sets errno to EDOM and returns NaNQ. If x is 0.0, the function returns -HUGE_VAL and errno remains unchanged.
Note: When environment variable _EDC_SUSV3 is set to 2, and if x is 0.0, the function returns -HUGE_VAL and sets errno to ERANGE.

Example

CELEBL08
⁄* CELEBL08                                      
 
  This example calculates the natural logarithm of 1000.0.                     
                                                                                
 *⁄                                                                             
#include <math.h>                                                               
#include <stdio.h>                                                              
                                                                                
int main(void)                                                                  
{                                                                               
   double x = 1000.0, y;                                                        
                                                                                
   y = log(x);                                                                  
                                                                                
   printf("The natural logarithm of %lf is %lf\n", x, y);                       
}                                                                               
Output
The natural logarithm of 1000.000000 is 6.907755

Related information