Logarithmic and Exponential Functions in C Library

Question: What are Various Logarithmic and Exponential Functions in Standard C Library?

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

advertisement
advertisement
    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:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
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,

advertisement
/* 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.

advertisement
If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.