Bonsoir à tous,
En me repenchant sur les limites des types, je bute sur une question par rapport aux limites des types flottants : s'il ne fait pas de doute que l'affectation d'un nombre dépassant sa borne négative ou positive constitue un dépassement, quid d'un nombre trop proche de zéro pour être représenté ?
Autrement dit, le code ci-dessous réalise-t-il un dépassement de capacité ou bien s'agit-il d'un ajustement à la « valeur la plus proche représentable » (zéro étant situé dans les bornes du type) ? La seconde hypothèse me paraît la plus plausible, mais je n'ai aucune certitude sur ce point.
Des avis sur la question ?
1 2 3 4 5 6 7 8 9 10 | #include <stdio.h> int main(void) { float f = 1e-308; /* Dépassement ? */ printf("%f\n", f); /* 0.000000 */ return 0; } |
When a value of real floating type is converted to a real floating type, if the value being converted can be represented exactly in the new type, it is unchanged. If the value being converted is in the range of values that can be represented but cannot be represented exactly, the result is either the nearest higher or nearest lower representable value, chosen in an implementation-defined manner. If the value being converted is outside the range of values that can be represented, the behavior is undefined. Results of some implicit conversions may be represented in greater range and precision than that required by the new type (see 6.3.1.8 and 6.8.6.4).
ISO/IEC 9899:2011, doc. N1570, 6.3.1.5, Real floatting types, p. 52