Les types entiers en C++#
#include <climits>
#include <cmath>
Type unsigned short int#
unsigned short int u;
u = 1
1
type: unsigned short
u = 10000
10000
type: unsigned short
u = 100000
input_line_15
:
2
:
6
:
warning
:
implicit conversion from 'int' to 'unsigned short' changes value from 100000 to 34464 [-Wconstant-conversion]
u = 100000
~ ^~~~~~
34464
type: unsigned short
USHRT_MAX
65535
type: int
pow(2,16) - 1
65535.000
type: double
u = 65533
65533
type: unsigned short
u = u + 1
65534
type: unsigned short
u = u + 1
65535
type: unsigned short
u = u + 1
0
type: unsigned short
u = u - 1
65535
type: unsigned short
Type signed short int#
short int s;
s = 1
1
type: short
s = 1000
1000
type: short
s = 100000
input_line_36
:
2
:
6
:
warning
:
implicit conversion from 'int' to 'short' changes value from 100000 to -31072 [-Wconstant-conversion]
s = 100000
~ ^~~~~~
-31072
type: short
Quel est le plus grand entier que l'on peut représenter avec un short int?
SHRT_MAX
32767
type: int
s = SHRT_MAX
32767
type: short
Que se passe-t-il si on lui ajoute 1?
s = s + 1
-32768
type: short
s = SHRT_MIN
-32768
type: short
s -= 1
32767
type: short
Conséquence amusante:
s = SHRT_MIN
-32768
type: short
s = -s
-32768
type: short
s = abs(s)
-32768
type: short
Type int#
int i;
i = INT_MAX
2147483647
type: int
i + 1
-2147483648
type: int
i = INT_MIN
-2147483648
type: int
-i
-2147483648
type: int
Type long#
long l = LONG_MAX
l + 1
-9223372036854775808
type: long
l = LONG_MIN
-9223372036854775808
type: long
l - 1
9223372036854775807
type: long
- l
-9223372036854775808
type: long
À retenir#
Les entiers des types
int,long, ... sont des entiers machine; ce sont des approximations des entiers mathématiques
Il est possible de représenter les entiers mathématiques arbitraires avec un type adéquat
De même pour
float,double, ...: ce sont des approximations des nombres réèls
Il n'est pas possible de représenter tous les nombres réels!