In this C++ tutorial, you will learn how to compute natural logarithm of (x+1), using log1p() function of cmath, with syntax and examples.
C++ log1p()
C++ log1p() returns natural logarithm of (x+1), where x is the given number as argument.
Syntax
The syntax of C++ log1p() is
log1p(x)
where
Parameter | Description |
---|---|
x | A double, float, long double, or any integral type value. |
Returns
The return value depends on the type of value passed for parameter x.
The return value of log1p(x) is
- double if x is double or integral type.
- float if x is float.
- long double if x is long double.
The synopsis of log1p() function is
double log1p (double x);
float log1p (float x);
long double log1p (long double x);
double log1p (T x); // for integral type argument values
log1p() is a function of cmath library. Include cmath library at the start of program, if using log1p() function.
Example
In this example, we read a value from user into variable x, and find the base-e logarithm value of (x+1) using log1p() function.
C++ Program
#include <iostream>
#include<cmath>
using namespace std;
int main() {
double x;
cout << "Enter a number : ";
cin >> x;
double result = log1p(x);
cout << "log1p(" << x << ") : " << result << endl;
}
Output
Enter a number : 10
log1p(10) : 2.3979
Program ended with exit code: 0
Enter a number : 0
log1p(0) : 0
Program ended with exit code: 0
Enter a number : -5
log1p(-5) : nan
Program ended with exit code: 0
Conclusion
In this C++ Tutorial, we learned the syntax of C++ log1p(), and how to use this function to find the natural logarithm of (x+1), with the help of examples.