Is it possible to use a if statement inside #define?

前端 未结 8 1721
名媛妹妹
名媛妹妹 2021-02-02 13:34

I\'m trying to make a macro with the following formula: (a^2/(a+b))*b, and I want to make sure that the there will be no dividing by zero.

#define          


        
8条回答
  •  面向向阳花
    2021-02-02 13:56

    You can convert the conditional statement into a simple expression. Conditions evaluate to 0 or 1

    // pseudo-code
    // if () { 0; } else { 42; }
    // if (!) { 42; } else { 0; }
    // ! * 42;
    

    In your specific case

    // if ((x == 0) || (y == 0)) { 0; } else { (x)(y)expression; }
    // if ((x != 0) && (y != 0)) { (x)(y)expression; }
    // ((x != 0) && (y != 0)) * ( (x)(y)expression );
    

提交回复
热议问题