The standard library `math.h`

contains about a dozen common mathematical functions like

- cosine
- sine
- tangeant
- power
- square root
- exponential
- logarithmic
- ...

`#include <math.h>`

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

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.

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.

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

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.

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

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

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]

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

**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...
- Learn C programming
- Lesson 1.1. History of C programming language
- Lesson 1.2. First program
- Lesson 1.3. Compilation
- Lesson 1.4. Preprocessor directives
- Lesson 1.5. Which compiler to choose?
- Lesson 1.6. Flowchart
- Lesson 2.1. Types and variables
- Lesson 2.2. Integers
- Lesson 2.3. Floating point numbers
- Lesson 2.4. Characters
- Lesson 2.5. Variables initialization
- Lesson 2.6. Ariane flight 501
- Lesson 3.1. Arithmetic operators
- Lesson 3.2. modulo
- Lesson 3.3. Operators and types
- Lesson 3.4. Type casting
- Lesson 3.5. Bitwise operators
- Lesson 3.6. Bitwise operators details
- Lesson 3.7. Shifting operators
- Lesson 3.8. Assignment operators
- Lesson 3.9. Increment and decrement operators
- Lesson 3.10. Comparison operators
- Lesson 3.11. Logical operators
- Lesson 3.12. Operator precedence
- Lesson 4.1. printf
- Lesson 4.2. scanf
- Lesson 4.3. putchar
- Lesson 5.1. Conditional branching (if...else)
- Lesson 5.2. Nested if and indentation
- Lesson 5.3. Checking intervals
- Lesson 5.4. Ternary Operator ?:
- Lesson 5.5. switch..case statement
- Lesson 5.6. Break keyword in switch..case
- Lesson 6.1. do..while loop in C
- Lesson 6.2. While loop in C
- Lesson 6.3. For loop in C
- Lesson 6.4. How to choose the right loop in C ?
- Lesson 6.5. Exercices on loops in C
- Lesson 7.1. Bit masking
- Lesson 7.2. Bit masking, how to clear a bit in C?
- Lesson 7.3. Bit masking, how to set a bit in C?
- Lesson 7.4. Bit masking, how to toggle a bit in C?
- Lesson 7.5. Bit masking, how to check a single bit in C?
- Lesson 7.6. Overview of bit masking
- Lesson 8.1. Basic syntax of C functions
- Lesson 8.2. Calling C functions
- Lesson 8.3. Void keyword
- Lesson 8.4. Return keyword
- Lesson 8.5. Variable scope
- Lesson 8.6. Global variables
- Lesson 8.7. Static variables
- Lesson 8.8. Random numbers in C
- Lesson 9.1. Syntax of C arrays
- Lesson 9.2. Array initialization
- Lesson 9.3. Multidimensional arrays
- Lesson 9.4. How C arrays are stored in memory?
- Lesson 9.5. Arrays and functions
- Lesson 9.6. Exercises on C arrays
- Lesson 10.1. Strings in C
- Lesson 10.2. Null terminated string
- Lesson 10.3. The string.h library
- Lesson 10.4. String and functions
- Lesson 11.1. Introduction to pointers in C
- Lesson 11.2. Syntax of C pointers
- Lesson 11.3. Dynamic memory allocation
- Lesson 11.4. Incrementing pointers
- Lesson 11.5. Pointers as function argument in C
- Lesson 12.1. Introduction to structures in C
- Lesson 12.2. Properties of structures in C
- Lesson 12.3. Structures and pointers
- Lesson 12.4. Structures and functions
- Lesson 13.1. Recursive functions in C
- Lesson 13.2. Depth of recursive functions
- Lesson 13.2. Mutual recursion
- Lesson 14.1. Additional exercises

Last update : 11/21/2022