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

**URL:** <https://internals.rust-lang.org/t/pre-acp-tryfrom-for-exact-float-int-conversions/23828>\
**Category:** language design\
**Created:** [December 16, 2025, 10:16pm UTC](https://internals.rust-lang.org/t/pre-acp-tryfrom-for-exact-float-int-conversions/23828 "2025-12-16T22:16:17Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![okaneco](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/okaneco/32/12920_2.png) [@okaneco](https://internals.rust-lang.org/u/okaneco)\
**Post date:** [December 19, 2025, 1:49am UTC](https://internals.rust-lang.org/t/pre-acp-tryfrom-for-exact-float-int-conversions/23828/4 "2025-12-19T01:49:24Z")

</div>

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

> <https://github.com/rust-lang/rust/pull/47857>
>
> This implements \`TryFrom\` for \`f32\`, and \`f64\` to all integer types.
> 
> This is …accomplished by casting the floating point number, into the target type and back, and then comparing if the resulting value is identical to the converted value.
> 
> I'm not sure this implementation is sound, in particular issues like #10184 seem to indicate that these kinds of casts are not always sound. A review by someone who knows more about floating point numbers, and what kind of guarantees the casts give us would be highly appreciated.
> 
> If there is a better way to determine if a given floating point number can be converted into an integer, I'd also love to know.

[https://internals.rust-lang.org/t/how-should-we-provide-fallible-float-to-int-conversions/6708](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](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.

---

_[View the full topic](https://internals.rust-lang.org/t/pre-acp-tryfrom-for-exact-float-int-conversions/23828)._
