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

f64::from(bool) and f32::from(bool) #74015

Closed
Tracked by #84
leonardo-m opened this issue Jul 3, 2020 · 7 comments
Closed
Tracked by #84

f64::from(bool) and f32::from(bool) #74015

leonardo-m opened this issue Jul 3, 2020 · 7 comments
Labels
A-floating-point Area: Floating point numbers and arithmetic C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@leonardo-m
Copy link

I think f64::from(bool) and f32::from(bool) could be added. Currently I perform that in two steps:

f64::from(u32::from(bool))
f32::from(u16::from(bool))

@hellow554
Copy link
Contributor

Funny enough, the people are aware of this problem:

error[E0606]: casting `bool` as `f64` is invalid
 --> src/main.rs:3:20
  |
3 |     println!("{}", true as f64);
  |                    ^^^^^^^^^^^
  |
  = help: cast through an integer first

I think there's a valid reason for this, but let's see

@jonas-schievink jonas-schievink added C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue. A-floating-point Area: Floating point numbers and arithmetic labels Jul 3, 2020
@Mark-Simulacrum
Copy link
Member

Integer casts are essentially no-ops since the bit pattern is the same, but casting to a float would be less obvious/straightforward, IMO.

I guess you expect 1.0 as the result, same as casting through an integer gets you?

@leonardo-m
Copy link
Author

I expect f64::from(bool) to have about the same semantics of f64::from(u32::from(bool)), yes.

@workingjubilee
Copy link
Member

Which zero to return?

@leonardo-m
Copy link
Author

The same as f32::from(0_u8), that should be a +0.0.

@Kixunil
Copy link
Contributor

Kixunil commented Mar 9, 2023

Doc says it's implemented, so this should be closed?

But what the actual hell?! bool and a number are conceptually different, why allow any of those conversions? IMO the only sensible conversion is if x { 1 } else { 0 } (with compiler optimization). This is converting apples into eggs... Anyway, the ship has sailed.

@est31
Copy link
Member

est31 commented Mar 9, 2023

This should be closed now, yes. Implementation PR was #100390.

@dtolnay dtolnay closed this as completed Mar 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-floating-point Area: Floating point numbers and arithmetic C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

8 participants