Académique Documents
Professionnel Documents
Culture Documents
TP N°2
Représentation interne des types de données Scalaires
Et types d'erreur
Objectif du TP :
Etudier les méthodes de représentation interne des types de données numériques scalaires
o Pour les entiers : Complément à 2
o Pour les réels : IEEE754 simple précision(float) et double précision(double).
Ecrire le code qui fait appel à l'API de Java pour effectuer les deux conversions suivantes:
o Représentation interne
o Déterminer les intervalles de données représentés par chacun des type
Comprendre les valeurs spéciales de float et double (NaN, infinity, -infinity).
Comprendre que les types float et double ne représentent pas un ensemble infini (continue) de valeurs
mais un ensemble fini (discret) de valeurs. Déterminer l'écart entre deux float et entre deux double
consécutifs.
Etudier le problème des débordements
Distinguer les 3 types d'erreurs dans un programme : de compilation, d'exécution et de logique
Bien comprendre la page 21 ci-dessous sur le site :
http://www.pitt.edu/~juy9/142/slides/L3-FP_Representation.pdf
Rappel:
Java fournit 8 types de données primitifs résumés dans le tableau ci-dessous :
Types de données primitifs
MotClé Description-Taille/Format Intervalle
Entiers
byte Entier signé sur 1octet en Cà2 -27 (-128) à + 27-1(127)
short Entier signé sur 2 octets en Cà2 -215(-32768)à + 215-1
int Entier signé sur 4 octets en Cà2 -231 à + 231-1
long Entier signé sur 8 octets en Cà2 -263 à + 263-1
Réels
Réel en virgule flottante simple précision sur -3.4..e38 à -1.4..e-45 (Partie négative)
float
4 octets (IEEE 754) 1.4..e-45 à 3.4..e38 (Partie positive)
Réel en virgule flottante double précision sur -1.7…e308 à -4.9…e-324 (Partie négative)
double
8 octets (IEEE 754) 4.9…e-324 à 1.7…e308 (Partie positive)
Autres types
char Un seul caractère sur 2 octets (Unicode) Code unicode de 0 à 65535
boolean Valeur boolèene sur 1 bit true or false
Pour les données de type float la notation IEEE754 simple précision utilise 32 bits:
B31 B30 B29 B28 B27 B26 B25 B24 B23 B22 B21 B20 B19 B18 B17 B16 B15 B14 B13 B12 B11 B10 B9 B8 B7 B6 B5 B4 B3 B2 B1 B0
B30 B29 B28 B27 B26 B25 B24 B23 : représentent l'exposant en excédent à 127.
B22 B21 B20 B19 B18 B17 B16 B15 B14 B13 B12 B11 B10 B9 B8 B7 B6 B5 B4 B3 B2 B1 B0 : représentent la partie fractionnaire de la
mantisse
La représentation normalisée a pour principal objectif de représenter un nombre par une seule représentation
binaire (un seul pattern de bits).
La représentation de l'exposant en excédent à 127 (en excédent à 1023 pour le double) permet de simplifier la
comparaison de deux floats. En effet si les exposants de deux floats (double) ne sont pas les mêmes, l'ordre de ces
exposants permet de déterminer l'ordre des floats (doubles) correspondants.
Le site ci-dessous permet de donner la représentation d'un float en IEEE754 Simple précision.
https://www.h-schmidt.net/FloatConverter/IEEE754.html
Exemple de représentation:
a-Représentation de +17F en IEEE754 Simple précision : 17(10) = 10001(2) = 1,0001 x 24(2)
Signe = 0 31
(bit de signe = 0 si nombre >0; =1 si nombre <0) 0
La représentation de -17 est identique à celle de +17 sauf que le bit de signe est 1 et non 0.
c-Représentation de 0.125F en IEEE754 Simple précision : +0.125(10) = 0.001 = 1,0 x 2-3(2)
31
Signe = 0 (Nombre >0)
0
31
Signe = 0 (Nombre >0)
0
22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Mantisse =
0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1
Remarque : le site ne donne pas la même valeur????
Exercice 3 : Représentation des valeurs spéciales de l'IEEE754 Simple précision
Le tableau ci-dessous donne la représentation des valeurs spéciales, 0, NaN, +infinity, -infinity , et des valeurs non
normalisées.
Ecrire une classe IEEE754SPVS (SPVS : Simple Précision Valeurs Spéciales) qui permet d'afficher la représentation
interne des valeurs spéciales d'un float : NaN, POSITVE_INFINITY (+infinity), NEGATIVE_INFINITY (-infinity) et d'une
valeur non normalisée comme -0.5x2-126 et 0.25 x 2-126 .
La classe doit aussi permettre de calculer ces valeurs à partir de leur représentation interne.
Représentation de 0
Représentation de -Infinity
La partie colorée en gris du tableau ci-dessous correspond aux float qui sont représentés avec la notation IEEE754
normalisée.
Ecrire une classe Java IEEE754SimplePrecisionValeursNormales qui permet, en utilisant l'API, de lire au clavier une
valeur de type float puis d'afficher sa représentation interne en binaire et en hexadécimale. La classe doit aussi
permettre de calculer la valeur décimale qui correspond à la représentation binaire d'un float. Pourriez-vous
déterminer la perte de précision causée par la représentation.
Exercice 6 : Ecrire une Classe RepresentationIEE754 à la manière dont est écrite la classe Math qui ne contient que
des méthodes statiques qui permettent d'obtenir les différentes informations sur le stockage interne des floats et
doubles.
API Java
Class Byte
public final class Byte
extends Number
implements Comparable<Byte>
The Byte class wraps a value of primitive type byte in an object. An object of type Byte contains a single field
whose type is byte
Field Summary
Class Short
public final class Short
extends Number
implements Comparable<Short>
The Short class wraps a value of primitive type short in an object. An object of type Short contains a single
field whose type is short.
Field Summary
Class Integer
public final class Integer
extends Number
implements Comparable<Integer>
The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a
single field whose type is int.
Field Summary
Method Summary
Returns a string representation of the integer argument as an unsigned integer in base 16.
Returns a string representation of the first argument in the radix specified by the second
argument.
Class Long
public final class Long
extends Number
implements Comparable<Long>
The Long class wraps a value of the primitive type long in an object. An object of type Long contains a single
field whose type is long.
Field Summary
Method Summary
Returns a string representation of the long argument as an unsigned integer in base 16.
Returns a string representation of the first argument in the radix specified by the second
argument.
Returns a string representation of the first argument as an unsigned integer value in the
radix specified by the second argument.
Class Float
public final class Float
extends Number
implements Comparable<Float>
The Float class wraps a value of primitive type float in an object. An object of type Float contains a single
field whose type is float.
Field Summary
A constant holding the largest positive finite value of type float, (2-2-23)·2127.
static int MIN_EXPONENT
A constant holding the smallest positive nonzero value of type float, 2 -149.
Method Summary
Returns a representation of the specified floating-point value according to the IEEE 754
floating-point "single format" bit layout.
Returns a representation of the specified floating-point value according to the IEEE 754
floating-point "single format" bit layout, preserving Not-a-Number (NaN) values.
Returns true if the argument is a finite floating-point value; returns false otherwise
(for NaN and infinity arguments).
boolean isInfinite()
Returns true if this Float value is infinitely large in magnitude, false otherwise.
Returns true if the specified number is infinitely large in magnitude, false otherwise.
boolean isNaN()
Returns true if the specified number is a Not-a-Number (NaN) value, false otherwise.
String toString()
Returns a Float object holding the float value represented by the argument string s.
Class Double
public final class Double
extends Number
implements Comparable<Double>
The Double class wraps a value of the primitive type double in an object. An object of type Double contains a
single field whose type is double.
Field Summary
A constant holding the largest positive finite value of type double, (2-2-52)·21023.
A constant holding the smallest positive nonzero value of type double, 2-1074.
Method Summary
Returns a representation of the specified floating-point value according to the IEEE 754
floating-point "double format" bit layout.
Returns a representation of the specified floating-point value according to the IEEE 754
floating-point "double format" bit layout, preserving Not-a-Number (NaN) values.
Returns true if the argument is a finite floating-point value; returns false otherwise
(for NaN and infinity arguments).
boolean isInfinite()
Returns true if this Double value is infinitely large in magnitude, false otherwise.
Returns true if the specified number is infinitely large in magnitude, false otherwise.
boolean isNaN()
Returns true if the specified number is a Not-a-Number (NaN) value, false otherwise.
Class Math
public final class Math
extends Object
The class Math contains methods for performing basic numeric operations such as the elementary exponential,
logarithm, square root, and trigonometric functions.
Method Summary
Computes the remainder operation on two arguments as prescribed by the IEEE 754
standard.
Returns the argument incremented by one, throwing an exception if the result overflows
an int.
Returns the argument incremented by one, throwing an exception if the result overflows
a long.
Returns the product of the arguments, throwing an exception if the result overflows an int.
Returns the product of the arguments, throwing an exception if the result overflows
a long.
Returns the negation of the argument, throwing an exception if the result overflows an int.
static long negateExact(long a)
Returns the negation of the argument, throwing an exception if the result overflows a long.
Returns the floating-point number adjacent to the first argument in the direction of the
second argument.
Returns the floating-point number adjacent to the first argument in the direction of the
second argument.
Returns the value of the first argument raised to the power of the second argument.
Returns the difference of the arguments, throwing an exception if the result overflows
an int.
Returns the difference of the arguments, throwing an exception if the result overflows
a long.