C++ Standard Library C++ STL Library

C++ <cmath> - log() Function



The C++ <cmath> log() function returns 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 example below, 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

5