You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: text/0000-float-semantics.md
+16-1Lines changed: 16 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -97,7 +97,6 @@ However, the exact function is not specified, and it is allowed to change across
97
97
In particular, there is no guarantee that the choice made in const evaluation is consistent with the choice made at runtime.
98
98
That is, the following assertion is allowed to fail (and in fact, it [fails on current versions of Rust](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=a594d2975c29b1c7fa457a4ec4ae4b87)):
99
99
```rust
100
-
#![allow(non_snake_case)]
101
100
usestd::hint::black_box;
102
101
103
102
constC:f32=0.0/0.0;
@@ -111,6 +110,22 @@ fn main() {
111
110
This means that evaluating the same `const fn` on the same arguments can produce different results at compile-time and run-time.
112
111
However, note that these functions are already non-deterministic: even evaluating the same function with the same arguments twice at runtime can [and does](https://play.rust-lang.org/?version=stable&mode=release&edition=2021&gist=50b5a549fa1fe259cea5ad138066ccf0) produce different results!
113
112
113
+
In other words, consider this code:
114
+
```rust
115
+
constfndiv(x:f32) ->i32 {
116
+
unsafe { std::mem::transmute(x/x) }
117
+
}
118
+
119
+
// This is not guaranteed to always succeed. That's new, currently
120
+
// all `const fn` you can write guarantee this.
121
+
assert_eq!(div(0.0), div(0.0));
122
+
// Consequently, different results can be observed at compile-time and at run-time.
123
+
constC:i32=div(0.0);
124
+
assert_eq!(C, div(0.0));
125
+
```
126
+
The first assertion is very unlikely to fail in practice (it would require the two invocations of `div` to be optimized differently).
127
+
The second however [actually fails](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=0b4b952929c9ebcd2bd50aee54e6cdf4) on current nightlies in debug mode.
128
+
114
129
This resolves the last open question blocking floating-point operations in `const fn`.
115
130
When the RFC is accepted, the `const_fn_floating_point_arithmetic` feature gate and the `const fn to_bits` methods can be stabilized.
0 commit comments