English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ log1p() function usage and example

C++ Library Function <cmath>

C ++in the log1p() function takes the parameter x and returns x + 1the natural logarithm (logarithm with base e).

This function calculates<cmath>defined in the header file.

log(x+1) = log1p(x)

log1p() prototype [from C ++ 11Standard library begin]

double log1p(double x);
float log1p(float x);
long double log1p(long double x);
double log1p(T x); //for integer

log1p() function has only one parameter and returns a value of type double, float, or long double.

log1p() parameter

log1p() function uses a single mandatory parameter, ranging from[-1, ∞].

if the value is less than-1, then log1p() returns NaN (Not a Number).

log1p() return value

log1p() function returns1the natural logarithm of the parameter plus the given parameter.

log1p() return value
Parameter (x)Return value
x > 0Positive
x = 0Zero
-1> x > 0Negative
x = -1-∞ (-infinity)
x <-1NaN (Not a Number)

Example1: log1p() function in C ++how it works?

#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
    double x = 21.371, result;
    result = log1p(x);
    cout << "log1p(x) = " << result << endl;
    return 0;
}

When running the program, the output is:

log1p(x) = 3.10777

Example2: integer type log1p() function

#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
    double result =
    int x = 147;
    result = log1p(x);
    cout << "log1p(x) = " << result << endl;
    return 0;
}

When running the program, the output is:

log1p(x) = 4.99721

C++ Library Function <cmath>