6

I was referring to the question ""IF" argument evaluation order?" for understanding the evaluation order for "if" statement in c++.

Here is the code where the conditions in if statements are evaluated in a wrong order.

#include <iostream>

using namespace std;

int main()
{
    int t = 0;
    if((1 / t) == 1 && t != 0)
    {
        cout << "0" << endl;
    }

    cout << "1" << endl;

    return 0;
}

The result is 1 instead of floating point exception.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Ayush Pandey
  • 565
  • 6
  • 19
  • So do you get a FP exception if you run `1/t` outside of an if? – juanchopanza Oct 03 '18 at 06:36
  • yes, but not in the if condition – Ayush Pandey Oct 03 '18 at 06:36
  • 5
    Have you checked generated assembly for that code? I'd guess compiler noticed that `t` is equal to `0` and skipped the condition altogether. Check again with `t` read from cin. – Yksisarvinen Oct 03 '18 at 06:36
  • I suggest you use a different means to check the order of evaluation. – juanchopanza Oct 03 '18 at 06:38
  • What compiler are you using? It is giving FP on GCC. – haccks Oct 03 '18 at 06:41
  • 1
    `if` doesn't have to evaluate its conditions (plural); it has **exactly one** condition. Now this expression may have sub-expressions, but that's not the concern of `if`. `if` only evaluates the outermost value, possibly converting it to `bool`. – MSalters Oct 03 '18 at 07:49
  • I got FP exception on GCC without optimization. I got `1` with optimization. I think it's quite reasonable. You have no guarantee for the side-effect of devision. If the complier evaluates condition you get left-right evaluation and may get FP exception. If the complier can remove the code you cannot get such side-effects like FP exception. – Askold Ilvento Oct 03 '18 at 09:43

2 Answers2

19

Division-by-zero is undefined behavior. Anything can happen.

[expr.mul]/4

If the second operand of / or % is zero the behavior is undefined.

llllllllll
  • 16,169
  • 4
  • 31
  • 54
  • Indeed. In this godbolt example we can see that the entire `if` statement has been elided by the compiler: https://godbolt.org/z/YzZ-q8 – Richard Hodges Oct 03 '18 at 09:27
  • @RichardHodges Not only that. If you replace `t != 0` in the `if` condition by `t == 0`, [the whole `main` function body disappears](https://godbolt.org/z/3bEx1f). We really can't make any assumption on UB. – llllllllll Oct 03 '18 at 09:37
6

divide by zero doesn't guarantee that every time program will throw you a runtime error. this is why dividing by zero is an undefined behaviour.

as C standard states;

The result of the / operator is the quotient from the division of the first operand by the second; the result of the % operator is the remainder. In both operations, if the value of the second operand is zero, the behaviour is undefined.

as for C++ (03-5.6.4);

The binary / operator yields the quotient, and the binary % operator yields the remainder from the division of the first expression by the second. If the second operand of / or % is zero the behavior is undefined; otherwise (a/b)*b + a%b is equal to a. If both operands are nonnegative then the remainder is nonnegative; if not, the sign of the remainder is implementation-defined

Yucel_K
  • 688
  • 9
  • 17