BitGen01100000
BitGen01100000

Reputation: 57

Should you use '>=' instead of '==' to be extra safe when coding with other types than float?

When working with floating-points one should use

a <= 0.0

instead of

a == 0.0

to make sure that you get the desired behaviour, as there is the problem with round-off errors when using floating-point variables.


But when using other variable types like int could it be useful to do the same? Like you have a for loop iterating over an int variable (like an index) and when it gets to a number it should do something. Should you then set the comparison to be >= instead of == when it should result in the same output? Like could there ever be a case where the == is not evaluated in the following case:

for (int i = 0; i < 10; i++)
{
    if (i == 5)
    {
        break;
    }
}

And that it would be "safer" to do the following instead:

for (int i = 0; i < 10; i++)
{
    if (i >= 5)
    {
        break;
    }
}

If there is no difference between the two when it comes to coding "safe" is there any performance or readability difference or other thing that can make one choose between the ways to code?

Tried to google this but couldn't find anything stating either way. But that might have to do with the problem with searching for operators.

Am I too paranoid for asking this?

Upvotes: 3

Views: 169

Answers (3)

4386427
4386427

Reputation: 44339

Like could there ever be a case where the == is not evaluated

No, using == is safe. Integers represent all values within the range of the integer type used accurately. There are no surprises.

Should you then set the comparison to be >= instead of ==

As the result is the same, you can do both but I would find >= confusing. So for readability I prefer ==

is there any performance . . . that can make one choose between the ways to code

You can't tell by looking at the C code. It depends on the system (CPU) used. In general I would however doubt that you would experience any major difference.

Upvotes: 1

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215507

The premise of the question is wrong; blindly using a <= 0.0 instead of a == 0.0 is not a valid practice with floating point. See How dangerous is it to compare floating point values? for a treatment of the topic.

With that said, there are some cases where use of inequality relational operators are "more hardened" than use of the equality operator, and vice versa. In general, I would recommend against it since it's likely to give you a false sense of safety; for example, in the example in your question with i==5 vs i>=5, the compiler is likely to be able to prove they're the same, and optimize either to the other if needed. This means it will not necessarily do anything to protect you against stack overflows or neutrinos or any other cause by which the value of i might become something other than 0..5 outside of the defined semantics of the language.

There are also some cases where use of equality is defined by inequality is not, particularly involving pointers. If pos and end are both pointers, pos==end is well-defined as long as they are both valid. But if they are both null, while pos==end is well-defined and true, pos>=end is undefined behavior. Likewise if they both point into different arrays (in particular if end points to a sentinel not part of your array), pos==end will always be false, but pos>=end is undefined behavior.

I also find it misleading to write if (i>=5) when you know i>5 is logically impossible - it makes the reader stop to think about whether it's possible, and if not, why you wrote it that way instead of if (i==5).

Upvotes: 3

Fiddling Bits
Fiddling Bits

Reputation: 8861

The answer is, it depends. If you have a loop like this:

for (int i = 0; i < 10; i++)
{
    if (i == 5)
    {
        break;
    }
}

Then there is no danger in i skipping 5.

On the other hand, if you have something like this:

volatile int i;

void interrupt(void)
{
    i++;
}

void foo(void)
{
    for (i = 0; i < 10; i++)
    {
        if (i == 5)
        {
            break;
        }
    }
}

Then i may change outside the normal flow of the program (e.g. because of an interrupt). In this case >= would be prudent.

Upvotes: 1

Related Questions