![]() |
log() function in C programming is a mathematical function provided by the math.h header file and is used to calculate the natural logarithm (base(e)) of a given number passed as parameter. This function returns natural logarithm of x as double and works with double data types for other data types like float or long double, we can use logf() and logl(), respectively. Syntax of log() in Cdouble log(double x); Parameters of log()The log(x) function takes a single parameter x, where:
Return Value of log()The log(x) function returns the logarithm of the given number x passed as a parameter. If the value of x is negative then it returns a domain error (NaN) because the logarithm of a negative number returns an imaginary number which is not valid. Examples of log() Function in CInput: Example 1The below program demonstrates how we can calculate the log of a given numbers using the log(x) function in C.
The natural logarithm of 5.000000 is 1.609438 Time Complexity: O(1) Explanation: In the above program, the errno global variable is used for error handling. Whenever a negative number is provided as an parameter to the log function the value of errno is set to EDOM which denotes domain error. Domain error occurs when an input argument is outside the domain of a mathematical function. Example 2The below program demonstrates how we can calculate the logarithm of different data types in C.
Output Natural logarithm of 2.00 is 0.69 Natural logarithm of 3.00 is 1.10 Natural logarithm of 4.00 is 1.39 Time Complexity: O(1) |
Reffered: https://www.geeksforgeeks.org
C Language |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 22 |