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

Package:
gcc
Source:
gcc-defaults
Description:
GNU C compiler
Submitter:
Martin Brain
Date:
2013-08-09 15:48:13 UTC
Severity:
normal
#719015#5
Date:
2013-08-07 18:30:21 UTC
From:
To:
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

#719015#10
Date:
2013-08-09 15:46:18 UTC
From:
To:
Sorry.  The previous examples hit a discrepancy between strtof and
strtod.  The nearest float to 1.999999940395356f should be 0x1.0p+1f,
rather than 0x1.fffffep+0f.  If instead the number 1.9999999403953553f
is used (see updated test case), then it correctly exposes the double
rounding bug.

1.9999999403953553 is almost exactly between (float):

0x1.fffffep+0f
0x1p+1f

but a little closer to the lower bound and thus rounds down when parsed
as a float.  It is between (double):

0x1.fffffefffffffp+0
0x1.ffffffp+0

but a little closer to the upper bound and thus rounds up when parsed as
a double.  Subsequent rounding to float then rounds up giving 0x1p+1f.

Cheers,
 - Martin