You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Variant] Allow lossless casting from integer to floating point (#8357)
# Which issue does this PR close?
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax.
- Closes #NNN.
# Rationale for this change
Historically, `Variant::as_fXX` methods don't even try to cast int
values as floating point, which is counter-intuitive.
# What changes are included in this PR?
Allow lossless casting of variant integer values to variant floating
point values, by a naive determination of precision:
* Every floating point number has some number of bits of precision
* 53 (double)
* 24 (single)
* 11 (half)
* Any integer that fits entirely inside the target floating point type's
precision can be converted losslessly
* This produces an intuitive result: "too big" numbers fail to convert,
while "small enough" numbers do convert.
* This is a sufficient but _not_ a necessary condition.
* Technically, wider integer can be represented losslessly as well, as
long as they have enough trailing zeros
* It's unclear whether allowing those wider values to cast is actually
helpful in practice, because only 1 in 2**k values can cast (where k is
the number of bits of excess precision); it would certainly make input
testing more expensive.
# Are these changes tested?
New unit tests and doc tests.
# Are there any user-facing changes?
Yes. Values that failed to cast before now succeed.
0 commit comments