log() in CPP
Overview
The log() function defined in the <cmath> header file in C++ returns the natural logarithm of the argument passed.
Syntax of log() in CPP
Types of prototype of log() function in C++11.
The syntax of the log() function is given as follows :
Here, result is the variable in which the natural log of x is stored.
Parameters of log() in CPP
The log() in cpp takes a number in the range of [0,] as a valid argument. If the argument passed is out of the range, i.e., less than 0, then the log() function returns NaN (Not a Number).
Return Value of log() in CPP
Parameter | Return value |
---|---|
x > 1 | Positive number |
x = 1 | 0 |
0< x <1 | Negative number |
x = 0 | -inf |
x < 0 | NaN |
Example
Let us try to implement the log() function in cpp.
Code:
Output:
Explanation:
In the above code,
- We are storing the value of x as 10.
- We will pass the value of x to the log() function in cpp.
- The log() function evaluates the natural logarithm of x i.e, 10.
- The value of the natural logarithm of x is then returned by the log() function.
- The value is stored in the result variable, which is later printed out as output.
What is log() in C++?
Natural logarithms are inverse of the natural exponential function. When we use the log in cpp, it returns the logarithmic value of the parameter passed, with base e. The log() function takes a number as an input and returns a double type value for integral parameters. Depending on the different values of the arguments passed, the log() function returns different values. A valid argument is in the range of [0, ]. Any number out of the given range will result in NaN (Not a Number) by the log() function.
More Examples
Let us consider trying to pass different possible values as arguments to the log() function in cpp.
Passing 0 as Argument
Code:
Output:
In the above code, we passed 0 as the parameter to log in cpp. The log() function in cpp returns -inf.
Passing a Negative Number as an Argument
Code:
Output:
In the above code, we passed a negative number as the parameter to log in cpp. The log() function in cpp returns -nan.
Passing a Positive Number Greater than 1 as an Argument
Code:
Output:
In the above code, we passed a positive number greater than 1 as the parameter to log in cpp. The log() function in cpp returns the natural logarithm of the number passed.
Passing a Number Between 0 and 1 as an Argument
Code:
Output:
In the above code, we passed a positive number greater than 0 and less than 1 as the parameter to log in cpp. The log() function in cpp returns the natural logarithm of the number passed. The result will be negative.
Passing 1 as an Argument
Code:
Output:
In the above code, we passed 1 as the parameter to log in cpp. The log() function in cpp returns 0 as the result.
Conclusion
- The log() function in cpp is used to find the natural logarithm of the number.
- The log() function takes a single number as a parameter.
- The log() function returns a double type value for an integral type valid argument.