Answer: Mentioned below are logarithmic and exponential functions defined in ‘math.h’ header.
double log(double value); /* computes natural logarithm */ double log10(double value); double exp(double value);
‘log()’ function computes logarithm of given argument to the base e. This is called “natural logarithm”. ‘log10()’ function computes logarithm of its argument to base 10. Recall that logarithm of a value, say x, to any arbitrary base ‘b’ can be calculated using the following formula
log x to base b = log x to base e / log b to base e;
This equals
log x to base b = log(x) / log(b);
Remember that logarithm for NEGATIVE values NOT DEFINED! Let’s try out a simple C program using ‘log()’ function
/* fp_nat_log_fun.c -- 'log()' computes value of base e logarithm */ /* of its arguments */ /* this is called natural logarathim */ #include <stdio.h> #include <stdlib.h> #include <math.h> int main(void) { double value, result; printf("User, enter +ve value to compute its natural " "logarithm to base \"e\"...\n"); printf("Logarithm for -ve values \"NOT DEFINED\"!\n"); scanf("%lf", &value); /* calling exp() function */ result = log(value); printf("Logarithm of \"%lf\": %lf\n", value, result); return 0; }
Output for some arbitrary values are as follows:
User, enter +ve value to compute its natural logarithmm to base "e"... Logarithm for -ve values "NOT DEFINED"! 2 Logarithm of "2.000000": 0.693147 User, enter +ve value to compute its natural logarithm to base "e"... Logarithm for -ve values "NOT DEFINED"! -2 Logarithm of "-2.000000": nan User, enter +ve value to compute its natural logarithm to base "e"... Logarithm for -ve values "NOT DEFINED"! -3.5 Logarithm of "-3.500000": nan User, enter +ve value to compute its natural logarithm to base "e"... Logarithm for -ve values "NOT DEFINED"! 200 Logarithm of "200.000000": 5.298317
Try out working with ‘log10()’ function for some arbitrary values as arguments and observe the outputs!
Exponent ‘exp()’ function is used to evaluate value of ‘e’ raised to power, say x, i.e. “e^x”. For example,
/* fp_exp_fun.c -- exp() computes value of e raised to the power */ /* of its arguments */ #include <stdio.h> #include <stdlib.h> #include <math.h> int main(void) { double to_what_power, result; printf("User, enter value for which you wish to compute " "expression \"e^value\"...\n"); scanf("%lf", &to_what_power); /* calling exp() function */ result = exp(to_what_power); printf("Exponent of exp. \"e^%lf\": %lf\n", to_what_power, result); return 0; }
Output for some arbitrary values as follows:
User, enter value for which you wish to compute expression "e^value"... 10 Exponent of exp. "e^10.000000": 22026.465795 User, enter value for which you wish to compute expression "e^value"... 0.12 Exponent of exp. "e^0.120000": 1.127497 User, enter value for which you wish to compute expression "e^value"... -12.5 Exponent of exp. "e^-12.500000": 0.000004
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Watch Advanced C Programming Videos
- Apply for Computer Science Internship
- Apply for C Internship
- Practice BCA MCQs
- Check C Books