Fuck you c++...!!!
float a = 1.0/10.0;
float b = a*10.0;
a == b returns false

A beginner of c++ here
Wrote about a 1000 lines code (spread across multiple files, m not dumb)
Passed 90% of cases
Took one and a half days to figure out what's wrong

Turns out c++ doesn't give accurate (as perceived by a human who thinks in decimal) results when comparing equality of 2 floats with ==

Shouldn't that be the first thing to be taught in schools?

  • 2
    @RantSomeWhere You're right, but even if they were the same wouldn't mean they are equal.

    Floating point arithmetics are special.
  • 14
    First of all
    "float b = a*10.0
    a == b returns false"
    Well duh, b is 10a, the only way it'd return true was if a was 0.

    secondly, the precision stuff has nothing to do with C++, it's with how floating point numbers are represented. Look up the IEEE754 standard. TLDR: it's not so simple and you shouldn't rely on them behaving like integers or something (or what you call "decimal"s)

    Also, technically you're forcing narrowing conversions from double to float, should write float constants with a "f" suffix eg. "10.0f"

    If you want precise results use a rational number representation or fixed point arithmetic or arbitrary precision arithmetic (with enough precision that your application doesn't care anymore).

    Comparing floating point numbers could be done using a range instead (eg. difference of a and b is less than some constant, which you define according to your application).
  • 6
    @rutvora What did they teach you about floating point arithmetics at school?
  • 7
    That's pretty much the first thing that is taught on beginner programming courses and it has little to do with C++
  • 1
    Correction: b == 1.0 returns false
  • 0

    This wasn't the example
    I had a few multiplications and divisions of floating points on each variable
    And then compare them

    I already am aware of IEEE754
    But I am ranting as the solution that you suggested (i.e. if diff is less than €, return equal) should be implemented by default (as most beginners would else be confused because of unawareness of IEEE754 or the fact that c++ doesn't do this, while other languages do something.... Although I don't know what)

    Advanced users can (as they are advanced) make their own function to compare bit by bit or change the error range if they ever need it...!!!
  • 0
    @ddephor not the point, check my reply above
  • 5
    @rutvora "beginner-friendly" isn't really a feature of C/C++ :p

    And in any case, in general the language doesn't like to do clever stuff by default, it tries to be as barebones as possible to give you maximum control.

    Floating point numbers trip people up in other languages too, eg. in JS (stick around on dR for a while and you'll see a few "JavaScript sucks because I don't understand floating point numbers!" posts).
  • 3
    @rutvora I don't think I know a language that compares floats using a constant distance threshold by default.
    C++ isn't doing something special here that no other language does. It's quite the opposite. Every language I know that uses floating point numbers behaves like C++ in this regard.
  • 0

    Maybe true
    But the fact remains that it's a thing that pisses people off

    Specially when that is the issue in a 1000 line code.... You never think of it...!!
  • 4
    You have the exact same behavior in C# and its the correct behavior according to how float and double is supposed to work.

    Not a c/c++ problem.

    In C# we always use the decimal type.

    Float and double sacrifice precision for performance while decimal guaranties precision but is slower.
  • 4
    @rutvora well now you know to never assume floating point computations work the way you think they do so you'll instantly mistrust any shady numerical code involving floats. Part of the learning process. The system is designed that way for a reason (as @Voxera mentioned, performance vs precision tradeoffs).

    This isn't even close to the most annoying stuff programming can throw at you, keep at it.
  • 3
    @rutvora You have to know your tools. Software development is not the "Learn ... in 10 days" thingy.

    There are many things that piss people off, but that doesn't mean it should be changed. Logic and arithmetic operations are not defined by democratic processes (and that's just fine)

    And if you're using floating point arithmetics, you have to know what you do.

    First rule of thumb: Never use floating point unless you really need it.

    I'm in business for more than ten years, being it a hobby ten years before I got professional and there where only a few occasions where integers weren't enough.
  • 0

    I actually never really needed floating point arithmetic before this

    M working with graphics right now, so need floats for storing slopes of lines :(
  • 0

    Quite agree there
    I am new to c++, not to coding
    I do most of my codes in Java and a few in python

    Professor asked this assignment to be done in c++ 😪

    And as mentioned in previous comment, the first time I encountered floating point arithmetic....
    Will make extra sure I need it from now on 😂
  • 0
    Also, float and double supports infinity , -infinity and NaN as values and should not die on divide by 0.
  • 1
  • 1
    I get your pain man. But honestly, it's quite common to >= instead of == with floats just because of that shit. You'll end up with a lot of != if you look too close into floats, just like quantum stuff... (not really but bah, fuck it it's Friday). :P
  • 2
    You can't compare floating point numbers like that. You must compare absolute value of the difference smaller than a delta. And that delta should be scaled to the biggest of the two numbers you are comparing. And even that does not always work, floating point can hurt you in so many ways ...
  • 1
    @Voxera Float and Double sacrifice precision for range, not performance. You only gain performance if you have hardware support (which might be common on x86, but not on all platforms or ARM). If you don't have a dedicated FP unit your compiler will insert code that does FP arithmetic in software which will kill your performance.
Add Comment