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

integer to floating point casts can cause undefined behaviour #10185

Closed
thestinger opened this issue Oct 31, 2013 · 10 comments
Closed

integer to floating point casts can cause undefined behaviour #10185

thestinger opened this issue Oct 31, 2013 · 10 comments
Labels
P-medium Medium priority

Comments

@thestinger
Copy link
Contributor

If the value cannot fit in the floating point value, the results are undefined.

-1u64 as f32
@brson
Copy link
Contributor

brson commented Oct 31, 2013

Nominating.

@pnkfelix
Copy link
Member

pnkfelix commented Nov 7, 2013

accepting for P-backcompat-lang, same reasoning as #10184

@pcwalton
Copy link
Contributor

I don't think this is backwards incompatible at a language level. It will not cause code that was working OK to stop working. Nominating.

@thestinger
Copy link
Contributor Author

@pcwalton: It could cause code to stop working, because the results will change from arbitrary (but perhaps what the program wanted) values to whatever we decide to output when we branch on overflow (INFINITY?). Luckily, most floating point values are f64 so we only need checks on those for casts from 64-bit integers.

@pnkfelix
Copy link
Member

changing to P-high, same reasoning as #10183.

But we need to look into this more carefully, I do not yet know what the most recent IEEE 754 says about this case (nor do I know whether LLVM conforms to IEEE 754 2008 in this case ... ) I'd like to investigate that before really claiming to understand this.

@huonw
Copy link
Member

huonw commented Jul 8, 2014

Is the example given in the issue actually undefined behaviour? 264 - 1 is in-range for f32 (since the maximum value is ~2128), it's just not precisely representable.

Something that would be undefined would be 100000 as f16, since 216 = 65536 is the largest value for the half-precision type f16 (that Rust does not yet have).

@Gankra
Copy link
Contributor

Gankra commented Jun 11, 2015

I have confirmed on the llvm IRC channel that the intent is that it's UB only if the value is bigger than the biggest finite value. So this is actually not a problem in Rust unless we introduce u128 (which can be bigger than f32's biggest finite) or f16. (i128 is still too small)

@jonas-schievink
Copy link
Contributor

unless we introduce u128

This has now happened

@Gankra
Copy link
Contributor

Gankra commented May 7, 2017

Sadness

@arielb1 arielb1 reopened this May 8, 2017
@nagisa
Copy link
Member

nagisa commented May 8, 2017

We already have an issue for this (#41799) which also has some code to reproduce the issue. Re-closing this as duplicate.

@nagisa nagisa closed this as completed May 8, 2017
flip1995 pushed a commit to flip1995/rust that referenced this issue Apr 23, 2023
Suppress the triggering of some lints in derived structures

Fixes rust-lang#10185
Fixes rust-lang#10417

For `integer_arithmetic`, `arithmetic_side_effects` and `shadow_reuse`.

* ~~Not sure how to test these use-cases so feel free to point any method or any related PR.~~

---

changelog: FP: [`integer_arithmetic`], [`arithmetic_side_effects`]: No longer lint inside proc macros
[rust-lang#10203](rust-lang/rust-clippy#10203)
<!-- changelog_checked -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P-medium Medium priority
Projects
None yet
Development

No branches or pull requests

9 participants