How to use math functions in C

1 Answer

0 votes
// #include <math.h> gives you access to:
// sqrt, cbrt
// log, log10, exp
// fabs, floor, ceil, pow
// M_PI, M_E (if supported)

#include <stdio.h>
#include <math.h>

int main() {
    double x = 9.0;

    printf("sqrt(x) = %f\n", sqrt(x));
    printf("log(x)  = %f\n", log(x));     // natural log
    printf("exp(1)  = %f\n", exp(1));     // e^1
    printf("fabs(-3.5) = %f\n", fabs(-3.5));
    printf("floor(3.7) = %f\n", floor(3.7));
    printf("ceil(3.2)  = %f\n", ceil(3.2));
    printf("pow(2, 8)  = %f\n", pow(2, 8));

    return 0;
}


/*
run:

sqrt(x) = 3.000000
log(x)  = 2.197225
exp(1)  = 2.718282
fabs(-3.5) = 3.500000
floor(3.7) = 3.000000
ceil(3.2)  = 4.000000
pow(2, 8)  = 256.000000

*/


 



answered 2 days ago by avibootz
edited 2 days ago by avibootz
...