3 ms·
Interesting. I thought Go used standard IEEE-754 floats. Why does it have this result?
by tomtomtom777 7y ago
Interesting. I thought Go used standard IEEE-754 floats.
Why does it have this result?
- SamReidHughes 7y agoThey evaluate constant expressions exactly at compile time, then convert to float.
- WalterBright 7y agoThat works great until people complain that it works differently at compile time, and they have a point.
- SamReidHughes 7y agoYeah. On the other hand, they won’t get any surprises when cross-compiling. Edit: Okay, maybe Go devs saved themselves the work of having floating point emulation. Edit: No, they are folding both constant expressions exactly and actual floating point operations, going by assembly output.
- gpderetta 7y agoGCC at least evaluates constant FP expressions using a model of the target machine FP unit, so that compile time and runtime expressions behave identically.
- tomtomtom777 7y agoGo uses IEEE-754. The behaviour of these may be odd for those new to the quirks of the float, but I thought the behaviour is nowadays standardized and reproducable across platforms.
- 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.