Bug 24479 - GCC donot promote single precision correctly with optimization flag on
Summary: GCC donot promote single precision correctly with optimization flag on
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 3.4.4
: P2 normal
Target Milestone: 4.1.0
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2005-10-21 20:21 UTC by Dongzhe Yang
Modified: 2005-10-21 21:53 UTC (History)
37 users (show)

See Also:
Known to work:
Known to fail:
Last reconfirmed:


Note You need to log in before you can comment on or make changes to this bug.
Description Dongzhe Yang 2005-10-21 20:21:57 UTC
Please run the following code through gcc with -O3 flag, then run the executable. The expected result is -13275031.0, but the result from the code is -13275030.0. That is because the result from the addition (a+0.5) was stored in a signle precision, not double precision as ISO C++ standard mandates. This is causing numerical problems with our product. This bug might be associated with other math built-in functions.

#include <math.h>
#include <stdio.h>

inline float foof(float a) {
    float b = floor(a + 0.5);
    return b;

int main(void) {
    float a = -13275031.0f;
    a = foof(a);
    printf("%f\n", a);
    return 0;
Comment 1 Andrew Pinski 2005-10-21 20:24:31 UTC

*** This bug has been marked as a duplicate of 323 ***
Comment 2 Dongzhe Yang 2005-10-21 21:51:54 UTC
I am quite surprised when i saw this bug report was marked as duplicate with #323. I have to reopen the bug and here is the reason.

With bug 323, it is caused by hardware using excessive precision so that it leads to different answers depending on if the excessive precision value gets to be stored in a 64bit memory or not. 

The bug I reported is that the double value out of a double precision addition (a + 0.5) is stored into a 64 bits memory (as double value), but when optimization (-O3) is used, the double value is actually stored in a 32 bits memory (a single precision value). This is clearly a violation of ISO C++ standard. It has nothing to do with bug 323. Plesae reconsider your decision on this one.

Comment 3 Andrew Pinski 2005-10-21 21:53:56 UTC
Fixed in 4.1.0 by:
2005-10-05  Dale Johannesen  <dalej@apple.com>

        * convert.c (convert_to_real):  Don't convert
        (float)floor(double d) to floorf((float)d).