Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library


The C++ cmath log() function is used to return the natural logarithm of a given number.

Syntax

double log(double x);
float log(float x);
long double log(long double x);
double log(double x);
float log(float x);
long double log(long double x);
//additional overloads for integral types 
double log(T x);                     

Parameters

x Specify the number.

Return Value

Returns the natural logarithm of a given number.
If the x is negative, domain error occurs.

Example:

In the below example, log() function is used to calculate the natural logarithm of a given number.

#include <iostream>
#include <cmath>

using namespace std;
 
int main (){
  cout<<log(10)<<"\n";
  cout<<log(50)<<"\n";
  cout<<log(100)<<"\n";
  return 0;
}

The output of the above code will be:

2.30259
3.91202
4.60517

❮ C++ <cmath> Library