Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
Float is 0.33333334326744080000000000000000000000000000000000

Non-Compliant Code Example

Wiki Markup
Additionally, compilers may treat floating point variables differently under different levels of optimization \[[Gough 2005|AA. C References#Gough 2005]\].

#ffcccc
Code Block
bgColor
double a = 3.0;
double b = 7.0;
double c = a / b;

if (c == a / b) {
  printf("Comparison succeeds\n");
} else {
  printf("Unexpected result\n");
}

...

Wiki Markup
The reason for this behavior is that Linux utilizesuses the internal extended precision mode of the x87 FPU on IA-32 machines for increased accuracy during computation.  When the result is stored into memory by the assignment to {{c}}, the FPU automatically rounds the result to fit into a {{double}}.  Later, when the value is read back from memory and compared to the internal representation (which has extended precision), the comparison fails, producing an unexpected result.  Windows does not use the extended precision mode, so all computation is done with double precision and there are no differences in precision between values stored in memory and those internal to the FPU.  On Linux, compiling with the {{-O}} optimization flag eliminates the unnecessary store into memory, so all computation happens within the FPU with extended precision \[[Gough 2005|AA. C References#Gough 2005]\].

...