Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document and/or allow truncation/rounding when casting from float to int? #296

Closed
JulianKnodt opened this issue Nov 9, 2023 · 4 comments

Comments

@JulianKnodt
Copy link

When casting from a float to an int, currently it follows the default approach of truncating the digits.
I think it may be worth more explicit in the docs that this is the default behavior.
In addition, for some cases it may be worth letting the caller dictate the rounding behavior, as right now in a generic context it's not possible to control how it's handled, unless I use some hacky check after conversion.

@JulianKnodt JulianKnodt changed the title Document and/or allow different behavior when casting from float to int? Document and/or allow truncation/rounding when casting from float to int? Nov 9, 2023
@cuviper
Copy link
Member

cuviper commented Nov 9, 2023

Doc improvements are welcome in PRs.

it may be worth letting the caller dictate the rounding behavior,

That sounds like a whole new API! Maybe you could implement your own wrapper with different floating point behavior, and I found an example of that in a different crate with az::Round. Another option I found is numeric_cast with multiple traits available.

@JulianKnodt
Copy link
Author

I'm currently working with num_traits inside of a crate I don't own (see linked PR), and it is using ToPrimitive in order to cast between f32 to either an integer or a float (specified only as an impl Primitive). If it is a float that it is being cast to, I don't want to do any rounding. While the library could change to use a different trait for their numeric types, it's a bit invasive of a change, and it feels natural that conversion between primitives should be able to control their rounding mode.

For the specific issue, there's workarounds, but I think it was worth asking here since it seems to be the most natural place to change it. If you don't think it should be added though, that's fine

@cuviper
Copy link
Member

cuviper commented Nov 10, 2023

If it is a float that it is being cast to, I don't want to do any rounding.

This isn't generally possible to guarantee either. f64 to f32 definitely has to round, but even integer to float will round if the value is greater than the mantissa bits can represent.

In any case, it's not possible to control rounding with the current API. A new rounding API would need to be proposed.

@JulianKnodt
Copy link
Author

ah yes, currently I'm doing f32 -> Generic which would not require rounding.

Sounds good. I don't think I'll propose such an API myself, so for now I'll just close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants