Suppressing spurious signals

The example "Arithmetic with infinities" on page 195 computes the net resistance of two parallel resistors. Here is a more robust version, which raises flags (accessible to the caller) for just those exceptions that pertain to the result. It uses TFPEnvironmentSaveAndUpdate.

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:

      #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;
      
    }
This code assumes that its resistances (even zero) have positive sign, as with elementary circuits. The code also allows inexact to be raised in circumstances when just one argument is zero or a NaN and the flag is undeserved. Underflow can arise in the implicit conversion to float in the 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.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker