1

I'm getting an unexpected behaviour when comparing a float value, I understand that floats could have rounding precission issues but here numbers are quite specific to present those issues.

#include <stdio.h>
int main()
{
    float alpha = 0.0f;
    int finish = 0;

    while (finish == 0)
    {
        alpha += 0.05f;

        if (alpha > 1.0f)
        {
            printf("%f", alpha);    // Expected result: 1.05f, actual result: 1.0f
            finish = 1;
        }
    }

    return 0;
}

Actually, condition enters when alpha = 1.0f. Can't understand that behaviour...

I'm compiling with MinGW (GCC 5.3.0) on Windows 10 (tested on 32bit and 64bit), Intel i5 processors.

Ray
  • 123
  • 2
  • 9
  • 3
    If you were to print `alpha` with more significant digits (e.g. `printf("%.7f\n", alpha);`) then you would immediately see the problem: http://ideone.com/zwQW7e. – Paul R Jan 24 '17 at 13:28
  • 1
    You have to know whether the constant for 0.05f is slightly smaller or slightly larger than the infinite bit stream that would represent 0.05 exactly. Presumably, it is slightly larger. – Jonathan Leffler Jan 24 '17 at 13:30
  • Check this out (it's what I used to build my answer):http://www.exploringbinary.com/floating-point-converter/ – Bathsheba Jan 24 '17 at 13:30
  • @Olaf, good duplicate but sometimes it's nice to explain the result in a particular case (whilst compiling some code). – Bathsheba Jan 24 '17 at 13:30
  • @Bathsheba: The explanation is in the dup. Askers are expected to be able to understand such basics and apply them to their situation if they are that obvious as here. – too honest for this site Jan 24 '17 at 13:32
  • @Olaf: I think I need to flatter your intelligence here. Indeed to you it is obvious. I tend to answer the well-written floating point questions. – Bathsheba Jan 24 '17 at 13:33
  • 1
    @Ray: Please study http://stackoverflow.com/questions/588004/is-floating-point-math-broken – Bathsheba Jan 24 '17 at 13:34
  • 1
    @Bathsheba: Considering the amount of "do my homework" and "what does operator/statement xyx mean" questions, you are right. There are indeed only few C questions which are no dups. Problem is more to find the dups, as noobs are very creative using wrong/missleading phrases. Oh, and we should expect some reason from posters, too. Without that they won't get anywhere in programming. – too honest for this site Jan 24 '17 at 13:36

1 Answers1

3

(Restricting the answer if I may to IEEE754 floating point).

There is no such float as 0.05. The nearest number representable to that is

0.0500000007450580596923828125

So what is happening, is that slightly larger values than what you think are added to alpha, which is enough to just push it over the 1.0f mark (which, out of interest, can be represented exactly.)

The default formatting in print is rounding that slightly greater than 1.0f number back to 1.0f.

In summary, all this is due to binary floating point not having, in general, an exact decimal representation.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • I disagree, although the OP would be advised to study the duplicate. Even armed with the floating point knowledge, it's nice to have an explanation for this particular case. (For what it's worth I probably close 2 or 3 floating point questions as duplicated to the one you've identified per week.) – Bathsheba Jan 24 '17 at 13:32