man 3 math.h
Le fichier header du système:
/usr/include/math.h
Le fichier du code de la librairie statique
/usr/lib/x86_64-linux-gnu/libm.a
Le fichier du code de la librairie dynamique
/usr/lib/x86_64-linux-gnu/libm.so
code exemple : math.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
/* Affichage de M_PI et M_PI/2 */
printf("M_PI = %.15f\n", M_PI);
printf("M_PI/2 = %.15f\n", M_PI/2);
/* Valeurs trigonométriques avec M_PI */
printf("\n--- Valeurs de base ---\n");
printf("sin(0) = %.15f\n", sin(0.0));
printf("sin(M_PI/2) = %.15f\n", sin(M_PI/2));
printf("sin(M_PI) = %.15f\n", sin(M_PI));
printf("\ncos(0) = %.15f\n", cos(0.0));
printf("cos(M_PI/2) = %.15f\n", cos(M_PI/2));
printf("cos(M_PI) = %.15f\n", cos(M_PI));
/* Attention : tan(M_PI/2) n'est pas définie mathématiquement */
/* En C, elle peut donner une très grande valeur ou inf */
printf("\n--- Tangente ---\n");
printf("tan(0) = %.15f\n", tan(0.0));
/* Pour tan(M_PI/2), on s'approche de l'asymptote */
double angle = M_PI/2;
printf("tan(M_PI/2 - 0.0000001) = %.15f\n", tan(angle - 0.0000001));
printf("tan(M_PI/2 + 0.0000001) = %.15f\n", tan(angle + 0.0000001));
/* Fonctions réciproques */
printf("\n--- Fonctions réciproques ---\n");
printf("asin(1) = %.15f (devrait être M_PI/2)\n", asin(1.0));
printf("acos(0) = %.15f (devrait être M_PI/2)\n", acos(0.0));
printf("atan(1) = %.15f (devrait être M_PI/4)\n", atan(1.0));
printf("atan(M_PI/2) = %.15f\n", atan(M_PI/2));
/* Conversions degrés ↔ radians */
printf("\n--- Conversions ---\n");
printf("M_PI radians = 180 degres\n");
printf("M_PI/2 radians = %.15f degres\n", (M_PI/2) * 180.0 / M_PI);
printf("45 degres = %.15f radians\n", 45.0 * M_PI / 180.0);
return EXIT_SUCCESS;
}
Compilation dynamique, toujours faire un lien avec l’option -l suivi du m de libm (libm.a ou libm.so)
bruno@debian13:~/Works/langage_C/Math$ gcc math.c -o math -lm -g
bruno@debian13:~/Works/langage_C/Math$ ls -lh
total 24K
-rwxrwxr-x 1 bruno bruno 18K 27 janv. 19:21 math
-rw-rw-r-- 1 bruno bruno 1,7K 27 janv. 19:21 math.c
bruno@debian13:~/Works/langage_C/Math$ ldd math
linux-vdso.so.1 (0x00007f3b91e7c000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f3b91d61000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f3b91b6c000)
/lib64/ld-linux-x86-64.so.2 (0x00007f3b91e7e000)
bruno@debian13:~/Works/langage_C/Math$
Compilation statique
bruno@debian13:~/Works/langage_C/Math$ gcc math.c -o math -lm -static -g
bruno@debian13:~/Works/langage_C/Math$ ls -lh
total 780K
-rwxrwxr-x 1 bruno bruno 775K 27 janv. 19:24 math
-rw-rw-r-- 1 bruno bruno 1,7K 27 janv. 19:21 math.c
bruno@debian13:~/Works/langage_C/Math$ ldd math
n'est pas un exécutable dynamique
bruno@debian13:~/Works/langage_C/Math$
#math_s est le code avec l'option -static !
bruno@debian13:~/Works/langage_C/Math$ time ./math_s
M_PI = 3.141592653589793
M_PI/2 = 1.570796326794897
--- Valeurs de base ---
sin(0) = 0.000000000000000
sin(M_PI/2) = 1.000000000000000
sin(M_PI) = 0.000000000000000
cos(0) = 1.000000000000000
cos(M_PI/2) = 0.000000000000000
cos(M_PI) = -1.000000000000000
--- Tangente ---
tan(0) = 0.000000000000000
tan(M_PI/2 - 0.0000001) = 9999999.988038061186671
tan(M_PI/2 + 0.0000001) = -10000000.000284528359771
--- Fonctions réciproques ---
asin(1) = 1.570796326794897 (devrait être M_PI/2)
acos(0) = 1.570796326794897 (devrait être M_PI/2)
atan(1) = 0.785398163397448 (devrait être M_PI/4)
atan(M_PI/2) = 1.003884821853887
--- Conversions ---
M_PI radians = 180 degres
M_PI/2 radians = 90.000000000000000 degres
45 degres = 0.785398163397448 radians
real 0m0,001s
user 0m0,001s
sys 0m0,000s
#math_d compilation dynamique
bruno@debian13:~/Works/langage_C/Math$ time ./math_d
M_PI = 3.141592653589793
M_PI/2 = 1.570796326794897
--- Valeurs de base ---
sin(0) = 0.000000000000000
sin(M_PI/2) = 1.000000000000000
sin(M_PI) = 0.000000000000000
cos(0) = 1.000000000000000
cos(M_PI/2) = 0.000000000000000
cos(M_PI) = -1.000000000000000
--- Tangente ---
tan(0) = 0.000000000000000
tan(M_PI/2 - 0.0000001) = 9999999.988038061186671
tan(M_PI/2 + 0.0000001) = -10000000.000284528359771
--- Fonctions réciproques ---
asin(1) = 1.570796326794897 (devrait être M_PI/2)
acos(0) = 1.570796326794897 (devrait être M_PI/2)
atan(1) = 0.785398163397448 (devrait être M_PI/4)
atan(M_PI/2) = 1.003884821853887
--- Conversions ---
M_PI radians = 180 degres
M_PI/2 radians = 90.000000000000000 degres
45 degres = 0.785398163397448 radians
real 0m0,002s
user 0m0,000s
sys 0m0,002s
bruno@debian13:~/Works/langage_C/Math$
on notera ici que le code statique est plus rapide que le dynamique!

