-
Notifications
You must be signed in to change notification settings - Fork 31
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
Add better numerical type conversion functions #52
Comments
Yes I agree. Back then, But it's true that we now have I think
I must confess I did not understand what you mean in this part; A bit more detail would help (thanks in advance and sorry!) |
This should be fixed; Although I close the issue with my commit, feel free to add to the discussion regardless. I'll push a new version with this change very soon. |
I mean, euclid has Also, how do you think about adding |
Oh ! Right, I did not think of
Okay, I see. I don't think I would like an equivalent to An advantage of explicit unwrapping is that you can quickly pinpoint the parts of your codebase that could panic because of this, which is somewhat harder when it's hidden in a library function.
From personal experience, this isn't ideal; bloating the library with as_foo for every primitive type isn't worth the benefit of avoiding typing To me, it's a balance between "there should be only one way to do this kind of task" (e.g keep I'm imagining adding |
I understand. For me, |
Writing
vec.numcast().unwrap()
is really noisy, especially where I don't expect any conversion errors and don't want to handle them. To avoid unnecessary checks and panics I keep writingvec.map(|v| v as f32)
which is still not ideal...So I suggest adding a new function for conversion between different numerical types, using
num_traits::cast::AsPrimitive
. In most cases, precision loss and overflowing isn't something you should worry about, especially in graphics programming (e.g. converting u32 framebuffer extent to f32).Perhaps, the old
numcast
function should be renamed totry_cast
and a new panickingcast
be introduced to match euclid behaviour. But that's another question...The text was updated successfully, but these errors were encountered: