|
Math Library Functions |
Include the math header file by using the preprocessor directive #include <math.h> when using functions in the math library.
Arguments may be constants, variables, or expressions.
| Function | Description | Example |
|
abs(x) |
Return absolute value of integer parameter |
n=abs(23) = 23 |
|
atof(str) |
Convert string to double |
|
|
ceil(x) |
Return the smallest integer that is greater or equal to x |
ceil (2.3) = 3.0 |
|
cos(x) |
Calculate cosine, Angle expressed in radians (180 degrees = PI radians). |
cos(45*3.14/180) = .707 |
|
exp(x) |
Calculate exponential |
exp(5)=148.413159 |
|
fabs |
Return absolute value of floating-point |
fabs (3.1416) = 3.1416 |
|
floor |
Round down value |
floor (2.3) = 2.0 |
|
fmod |
Return remainder of floating point division |
fmod (5.3,2) = 1.300000 |
|
labs |
Return absolute value of long integer parameter |
labs(100000)=100000 labs(-123456)=123456 |
|
log |
Calculate natural logarithm |
log (5.5)=1.704748 |
|
log10 |
Calculate logarithm base 10 |
log10 (param)= 3.000000 |
|
modf(x,i) |
Spli floating-point value into fractional and integer parts |
double param, fractpart, intpart; fractpart = 0.141593 |
|
pow(x) |
Calculate numeric power |
pow (7,3)=343.0 pow(2.2,4) = 23.4256 |
|
sin(x) |
Calculate sine, x= Angle expressed in radians (180 degrees = PI radians). |
sin (45*3.14/180);=.707 |
|
sqrt(x) |
Calculate square root |
sqrt(1024.0)=32.0 |
|
tan |
Calculate tangent |
tan (60*3.14/180)=1.7 |
| /* Testing the math
library functions */ #include <stdio.h> #include <math.h> int main() { printf( "sqrt(%.1f) = %.1f\n", 900.0, sqrt( 900.0 ) ); printf( "sqrt(%.1f) = %.1f\n", 9.0, sqrt( 9.0 ) ); printf( "exp(%.1f) = %f\n", 1.0, exp( 1.0 ) ); printf( "exp(%.1f) = %f\n", 2.0, exp( 2.0 ) ); printf( "log(%f) = %.1f\n", 2.718282, log( 2.718282 ) ); printf( "log(%f) = %.1f\n", 7.389056, log( 7.389056 ) ); printf( "log10(%.1f) = %.1f\n", 1.0, log10( 1.0 ) ); printf( "log10(%.1f) = %.1f\n", 10.0, log10( 10.0 ) ); printf( "log10(%.1f) = %.1f\n", 100.0, log10( 100.0 ) ); printf( "fabs(%.1f) = %.1f\n", 13.5, fabs( 13.5 ) ); printf( "fabs(%.1f) = %.1f\n", 0.0, fabs( 0.0 ) ); printf( "fabs(%.1f) = %.1f\n", -13.5, fabs( -13.5 ) ); printf( "ceil(%.1f) = %.1f\n", 9.2, ceil( 9.2 ) ); printf( "ceil(%.1f) = %.1f\n", -9.8, ceil( -9.8 ) ); printf( "floor(%.1f) = %.1f\n", 9.2, floor( 9.2 ) ); printf( "floor(%.1f) = %.1f\n", -9.8, floor( -9.8 ) ); printf( "pow(%.1f, %.1f) = %.1f\n", 2.0, 7.0, pow( 2.0, 7.0 ) ); printf( "pow(%.1f, %.1f) = %.1f\n", 9.0, 0.5, pow( 9.0, 0.5 ) ); printf( "fmod(%.3f/%.3f) = %.3f\n", 13.675, 2.333, fmod( 13.675, 2.333 ) ); printf( "sin(%.1f) = %.1f\n", 0.0, sin( 0.0 ) ); printf( "cos(%.1f) = %.1f\n", 0.0, cos( 0.0 ) ); printf( "tan(%.1f) = %.1f\n", 0.0, tan( 0.0 ) ); return 0; } |