tadata

Nombre à virgule flottante

Programmation · Intermédiaire

La façon dont les ordinateurs stockent les nombres décimaux de manière approchée : d'où des résultats comme 0,1 + 0,2 = 0,30000000000000004.

Comment ça marche

Un flottant est écrit en binaire comme une mantisse et un exposant (norme IEEE 754) ; un double occupe 64 bits, soit environ 15 à 16 chiffres significatifs. 0,1 n'a pas d'écriture binaire exacte, comme 1/3 n'en a pas en décimal. On ne compare donc jamais deux flottants avec ==, et pour l'argent on utilise des entiers en centimes ou un type décimal.

Valeur d'un double IEEE 754 (cas normal)
tadata
nombre représenté
bit de signe (0 ou 1)
fraction de la mantisse, sur 52 bits (de 0 inclus à 1 exclu)
exposant stocké, sur 11 bits (de 1 à 2046)

Exemple, , : .

tadata
x = 0.1 sign exponent mantissa (fraction) fp32 1 + 8 + 23 bits 0x3DCCCCCD 0 0 1 1 1 1 0 1 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 1 s = 0 (+) · E = 01111011₂ = 123 → exponent 123 − 127 = −4, scale 2⁻⁴ · M = 5033165 → 1 + 5033165/8388608 stored x′ = 0.100000001 · abs err 1.49×10⁻⁹ · rel err 1.49×10⁻⁸ fp16 1 + 5 + 10 bits 0x2E66 0 0 1 0 1 1 1 0 0 1 1 0 0 1 1 0 s = 0 (+) · E = 01011₂ = 11 → exponent 11 − 15 = −4, scale 2⁻⁴ · M = 614 → 1 + 614/1024 stored x′ = 0.0999755859 · abs err 2.44×10⁻⁵ · rel err 0.000244 format exp / mant bits max finite min normal min subnormal ε fp32 8 / 23 3.4028×10³⁸ 1.175×10⁻³⁸ 1.401×10⁻⁴⁵ 1.192×10⁻⁷ fp16 5 / 10 65504 6.104×10⁻⁵ 5.96×10⁻⁸ 0.0009766 normal (0 < E < all ones): x′ = (−1)^s × 2^(E − bias) × (1 + M / 2^m) subnormal (E = 0): x′ = (−1)^s × 2^(1 − bias) × (M / 2^m) · bias = 2^(e − 1) − 1 · ε = 2^(−m) abs err = |x′ − x| · rel err = |x′ − x| / |x| where x = the value to store; x′ = the value actually stored after rounding; s = sign bit (0 = +, 1 = −); E = exponent field read as an unsignedinteger; M = mantissa (fraction) field read as an unsigned integer; e, m = number of exponent and mantissa bits; bias = exponent offset; ε = machineepsilon: gap between 1 and the next representable number Pourquoi 0,1 n'est pas stocké exactement
x = 0.1 sign exponent mantissa (fraction) fp32 1 + 8 + 23 bits 0x3DCCCCCD 0 0 1 1 1 1 0 1 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 1 s = 0 (+) · E = 01111011₂ = 123 → exponent 123 − 127 = −4, scale 2⁻⁴ · M = 5033165 → 1 + 5033165/8388608 stored x′ = 0.100000001 · abs err 1.49×10⁻⁹ · rel err 1.49×10⁻⁸ fp16 1 + 5 + 10 bits 0x2E66 0 0 1 0 1 1 1 0 0 1 1 0 0 1 1 0 s = 0 (+) · E = 01011₂ = 11 → exponent 11 − 15 = −4, scale 2⁻⁴ · M = 614 → 1 + 614/1024 stored x′ = 0.0999755859 · abs err 2.44×10⁻⁵ · rel err 0.000244 format exp / mant bits max finite min normal min subnormal ε fp32 8 / 23 3.4028×10³⁸ 1.175×10⁻³⁸ 1.401×10⁻⁴⁵ 1.192×10⁻⁷ fp16 5 / 10 65504 6.104×10⁻⁵ 5.96×10⁻⁸ 0.0009766 normal (0 < E < all ones): x′ = (−1)^s × 2^(E − bias) × (1 + M / 2^m) subnormal (E = 0): x′ = (−1)^s × 2^(1 − bias) × (M / 2^m) · bias = 2^(e − 1) − 1 · ε = 2^(−m) abs err = |x′ − x| · rel err = |x′ − x| / |x| where x = the value to store; x′ = the value actually stored after rounding; s = sign bit (0 = +, 1 = −); E = exponent field read as an unsignedinteger; M = mantissa (fraction) field read as an unsigned integer; e, m = number of exponent and mantissa bits; bias = exponent offset; ε = machineepsilon: gap between 1 and the next representable number Pourquoi 0,1 n'est pas stocké exactement

Qui l'utilise, et pourquoi

Tout calcul scientifique, graphique et d'IA (FP16, FP32) ; un piège classique en finance.

Termes liés

Autres termes : Programmation

Accessibilité numérique (a11y, WCAG)Adaptateur (patron de conception)Agile et ScrumAlgorithmes de triAnalyse statique de codeApplication web progressive (PWA)Arbre binaire de rechercheArbre et grapheAssistant de programmation IAAsync / awaitBibliothèque et frameworkBoucle d'événementsBundler et tree shakingBytecode et JIT

Nous utilisons des cookies analytiques pour améliorer votre expérience. Aucune donnée personnelle n'est collectée.