Is there a way to simplify fractions prior to evaluation in order to avoid division by zero?
Example:
double x1 = 2;
double x2 = 2;
double x3 = 2;
double r1 = (x1-x2)/(x1-x2-x3);
cout << r1 << endl;
x3 = 0;
r1 = (x1-x2)/(x1-x2-x3);
cout << r1 << endl;
will return:
-0
-nan
Is it possible to make the compiler simplify the second expression at compile time to avoid the division by zero? In particular, if x3
equals 0, I expect (x1-x2)/(x1-x2-x3)
to be simplified to 1.
In my code the factors x1
, x2
and x3
are replaced by functions.