Commit a7fd052
authored
Canonicalize NaNs when casting between float and double (#8645)
Fixes #8626. Also part of #8261.
The bug is due to the fact that C++ doesn't guarantee a bitwise
representation for NaNs that are casted from double -> float or vice
versa. From
https://en.cppreference.com/cpp/language/implicit_conversion:
> Floating-point promotion
A
[prvalue](https://en.cppreference.com/cpp/language/value_category#prvalue)
of type float can be converted to a prvalue of type double. The value
does not change.
In the case of NaNs, the value (NaN) is preserved regardless of the
particular NaN that is picked, so we have no guarantee of the bitwise
representation. And for double -> float:
> If the conversion is listed under floating-point promotions, it is a
promotion and not a conversion.
> * If the source value can be represented exactly in the destination
type, it does not change.
> * If the source value is between two representable values of the
destination type, the result is one of those two values (it is
implementation-defined which one, although if IEEE arithmetic is
supported, rounding defaults [to
nearest](https://en.cppreference.com/cpp/numeric/fenv/FE_round)).
> * Otherwise, the behavior is undefined.
I assume NaN conversions again fall under case 1 meaning that the
"value" (NaN) is preserved but not the bitwise representation.
Canonicalize NaNs after promotion or demotion to ensure that the quiet
bit remains set.1 parent 35ba23c commit a7fd052
1 file changed
Lines changed: 4 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
864 | 864 | | |
865 | 865 | | |
866 | 866 | | |
867 | | - | |
| 867 | + | |
868 | 868 | | |
869 | 869 | | |
870 | 870 | | |
| |||
1164 | 1164 | | |
1165 | 1165 | | |
1166 | 1166 | | |
1167 | | - | |
| 1167 | + | |
1168 | 1168 | | |
1169 | 1169 | | |
1170 | 1170 | | |
| |||
2786 | 2786 | | |
2787 | 2787 | | |
2788 | 2788 | | |
2789 | | - | |
| 2789 | + | |
| 2790 | + | |
2790 | 2791 | | |
2791 | 2792 | | |
2792 | 2793 | | |
| |||
0 commit comments