Two parallel resistors R1
and R2
have net resistance R
given by the expression
1.0
/
((1.0 / R1) + (1.0 / R2))
. The earlier example showed that in the floating-point number system it is possible to model the electrical possibility of zero or infinite resistance. This version raises only those exceptions relevant to the result. Several cases arise:
R
is at worst inexact. A double_t intermediate value defends against overflow or underflow of the intermediate results.
R
is also zero. The divide-by-zero flag raised by the evaluation is undeserved and is cleared.
R
. The divide-by-zero flag that arises from the last quotient is cleared.
R
. If underflow arises on the conversion to float, the flag is raised. Overflow can't arise.
#pragma fenv_access on float ParallelResistance(float R1, float R2) { TFPEnvironmentSaveAndUpdate localEnvironment; double_t R = 1.0 / ((1.0 / (double_t) R1) + (1.0 / (double_t) R2)); localEnvironment.ClearFlags(kFPDivideByZero); // never deserved return R; }
return
statement.
TIP
The inexact flag is usually of interest only locally. In this case, the physical model is inherently imprecise, so it is not worth the code complexity to filter the cases where the inexact flag should not be raised.
[Contents]
[Previous]
[Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.