← The C Language
Lesson 31 of 77 · 10 XP
Maths functions
Theory and problem statement
Machine translation, not yet reviewed by a native speaker.
Maths lives in the <math.h> header:
#include <math.h>
sqrt(16.0) // 4.0 — square root
pow(2, 10) // 1024.0 — power
fabs(-3.5) // 3.5 — absolute value of a double (for int — abs from stdlib.h)
floor(2.7) // 2.0 — down
ceil(2.1) // 3.0 — up
round(2.5) // 3.0 — to the nearest
sin(x), cos(x) // in radians
M_PI // π (not part of the strict standard, but available almost everywhere)
All these functions work with double and return double. It's better not to thoughtlessly turn the result of pow for whole powers into an int: because of rounding errors, pow(10, 2) could in theory give 99.999….
On Linux, when compiling with gcc, the maths library is linked with the -lm flag: gcc main.c -o main -lm. Here it's already done for you.
Task. Read the coordinates of two points x1 y1 x2 y2 (fractional numbers) and print the distance between them with two decimal places.
Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.