178 lines
2.9 KiB
Markdown
178 lines
2.9 KiB
Markdown
# Exemples / DO-DON'T — Chapitre 24 — Casts et conversions
|
|
|
|
> Document compagnon. Les exemples illustrent les règles du chapitre ; la formulation normative reste dans le chapitre lui-même.
|
|
|
|
## Exemples extraits du chapitre
|
|
|
|
### Exemple 1
|
|
|
|
```text
|
|
conversion de valeur
|
|
cast de hiérarchie nominale
|
|
bitcast de représentation binaire
|
|
```
|
|
|
|
### Exemple 2
|
|
|
|
```text
|
|
int8 small = ...;
|
|
int32 large = small; // ERROR
|
|
int32 explicitLarge = small::toInt32(); // OK
|
|
```
|
|
|
|
### Exemple 3
|
|
|
|
```text
|
|
Dog dog = ...;
|
|
Animal animal = dog;
|
|
Serializable serializable = dog;
|
|
```
|
|
|
|
### Exemple 4
|
|
|
|
```text
|
|
Animal animal = ...;
|
|
|
|
if (animal is Dog) {
|
|
// animal est raffiné en Dog ici.
|
|
}
|
|
```
|
|
|
|
### Exemple 5
|
|
|
|
```text
|
|
toTarget()
|
|
conversion exacte et totale
|
|
|
|
tryToTarget()
|
|
conversion exacte pour la valeur courante, récupérable si impossible
|
|
|
|
roundToTarget()
|
|
perte de précision explicitement acceptée, conversion totale
|
|
|
|
tryRoundToTarget()
|
|
perte de précision explicitement acceptée, mais conversion pouvant échouer
|
|
|
|
saturateToTarget()
|
|
saturation explicite lorsqu'aucun arrondi supplémentaire n'est nécessaire
|
|
|
|
saturatingRoundToTarget()
|
|
arrondi + saturation explicitement annoncés
|
|
|
|
wrapToTarget()
|
|
wrapping entier explicite
|
|
```
|
|
|
|
### Exemple 6
|
|
|
|
```text
|
|
tryToIntXX()
|
|
tryToUintXX()
|
|
```
|
|
|
|
### Exemple 7
|
|
|
|
```text
|
|
floor()
|
|
ceil()
|
|
round()
|
|
truncate()
|
|
```
|
|
|
|
### Exemple 8
|
|
|
|
```text
|
|
value::floor()::tryToInt32()
|
|
value::ceil()::tryToInt32()
|
|
value::round()::tryToInt32()
|
|
value::truncate()::tryToInt32()
|
|
```
|
|
|
|
### Exemple 9
|
|
|
|
```text
|
|
value::floor()::trySaturateToInt32()
|
|
value::round()::tryWrapToInt32()
|
|
```
|
|
|
|
### Exemple 10
|
|
|
|
```text
|
|
NaN -> NaN
|
|
+Infinity -> +Infinity
|
|
-Infinity -> -Infinity
|
|
+0 -> +0
|
|
-0 -> -0
|
|
```
|
|
|
|
### Exemple 11
|
|
|
|
```text
|
|
payload NaN
|
|
quiet/signaling bit
|
|
signe du NaN
|
|
représentation binaire exacte
|
|
```
|
|
|
|
### Exemple 12
|
|
|
|
```text
|
|
tryToFloatXX()
|
|
exige une représentation exacte
|
|
|
|
tryRoundToFloatXX()
|
|
accepte l'arrondi canonique
|
|
refuse une valeur finie hors domaine fini destination
|
|
|
|
saturatingRoundToFloatXX()
|
|
accepte l'arrondi canonique
|
|
sature une valeur finie hors domaine vers +/-Target::Max
|
|
```
|
|
|
|
### Exemple 13
|
|
|
|
```text
|
|
NotFinite
|
|
NotIntegral
|
|
OutOfRange
|
|
Inexact
|
|
```
|
|
|
|
### Exemple 14
|
|
|
|
```text
|
|
NotFinite
|
|
NaN ou +/-Infinity lorsqu'une valeur finie est requise
|
|
|
|
NotIntegral
|
|
float -> integer alors que la valeur n'est pas mathématiquement entière
|
|
|
|
OutOfRange
|
|
valeur mathématique hors du domaine de la destination
|
|
|
|
Inexact
|
|
valeur dans le domaine destination mais non représentable exactement
|
|
alors que l'opération exige l'exactitude
|
|
```
|
|
|
|
### Exemple 15
|
|
|
|
```text
|
|
sourceType
|
|
targetType
|
|
sourceValue
|
|
timestamp
|
|
backend
|
|
roundingMode
|
|
```
|
|
|
|
### Exemple 16
|
|
|
|
```text
|
|
annexes/A-numeric-conversions.md
|
|
```
|
|
|
|
## DO / DON'T / WHY / compiler error / edge cases
|
|
|
|
À consolider progressivement avant la baseline publique V3 et à transformer, lorsque pertinent, en tests de conformité de la toolchain.
|