C++ Standard Library C++ STL Library

C++ <cmath> - log1p() Function



The C++ <cmath> log1p() function returns the natural logarithm of (1 + number), i.e., log(1+number).

Syntax

double log1p (double x);
float log1p (float x);
long double log1p (long double x);
double log1p (T x);                     

Parameters

x Specify the number.

Return Value

Returns the natural logarithm of (1 + number), i.e., log(1+number).
If the x is less than -1, domain error occurs.

Example:

In the example below, log1p() function is used to calculate the log(1+number).

#include <iostream>
#include <cmath>
using namespace std;
 
int main (){
  cout<<log1p(0)<<"\n";
  cout<<log1p(1)<<"\n";
  cout<<log1p(2)<<"\n";
  return 0;
}

The output of the above code will be:

0
0.693147
1.09861

❮ C++ <cmath> Library