# Pre-RFC: Add explicitly-named numeric conversion APIs

**URL:** <https://internals.rust-lang.org/t/pre-rfc-add-explicitly-named-numeric-conversion-apis/11395>\
**Category:** libs\
**Created:** [November 29, 2019, 1:31pm UTC](https://internals.rust-lang.org/t/pre-rfc-add-explicitly-named-numeric-conversion-apis/11395 "2019-11-29T13:31:34Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![Shnatsel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/shnatsel/32/4613_2.png) [@Shnatsel](https://internals.rust-lang.org/u/Shnatsel)\
**Post date:** [November 29, 2019, 2:20pm UTC](https://internals.rust-lang.org/t/pre-rfc-add-explicitly-named-numeric-conversion-apis/11395/4 "2019-11-29T14:20:44Z")

</div>

I find the behavior of `NaN` being silently turned into `0` on float-to-int conversion **very surprising.** It feels like a major footgun. I suggest making this method failible, just like `try_into()`, and returning error on NaN and possibly `+/-inf`.

Looking through the original issue, it seems this has been [already proposed](https://github.com/rust-lang/rust/issues/10184#issuecomment-559298292) and discussed in detail. It seems that the CPUs allow efficient detection of `inf` and `NaN` conversions to integer, too. It was not an option for `as` keyword since that must be infallible, but sounds like a great option for this API.

**Bikeshedding ahead:** It is also surprising to have `modulo_to` produce a negative value - anything called "modulo" should return only non-negative numbers in my mind. It is also inconsistent with `try_into()`. I suggest calling it `truncate_into()`. Also, use of the word "round" in float-to-float conversions is surprising as it's already reserved for turning a float into an integer - see e.g. the built-in `round()` function.

Other than that, looks _very_ good to me! I would be glad to see this in the language.

---

_[View the full topic](https://internals.rust-lang.org/t/pre-rfc-add-explicitly-named-numeric-conversion-apis/11395)._
