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

Convertion of negative f64 to u64. #72899

Closed
aslpavel opened this issue Jun 1, 2020 · 6 comments
Closed

Convertion of negative f64 to u64. #72899

aslpavel opened this issue Jun 1, 2020 · 6 comments
Labels
C-bug Category: This is a bug.

Comments

@aslpavel
Copy link

aslpavel commented Jun 1, 2020

I tried this code:

let v: f64 = -10.0;
println!("{}", v as u64);
let v: f32 = -10.0;
println!("{}", v as u64);

I expected to see this happen:
Should print

0
0

Instead, this happened:
Stable: 1.43.1

140731856997200
0

Beta channel
Build using the Beta version: 1.44.0-beta.4
(2020-05-23 02c25b3)

1
0

This one is actually correct. Sorry if it has been fixed, I have not found bug report for this, so I've decided to create this one.
Nightly channel
Build using the Nightly version: 1.45.0-nightly
(2020-05-31 5fd2f06)

0
0
@aslpavel aslpavel added the C-bug Category: This is a bug. label Jun 1, 2020
@sfackler
Copy link
Member

sfackler commented Jun 1, 2020

This is #10184

@aslpavel
Copy link
Author

aslpavel commented Jun 1, 2020

I thought #10184 was about NaN, Inf and ect. This one is a normal float as returned by f64::classify(). Plus behavior for f32 is actually correct.

@sfackler
Copy link
Member

sfackler commented Jun 1, 2020

It is about any out of bounds value.

@ehuss
Copy link
Contributor

ehuss commented Jun 2, 2020

As you've noticed, this has changed in 1.45. This is part of #71269, so that both values return 0. The new rules are documented at https://doc.rust-lang.org/nightly/reference/expressions/operator-expr.html#semantics.

@RalfJung
Copy link
Member

RalfJung commented Jun 2, 2020

Plus behavior for f32 is actually correct.

That's just pure chance. Behavior is undefined in both cases (until #71269). When behavior is undefined, looking at what the final executable does is meaningless.

I think this issue should be closed, it is a duplicate of #10184.

@aslpavel
Copy link
Author

aslpavel commented Jun 2, 2020

I see, Thank you!

@aslpavel aslpavel closed this as completed Jun 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

4 participants