5

In C++, I want to test values passed to a function for being non-zero, and base some behavior on that condition.

e.g.:

void do_something(float x){
    if(x)          //        <-- prefer this format?
        do_a();
    else
        do_b();
}

vs:

void do_something(float x){
    if(x != 0)        //     <-- or this format?
        do_a();
    else
        do_b();
}

other forms:

void do_something(int x){
    x? do_a() : do_b();      //  <-- prefer this?
    x!=0? do_a() : do_b();   //  <-- or this?
}

Are both of these 'well formed', or is there a reason that either would be undefined behavior in some case?

I tested on godbolt.org, and both forms produce the exact same assembly code. I tested with int, float, ternary operator and if(), and in all cases the code looked the same for the two forms.

I currently tend to use the if(x != 0) for float/double, and if(x) for int, somewhat due to the complexities of NaN, and other special values in the floating point value.

ttemple
  • 852
  • 5
  • 20
  • 3
    Possibility opinion-based except that I'm right in saying that `if(x)` is always the correct way. – Bathsheba Oct 30 '18 at 15:39
  • 2
    [Be wary of floating point comparisons](https://stackoverflow.com/questions/588004/is-floating-point-math-broken). That said it is a matter of personal preference or a style guide. – NathanOliver Oct 30 '18 at 15:39
  • Possible duplicate of [What is the most effective way for float and double comparison?](https://stackoverflow.com/questions/17333/what-is-the-most-effective-way-for-float-and-double-comparison) – Matthieu Brucher Oct 30 '18 at 15:40
  • 5
    @MatthieuBrucher You really shouldn't vote to close and answer. You're sending mixed signals. – NathanOliver Oct 30 '18 at 15:40
  • @NathanOliver - per my comment on the answer below, this is a test for int 0, or float 0.0, (as a magic number), not an epsilon test for a small value. – ttemple Oct 30 '18 at 15:46
  • @Bathsheba Then you probably know how to distinguish between "`if(x)` is supposed to be `if(x != 0)`" and "`if(x)` is written this way because programmer forgot to type condition" situations? – user7860670 Oct 30 '18 at 15:51
  • I'd have to agree with this being opinion based to some degree, however, this could even be independent between machines as in the actual hardware, the operating system, the developing environment (compilers) and the specific data types that are being used and the conventions of that data type. Strictly speaking, in many cases there is more than one way to do the same task within a programming language that are both correct and valid each having their own pros and cons. Besides that it is a matter of the coding convention and style that one may prefer to use. – Francis Cugler Oct 30 '18 at 15:54
  • ", is there any reason to prefer one over the other?" is not really opinion based. The answer is a clear "No". For floating point numbers `if (x)` as well as `if (x != 0)` is almsot always wrong, which is also not a matter of opinions – 463035818_is_not_an_ai Oct 30 '18 at 15:57
  • As long as 0.0 is represented as all bits off, I don't see why detecting the value of 0.0 being passed to a function is 'wrong'. I understand why epsilon values exist, and in this case I don't want an epsilon comparison. I want to know that the caller called the function with the value 0.0 (all bits off). – ttemple Oct 30 '18 at 16:03
  • 2
    @ttemple: because of the floating point signed zero, it's not as simple as "all bits are off" for floating point. – Bathsheba Oct 30 '18 at 16:32
  • For the IEEE-754 representation, which is almost exclusively used, even in math co-processors, zero is encoded as all bits off. The question is basically, according the the C++ standard, will if(x) always give the same result as if(x!=0). I'm not asking whether it is a good idea, or whether you like it, but is it always guaranteed to be the same as defined by standard C++. – ttemple Oct 30 '18 at 17:28
  • For instance, `if (x % 2) ...` is super confusing. You would expect this to be asking 'is x divisible by 2?'. But instead, if that's the case, the expression evaluates to 0/false. For this reason alone, I always use (!= 0) when appropriate. – Zonico Apr 06 '21 at 09:38

0 Answers0