C FunctionsLesson 28Free preview
C Math Functions
This lesson is part of C · All levels
Summary
The C mathematics library is provided with a header math.h containing functions such as sqrt (square root), pow (rank), ceil and floor (up/down clearance), and the M_PI constant for the value of pi. To use a mathematical function, a program usually needs to be linked to a mathematics library when compilation, for example -lm in GCC. Absolute values are taken with fabs for double or abs for int. These functions save time because you don't need to write the logic yourself.
Key points
- Header provides sqrt, pow, ceil, floor, fabs, and so on
- Sqrt calculates the square root, pow(a, b) calculates a of the order b
- Ceilings round up, floors round down
- In the GCC it's often necessary to flag-lms when compiling programs with math.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.