Skip to content

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

Closed
Listed in
Closed
@leonardo-m

Description

@leonardo-m

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))

Activity

hellow554

hellow554 commented on Jul 3, 2020

@hellow554
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

added
C-feature-requestCategory: A feature request, i.e: not implemented / a PR.
T-langRelevant to the language team
A-floating-pointArea: Floating point numbers and arithmetic
on Jul 3, 2020
Mark-Simulacrum

Mark-Simulacrum commented on Jul 3, 2020

@Mark-Simulacrum
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

leonardo-m commented on Jul 4, 2020

@leonardo-m
Author

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

workingjubilee

workingjubilee commented on Apr 29, 2021

@workingjubilee
Member

Which zero to return?

leonardo-m

leonardo-m commented on Apr 29, 2021

@leonardo-m
Author

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

Kixunil

Kixunil commented on Mar 9, 2023

@Kixunil
Contributor

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

est31 commented on Mar 9, 2023

@est31
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-floating-pointArea: Floating point numbers and arithmeticC-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @hellow554@Kixunil@jonas-schievink@dtolnay@Mark-Simulacrum

        Issue actions

          f64::from(bool) and f32::from(bool) · Issue #74015 · rust-lang/rust