[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#719015: Double rounding bug in the conversion from decimal string to single precision floating point literals.



Package: gcc
Version: 4:4.7.2-1

I'm working on a stock Debian Wheezy system on a 32 bit x86 system:

$ uname -a
Linux twinkle 3.2.0-4-686-pae #1 SMP Debian 3.2.46-1 i686 GNU/Linux

In compiling code, probably in parsing, GCC appears to convert a single
precision literal, 1.999999940395356f to a double precision float and
then round it back to a single precision float, giving a double rounding
bug.  The attached code demonstrates the discrepancy with strtof.  The
fix will likely be making sure that float literals are parsed as floats
not doubles.

Cheers,
 - Martin

/*
** gcc does not seem to handle (single precision) floating point constants correctly.
** I believe the closest single precision floating point number to:
**   1.999999940395356f
** is
**   0x1.fffffep+0
** which strtof correctly identifies, while gcc gives:
**   0x1p+1
** which is out by 1 ulp.  The likely cause for this is that the number is first parsed
** as a double and then rounded to a single precision number, giving a double rounding bug.
*/

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char **argv) {
  float parsedAtCompileTime = 1.999999940395356f;
  float parsedAtRunTime = strtof("1.999999940395356f",NULL);

  printf("parsedAtCompileTime = %a\n",parsedAtCompileTime);
  printf("parsedAtRunTime = %a\n",parsedAtRunTime);
  printf("parsedAtCompileTime == parsedAtRunTime\t%d\n",(parsedAtCompileTime == parsedAtRunTime));

  return 1;
}

Reply to: