From e0cf06fbe76117b87a6d6f2e3d4cec8b825261f8 Mon Sep 17 00:00:00 2001 From: Francois Lesueur Date: Tue, 6 Sep 2022 10:17:24 +0200 Subject: [PATCH] ajout exemples de code cm2 --- cm2-nombres-code/char.c | 12 ++++++++++++ cm2-nombres-code/char2.c | 12 ++++++++++++ cm2-nombres-code/double.c | 11 +++++++++++ cm2-nombres-code/int.c | 11 +++++++++++ cm2-nombres.md | 12 ++++++------ 5 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 cm2-nombres-code/char.c create mode 100644 cm2-nombres-code/char2.c create mode 100644 cm2-nombres-code/double.c create mode 100644 cm2-nombres-code/int.c diff --git a/cm2-nombres-code/char.c b/cm2-nombres-code/char.c new file mode 100644 index 0000000..8741462 --- /dev/null +++ b/cm2-nombres-code/char.c @@ -0,0 +1,12 @@ +#include + +void main() +{ + unsigned char a = 155; + unsigned char b = 155; + unsigned char c = a + b; + + printf("Value of c is: %d\n",c); + + +} diff --git a/cm2-nombres-code/char2.c b/cm2-nombres-code/char2.c new file mode 100644 index 0000000..e615001 --- /dev/null +++ b/cm2-nombres-code/char2.c @@ -0,0 +1,12 @@ +#include + +void main() +{ + char a = 127; + char b = 1; + char c = a + b; + + printf("Value of c is: %d\n",c); + + +} diff --git a/cm2-nombres-code/double.c b/cm2-nombres-code/double.c new file mode 100644 index 0000000..262f0c5 --- /dev/null +++ b/cm2-nombres-code/double.c @@ -0,0 +1,11 @@ +#include + +void main() +{ + double a = 0.1; + double b = 0.2; + double c = a + b; + + printf("Value of c is: %.20f\n",c); + +} diff --git a/cm2-nombres-code/int.c b/cm2-nombres-code/int.c new file mode 100644 index 0000000..096031c --- /dev/null +++ b/cm2-nombres-code/int.c @@ -0,0 +1,11 @@ +#include + +void main() +{ + int a = 0.1; + int b = 0.2; + int c = a + b; + + printf("Value of c is: %d\n",c); + +} diff --git a/cm2-nombres.md b/cm2-nombres.md index cab7df3..6f832e7 100644 --- a/cm2-nombres.md +++ b/cm2-nombres.md @@ -49,7 +49,7 @@ La notion de type Les entiers =========== -Ici, 0.1 + 0.2 = 0 (int.c) +Ici, 0.1 + 0.2 = 0 ([int.c](cm2-nombres-code/int.c)) Représentation des entiers positifs ----------------------------------- @@ -57,11 +57,11 @@ Représentation des entiers positifs - Changement de base "simple" (TD2) - 15510 -> 1001 10112 (0x9B) -Exemple simple d'addition (char.c) : +Exemple simple d'addition : - 155 + 3 = 158 - 1001 1011 + 0000 0011 = 1001 1110 -Mais le débordement : +Mais le débordement ([char.c](cm2-nombres-code/char.c)) : - 155 + 155 = 310 - **MAIS** 1001 1011 + 1001 1011 = 1 0011 0110 -> 0011 0110 = 54 (= 310 - 256) @@ -74,7 +74,7 @@ Représentation des entiers relatifs - Sur 1 octet : 1 bit de signe, 7 bits de valeur - 1001 1011 -> -27 ;-) - Convention d'interprétation du binaire par le type... - - 0000 0000 -> 0, 1000 000 -> -0 => 0.1 + 0.2 == -0 (int2.c) + - 0000 0000 -> 0, 1000 000 -> -0 => 0.1 + 0.2 == -0 - Pas très pratique... - Complément à 2 @@ -88,7 +88,7 @@ Représentation des entiers relatifs - les opérations sont identiques à celles pour un entier non signé - Détails [ici](https://fr.wikipedia.org/wiki/Compl%C3%A9ment_%C3%A0_deux) -- Débordement (char2.c) : +- Débordement ([char2.c](cm2-nombres-code/char2.c)) : - 127 + 1 = 128 - 0111 1111 + 0000 0001 = 1000 0000 -> -128 @@ -96,7 +96,7 @@ Représentation des entiers relatifs Les réels ========= -Ici, 0.1 + 0.2 = 0.30000000000004 ([détails ici](https://0.30000000000000004.com/)) (double.c) +Ici, 0.1 + 0.2 = 0.30000000000004 ([détails ici](https://0.30000000000000004.com/)) ([double.c](cm2-nombres-code/double.c)) Virgule fixe