4 ms·
> No, they are folding both constant expressions exactly and actual floating point operations, going by assembly output. Yes. The behaviour is quite odd and un
by tomtomtom777 7y ago
> No, they are folding both constant expressions exactly and actual floating point operations, going by assembly output.
Yes. The behaviour is quite odd and unexpected. It seems they have different rules for when to apply the constant version of floating math than what constitutes actual compile time evaluation.
const a float64 = 0.1;
const b float64 = 0.2;
const x float64 = 0.1 + 0.2; // Const evaluation, "rational" math
const y float64 = a + b; // Const evaluation, IEEE-754 math
fmt.Println(x == y) // false ??!?
This seems quite a horrible approach.
EDIT: Or how about this one:
const x float64 = 0;
fmt.Println(x + 0.1 + 0.2 == 0.1 + 0.2 + x ); // false!
fmt.Println(x + 0.1 + 0.2 == x + (0.1 + 0.2) ); // false!
- SamReidHughes 7y agoThe literal folding is specified language behavior, the other is just an optimization.