Lesson 8.9. Mathematical functions in C

math.h

The standard library math.h contains about a dozen common mathematical functions like

#include <math.h>

All functions that involve angles use the radian as a unit.

List of functions in math.h

The list of functions can be found on the page Wikipedia of math.h. Here are some of the most common ones:

// Sine of X
double sin(double X);
// Cosine of X
double cos(double X);
// Tangeante of X
double tan(double X);
// Arcsin(X) in domain [-π/2, π/2], x[-1, 1]
double asin(double X);
// Arccos(X) in domain [0, π], x[-1, 1]
double acos(double X);
// Arctan(X) in domain [-π/2, π/2]
double atan(double X);
// arc tangent of the quotient of its arguments (in the right quadrant)
double atan2( double Y, double X);
// Exponential of X
double exp(double X);
// Natural logarithm: ln(X), X>0
double log(double X);
// Base 10 logarithm: log10(X), X>0
double log10(double X);
// X exposant Y (X power Y)
double pow(double X, double Y);
// Square root of X, X>=0
double sqrt(double X);
// Absolute value of X : |X|
double fabs(double X);
// Rounded down to the nearest integer
double floor(double X);
// Rounded up to the nearest integer
double ceil(double X);
// Rounded to the nearest
double round(double X);

We will not detail each function, but only the atan2() function.

atan2

The function atan2() computes the arc tangeant on the basis of coordinates. In other words, atan2() computes the angle formed by a vector x,y and the x-axis.

Illustration of the atan2() function in C

Note the counter-intuitive order of the parameters: y first, then x.

Pi

The math.h library defines the symbolic constant M_PI which contains the value of \( \pi \):

#define M_PI       3.14159265358979323846

As long as the math.h library is included, this constant is accessible.

Example

The following example calculates the angle formed by the x,y vector and the x-axis.

x = 1
y = 0
atan2(0.00, 1.00) = 0.0000 rad
x = 1
y = 1
atan2(1.00, 1.00) = 0.7854 rad

Exercises

Exercise 1

Write a pythagoras() function that computes the length of the hypothenuse of a triangle from the length of the other two sides. Recall here the theorem of Pythagoras :

In a right triangle, the square of the length of the hypotenuse (or side opposite the right angle) is equal to the sum of the squares of the lengths of the other two sides.

Opposite side = 4.0
Adjacent side = 3.0
hypotenuse = 5.00

Exercise 2

Write a program that displays the coordinates of the points of a unit circle with a step of 20 degrees. Recall that the coordinates of the unit circle can be calculated by the following formulas:

$$ \begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} cos(\alpha) \\ sin(\alpha) \end{bmatrix} $$

Display the points according to this example:

α=0.00   =>   [ 1.00 , y= 0.00]
α=0.35   =>   [ 0.94 , y= 0.34]
α=0.70   =>   [ 0.77 , y= 0.64]
α=1.05   =>   [ 0.50 , y= 0.87]
α=1.40   =>   [ 0.17 , y= 0.98]
α=1.75   =>   [-0.17 , y= 0.98]
α=2.09   =>   [-0.50 , y= 0.87]
α=2.44   =>   [-0.77 , y= 0.64]
α=2.79   =>   [-0.94 , y= 0.34]
α=3.14   =>   [-1.00 , y= 0.00]
α=3.49   =>   [-0.94 , y=-0.34]
α=3.84   =>   [-0.77 , y=-0.64]
α=4.19   =>   [-0.50 , y=-0.87]
α=4.54   =>   [-0.17 , y=-0.98]
α=4.89   =>   [ 0.17 , y=-0.98]
α=5.24   =>   [ 0.50 , y=-0.87]
α=5.59   =>   [ 0.77 , y=-0.64]
α=5.93   =>   [ 0.94 , y=-0.34]
α=6.28   =>   [ 1.00 , y=-0.00]

Exercise 3

Write a function that calculates the distance between a point in space (x,y,z) and the origin:

// Calculate the norm of a vector
double norm(double x, double y, double z);

The main program asks the user to enter the coordinates of a point in the plane (X,Y) then the program displays the polar coordinates of this point:

X = 1.
Y = 2.
Argument = 1.11
Norm = 2.24

Quiz

To calculate x², what can we use?

Check Bravo! The multiplication is faster than the pow() function Try again...

If you have to use \( \pi \) in a program, how do you proceed?

Check Bravo! M_PI is already defined in math.h Try again...

How to round a number down?

double x=-2.7;
Check Bravo! You have to use the floor function. The cast might work for positive numbers, but not for negative ones. Try again...

See also


Last update : 11/21/2022