Description
TLDR: should we allow floating point types in const fn?
Basically the question is whether the following const fn
const fn foo(a: f32, b: f32) -> f32 {
a / b
}
must yield the same results for the same arguments if it is invoked at runtime or compile-time:
const RES1: f32 = foo(1.0, 0.0);
fn main() {
let res2: f32 = foo(1.0, 0.0);
assert_eq!(RES1.to_bits(), res2.to_bits());
}
Depending on the platform's NaN behavior, the result will differ between runtime and compile-time execution of foo(1.0, 0.0)
. Compile-time execution is determined by the Rust port of apfloat (a soft-float implementation); runtime behavior depends on the actual NaN patterns used by the hardware which are not always fully determined by the IEEE specification.
Note that this is entirely independent of any optimizations; we are discussing here the relationship between code that the user explicitly requests to be executed at compile-time, and regular run-time code. Optimizations apply to all code equally and they treat fn
and const fn
the same, so the the questions of how floating-point operations can be optimized is an entirely separate from and off-topic for this issue.
cc @rust-lang/wg-const-eval