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


The C++ cmath log1p() function is used to return 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);
// additional overloads for integral types 
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 below example, 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