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

Exact casts from float to int? #8

Open
BartMassey opened this issue Mar 20, 2021 · 5 comments
Open

Exact casts from float to int? #8

BartMassey opened this issue Mar 20, 2021 · 5 comments

Comments

@BartMassey
Copy link
Contributor

BartMassey commented Mar 20, 2021

Unless I'm missing something, casts from float to int are currently missing. This is too bad, as these are some of the most problematic casts as provides. (Edit: I was missing something. See below.)
Suggested semantics are to only convert from floats that are exact integers (only zero bits after the decimal point) that will fit in the target int.

No prize for guessing what Rust does with (0.0f32 / 0.0) as u32. 🙂

@dhardy
Copy link
Contributor

dhardy commented Mar 20, 2021

They're not missing, they're just written differently: x.cast_nearest(), x.cast_floor() etc.

Yes, as fails to fail and converts not-a-number into a number...

@BartMassey BartMassey changed the title Casts from float to int? Exact casts from float to int? Mar 20, 2021
@BartMassey
Copy link
Contributor Author

Ah yes, I'd forgotten. Apologies.

Could we add .cast_exact() or somesuch to the possibilities, that only casts if the "float" is already an exact integer? Or add this case to plain ol' .cast()? Maybe it's not worth the work, but I can think of some algorithms that would be surprised if their floating calculations didn't yield an integer. Ofc given floating rounding errors I suppose the next thing would be a delta…

Maybe .cast_delta(delta: float). Or maybe just make me check ranges before calling .cast_nearest() and call it a day.

Feel free to close this as wontfix, which is probably the right answer. Again, apologies.

@BartMassey
Copy link
Contributor Author

BartMassey commented Mar 20, 2021

Yes, as fails to fail and converts not-a-number into a number...

But what number? 🙂 I had a guess that turned out to be right, since there's only one "reasonable" choice; the Rust reference confirms it as canon. Ugh. I mean, I guess the Romans thought it was not a number, which should be good enough for me.

Off-topic, but I find the whole thing as funny as it is sad. Very grateful for your work on this crate, which I plan to use as my go-to tool for numeric conversions henceforth.

@dhardy
Copy link
Contributor

dhardy commented Mar 20, 2021

that only casts if the "float" is already an exact integer

It's a bit too easy to end up with floats that are not quite ints though IMO, e.g.:

fn main() {
    let x = (1.0 - 0.9) * 10.0;
    println!("{}", x);
    println!("x == 1: {}", x == 1.0);
}

But what number? slightly_smiling_face

Any number? Define f(x) = nx, then f(x)/x == n for all x != 0. If you try extending that to 0 you get 0/0 == n.

@dhardy
Copy link
Contributor

dhardy commented Jul 13, 2021

So...

Could we add .cast_exact() or somesuch to the possibilities, that only casts if the "float" is already an exact integer?

Technically, yes. Practically, I'm not sure if it's sufficiently useful. Perhaps we'll see if there are further requests / justifications.

Maybe .cast_delta(delta: float)

This isn't an approximate-equality library so my gut feeling is that it doesn't belong here, though that's not a big thing to add.

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