log1p() in C++
The log1p() function takes an argument x and returns the natural logarithm of the base-e logarithm of x+1. Here e is a mathematical constant with value equal to 2.71828.
Syntax:
double log1p (double x); float log1p (float x); long double log1p (long double x);
Parameter:
- The log1p() function takes a single argument in the range [-1, ?].
- If we pass the value which is less than -1, log1p() returns Nan (Not a Number).
Return:
- a positive number : if x > 0
- zero if x=0
- a negative number if -1 > x > 0
- -?(- infinity) if x=-1
- NaN if x<-1
Error and Exception:
- It is mandatory to give both the arguments otherwise it will give error no matching function for call to ‘log1p()’.
- If we pass the string as argument we will get error no matching function for call to ‘log1p(const char [n]).
- It gives -inf if we pass -1.
- It gives zero if we pass 0.
Examples:
Input : log1p(50.35) Output : 3.93866
Input : log1p(143) Output : 4.96981
# CODE 1
CPP
// CPP program to illustrate log1p() #include <cmath> #include <iostream> using namespace std; int main() { double x = 50.35, answer; // returns logarithm of 51.35 base e answer = log1p(x); cout << "log1p(" << x << ") = " << answer << endl; return 0; } |
Output:
log1p(50.35) = 3.93866
Time Complexity: O(1)
Space Complexity: O(1)
# CODE 2
CPP
// CPP program to illustrate log1p() #include <cmath> #include <iostream> using namespace std; int main() { double answer; int x = 143; // returns logarithm of 144 base e answer = log1p(x); cout << "log1p(" << x << ") = " << answer << endl; return 0; } |
Output:
log1p(143) = 4.96981
Time Complexity:O(1)
Space Complexity: O(1)
Practical usage:
- It is practically used to get logarithm value of given argument+1.
Please Login to comment...