But what about the case where b=+0 and c=-0 ?
Convert -0.0 to +0.0 without branching by adding 0.0 .
int main(void) { double b = +0.0; double c = -0.0; printf("%e\n", b); printf("%e\n", c); printf("%e\n", 1.0/(1.0/b + 1.0/c)); b += 0.0; c += 0.0; printf("%e\n", 1.0/(1.0/b + 1.0/c)); return 0; }
Output
0.000000e+00 -0.000000e+00 nan 0.000000e+00
[Edit] On the other hand, any values around 0.0 , but not 0.0 , are most likely numerical artifacts, not exact resistance values. The above still has problems with tiny value pairs, such as b = DBL_TRUE_MIN; c = -b; b = DBL_TRUE_MIN; c = -b; Problem: 1.0/tiny → Infinity and +Infinity + -Infinity → NAN . You can use either a wider floating point to calculate the quotient, or narrow operands.
b += 0.0; c += 0.0; printf("%Le\n", 1.0/(1.0L/b + 1.0L/c)); // Lose some precision, but we are talking about real resistors/lenses here. b = (float) b + 0.0f; c = (float) c + 0.0f; printf("%e\n", 1.0/(1.0/b + 1.0/c));
source share