The <math.h> header file contains various methods for performing mathematical operations. Most of these functions work with floating-point numbers (double and float) and return a double value.
On many Linux systems, you must explicitly tell the compiler to link the math library by adding the -lm flag at the end of your command.
Standard C operators do not include a symbol for powers (like ^ in some languages). You must use these functions:
C provides multiple ways to round numbers depending on the direction you need.
| Function | Result (Input: 3.2) | Description |
|---|---|---|
ceil(x) |
4.0 | Rounds up to the nearest integer. |
floor(x) |
3.0 | Rounds down to the nearest integer. |
round(x) |
3.0 | Rounds to the nearest integer. |
fmod(x, y) |
0.2 (if y=3) | Returns the floating-point remainder. |
These functions expect angles to be in Radians, not degrees.
sin(x), cos(x), tan(x): Standard trigonometry.asin(x), acos(x), atan(x): Inverse trigonometry.When you pass invalid numbers to math functions (like sqrt(-1)), C returns a special value called NaN (Not a Number) or HUGE_VAL for infinity.
math.h functions are optimized for double precision by default!