The log function in C computes the base-e (natural) logarithm of x
. A domain error occurs if the argument is negative. A pole error may occur if the argument is zero. The x
is the argument that is passed into the log().
It is declared in math.h
and takes one argument in the form of double and returns the value of type double.
Syntax of log function in C:
//Syntax of log function in C double log(double x);
Parameters:
x
=> floating types (double)
Return value:
The log functions return loge(x).
You can use the logf() function to work specifically with float and logl() to work with long double type. See the below syntax.
//other log function in C float logf(float x); (since-c99) long double logl(long double x); (since-c99)
C program to understand the working of log function:
Below mentioned C example code shows the usage of log().
Example 1:
#include <stdio.h> #include <math.h> int main() { double x = 4.2, result; result = log(x); printf("log(%lf) = %lf", x, result); return 0; }
Output:
log(4.200000) = 1.435085
Example 2:
#include <stdio.h> #include <math.h> int main() { double x = 2.71828182846; double result = log(x); printf("log(%lf) = %lf", x, result); return 0; }
Output:
log(2.718282) = 1.000000
Error handling
- Errors are reported as specified in math_errhandling.
- Domain error occurs if arg is less than zero.
- Pole error may occur if arg is zero.
If the implementation supports IEEE floating-point arithmetic (IEC 60559),
- If the argument is ±0, -∞ is returned and FE_DIVBYZERO is raised.
- If the argument is 1, +0 is returned.
- If the argument is negative, NaN is returned and FE_INVALID is raised..
- If the argument is +∞, +∞ is returned.
- If the argument is NaN, NaN is returned.
#include <stdio.h> #include <math.h> #include <float.h> #include <errno.h> #include <fenv.h> #pragma STDC FENV_ACCESS ON int main() { // special values printf("log(1) = %f\n", log(1)); printf("log(+Inf) = %f\n", log(INFINITY)); //error handling errno = 0; feclearexcept(FE_ALL_EXCEPT); printf("log(0) = %f\n", log(0)); if(errno == ERANGE) { perror("errno == ERANGE"); } if(fetestexcept(FE_DIVBYZERO)) { puts("FE_DIVBYZERO raised"); } return 0; }
Output:
log(1) = 0.000000
log(+Inf) = inf
log(0) = -inf
errno == ERANGE: Numerical result out of range
FE_DIVBYZERO raised
C program to calculates log2 with base 5.
here we will take the help of Logarithm change of base rule to calculate log2 of base 5.
log5(2) = loge(2) / loge(5)
#include <stdio.h> #include <math.h> int main() { double result = log(2)/log(5); printf("log2(5) = %lf",result); return 0; }
Output:
log2(5) = 0.430677
Recommended Post:
- fabs use in C language
- abs labs llabs functions in C/C++
- floor function in C with example code.
- ceil function use in C programming.
- Use of pow function in C language.
- C program to calculate the power of a number.
- sqrt function in C.
- C program to find all roots of a quadratic equation using switch case.
- C program to find the roots of a quadratic equation.
- How to find whether a given number is prime number in C?
- Use of isxdigit in C programming.
- How to use ispunct function in C programming?