-11

Strange floating point comparision

As you can see in the picture, rotCount is 1.0f. However, when comparing rotCount with 0.0f, the debugger enters the if statement, which is not supposed to happen.

I use the default debugging mode of Visual studio 2010 with optimization off. Here is the dis-assembly of this piece of code. enter image description here

It seems that after fcomp, there is no test and jump assembly codes generated. Is this considered a bug in VS2010? Anyone knows why?

5
  • 10
    Screenshots of programs are not appropriate in questions. They break usability and copy-paste. Oct 25, 2013 at 7:15
  • 11
    One thing I've learned: When you think you have found a compiler bug, then with a probability practically indistinguishable from 1, it's a bug in your own code.
    – molbdnilo
    Oct 25, 2013 at 8:13
  • @molbdnilo Except when talking about MSVC. Oct 25, 2013 at 8:43
  • @molbdnilo: No. Compiler bugs in MSVC indeed are possible :) stackoverflow.com/questions/10539140/…
    – m3tikn0b
    Oct 25, 2013 at 16:10
  • 2
    A funny phenomenon is the geeky urge to reply to "most of the time, X is the case" with a "counterexample" where X is not the case, showing that geeks, too, don't always apply logic correctly.
    – molbdnilo
    Oct 25, 2013 at 18:34

1 Answer 1

31

You have a semicolon at the end of your if statement so the if isn't controlling the block. Remove it

6
  • 3
    I like how the question got a bunch of upvotes, and the second you answered they all came down here :p
    – sandymatt
    Oct 25, 2013 at 7:06
  • @KepaniHaole "a bunch" == 1?
    – Shoe
    Oct 25, 2013 at 7:07
  • 1
    The answer was a good spot: I stared at the disassembler for a minute and couldn't see it. Before that, the question appeared to be interesting.
    – Bathsheba
    Oct 25, 2013 at 7:07
  • 9
    That's why you don't stare ar assembler and instead use a compiler that catches this kind of thing...
    – rubenvb
    Oct 25, 2013 at 7:08
  • 16
    If only there was a warning to tell you when you did this... I'd call it C4390. Oct 25, 2013 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.