Home »
C++ programming language
log1p() Function with Example in C++
C++ log1p() function: Here, we are going to learn about the log1p() function with example of cmath header in C++ programming language.
Submitted by IncludeHelp, on May 20, 2020
C++ log1p() function
log1p() function is a library function of cmath header, it is used to get the natural logarithm (the base-e logarithm) of the one plus given value. It accepts a value (float, double, or long double) and returns the natural logarithm.
Syntax
Syntax of log1p() function:
C++11:
double log1p (double x);
float log1p (float x);
long double log1p (long double x);
double log1p (T x);
Parameter(s)
- x – represents the value whose natural logarithm to be found.
Return value
It returns the natural logarithm of the one plus given value x.
Note:
- If the given value is less than -1, it causes a domain error.
- If the given value is -1, it may cause a pole error.
Sample Input and Output
Input:
float x = 1.0f
Function call:
log1p(x);
Output:
0.693147
Example
C++ code to demonstrate the example of log1p() function:
// C++ code to demonstrate the example of
// log1p() function
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
float x = 0.0f;
x = 10.0f;
cout << "log1p(" << x << "): " << log1p(x) << endl;
x = 1.0f;
cout << "log1p(" << x << "): " << log1p(x) << endl;
x = 20.0f;
cout << "log1p(" << x << "): " << log1p(x) << endl;
x = -10.4f;
cout << "log1p(" << x << "): " << log1p(x) << endl;
x = 0.0f;
cout << "log1p(" << x << "): " << log1p(x) << endl;
x = 100.6f;
cout << "log1p(" << x << "): " << log1p(x) << endl;
return 0;
}
Output
log1p(10): 2.3979
log1p(1): 0.693147
log1p(20): 3.04452
log1p(-10.4): -nan
log1p(0): 0
log1p(100.6): 4.62104
Reference: C++ log1p() function