Hacker News new | past | comments | ask | show | jobs | submit login

They evaluate constant expressions exactly at compile time, then convert to float.



That works great until people complain that it works differently at compile time, and they have a point.


Yeah. 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.


> 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!


The literal folding is specified language behavior, the other is just an optimization.


GCC at least evaluates constant FP expressions using a model of the target machine FP unit, so that compile time and runtime expressions behave identically.


Go 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.




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: