top | item 43444898

(no title)

tyilo | 11 months ago

Floats are not guaranteed to be bit-identical at compile time and run time in Rust.

discuss

order

kibwen|11 months ago

It's not quite as bad as it sounds, because the only difference is that the representation of NaN (the sign and the payload bits) isn't guaranteed to be stable. If you're not relying on any specific representation of NaN, then floating-point math in const fn is identical, and observed differences would be considered a soundness bug in the Rust compiler.

weinzierl|11 months ago

I had to look this up because it is a while that I tried to use floating point math in a const fn and it seems that the differences you described have been decided to be acceptable.

Looks like floating point math in const fn is coming. Here is the respective tracking issue: https://github.com/rust-lang/rust/issues/128288

throwawaymaths|11 months ago

what happens if you compile on a system that has a different precision than the system you run on? like suppose you compile on a 64 bit system targetting 32 bit embedded with an fp accelerator or a 16 bit system with softfloat?

weinzierl|11 months ago

Last time I checked the float functions that have no bit-identical results (mostly transcendental functions) were missing from Rust's const fn for exactly that reason.

codedokode|11 months ago

They are not guaranteed to be precise anyway.