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
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 );