0

I found a bug in c++. I tested it with some compilers, and they all gave me the wrong. Basically we have two variables: a and b. a equals to 2.85f and b equals to 0.85f. If we do a - b we'll end up with two. If we do floorf(a), it also equals to two. That means they both equal. But if we compare their equality, somehow that gives us false. If we test it with different numbers (a = 2.84f, b = 0.84f etc.) it gives us the right answer which is true. I tested the code with TDM-GCC 4.9.2, TDM-GCC 5.1.0 and Visual Studio 2017. Here is the code:

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    float a = 2.85f;
    float b = 0.85f;

    cout << "a: " << a << endl
         << "b: " << b << endl
         << "floorf(a): " << floorf(a) << endl
         << "a - b: " << a - b << endl
         << "floorf(a) == (a - b): " << (floorf(a) == (a - b) ? "True" : "False");
    return 0;
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    It's precisely the same reason that in fixed-precision decimal representation, 1/3 + 1/3 + 1/3 will not be equal to 1 but will be equal to 0.9999... – David Schwartz Apr 06 '20 at 22:12
  • 2
    If all the compilers are "wrong", you may want to consider that it isn't actually a bug, and they are doing something for a reason. – ChrisMM Apr 06 '20 at 22:42
  • 2
    Please don't make more work for other people by vandalizing your posts. By posting on the Stack Exchange network, you've granted a non-revocable right, under the [CC BY-SA 4.0 license](//creativecommons.org/licenses/by-sa/4.0/), for Stack Exchange to distribute that content (i.e. regardless of your future choices). By Stack Exchange policy, the non-vandalized version of the post is the one which is distributed. Thus, any vandalism will be reverted. If you want to know more about deleting a post please see: [How does deleting work?](//meta.stackexchange.com/q/5221) – Dharman Jun 15 '20 at 15:14

1 Answers1

6

You have stumbled upon the madness of floating point math. You might want to read up on it at What Every Programmer Should Know About Floating-Point Arithmetic.

Apart from that you might want to read up on the following links to understand why posting "this language is broken!"-statements are often not well received:

Marcus Riemer
  • 7,244
  • 8
  • 51
  • 76
  • 3
    there's no madness. It's all very sensible. It just need a little bit education – Jeffrey Apr 06 '20 at 22:11
  • 1
    I had my share of computational algebra during university and I still can't fathom the practical limits of floating point math for numerical simulations. Maybe I haven't studied it enough, but for practical applications I do like the term "madness" :-) – Marcus Riemer Apr 06 '20 at 22:18
  • It's not madness. It's a mental mismatch: floating-point numbers are not real numbers. If you insist on applying the intuition you've learned from years of dealing with real numbers in situations where they are not applicable, it's you that's mad. – Pete Becker Apr 07 '20 at 13:43