[Pre-ACP] `TryFrom` for exact float-int conversions

Previous attempts and discussion, note that this was before casting floats to ints was made sound.

https://internals.rust-lang.org/t/how-should-we-provide-fallible-float-to-int-conversions/6708


Since there isn't one obvious implementation of TryFrom, that seems too contentious to pursue.

Taking recent features like exact_div and exact shifts as naming inspiration, this might be better proposed as something like (checked_)to_float_exact and (checked_)to_int_exact for integers and floats of the same bit size?
The naming would better indicate that no rounding is taking place.

I've wanted this behavior in the past but never got around to writing an ACP until now. It relates to this topic by adding associated constants to floats for the max integers that they can represent. Those constants can be used as building blocks for handling lossless conversion.
https://github.com/rust-lang/libs-team/issues/713

For float-int conversions, a decision would need to be made whether exact conversion for values larger than ±2.pow(<$float>::MANTISSA_DIGITS) are valid. I would lean towards those returning nothing or an error.

2 Likes