Guardian Programmer's Guide

Table Of Contents
Using Floating-Point Formats
Guardian Programmer’s Guide 421922-014
28 - 7
Floating-Point Operating Mode Routines
rounding mode settings within a procedure might be affected by instruction reordering
within a procedure. When evaluating IEEE floating-point expressions, the compiler
uses the default floating-point modes identified above. The expressions evaluated at
compile time do not affect the exception flags.
See the Guardian Procedure Calls Reference Manual for details about these operating
mode routines.
Operating Modes Recommendations
Encoding rounding modes, exception flags, and other details could vary with future
CPU families. Use the kfpieee.h header file for the encodings.
Do not enable IEEE floating-point traps. To check for possible problems, clear the
exception flags before a computation. Also, check the exception flags after
computation. The following example illustrates the use of
FP_IEEE_EXCEPTIONS_SET_ and FP_IEEE_EXCEPTIONS_GET_.
#include <math.h> nolist
#include <stream.h> nolist
#include <kfpieee.h> nolist
double triangleArea /* Area of a triangle, as per Kahan */
( double a, double b, double c /* lengths of the sides */ )
{
double t;
/* sort sides so a >= b >= c */
#define EXCHANGE(x,y) { t=x; x=y; y=t; }
if(a<b) EXCHANGE(a,b);
if(b<c) {
EXCHANGE(b,c);
if(a<b) EXCHANGE(a,b);
}
return( sqrt( (a+(b+c))*(c-(a-b))*(c+(a-b))*(a+(b-c)) )/4 );
} /* triangleArea */
int main (void) {
double area;
FP_IEEE_EXCEPTIONS_SET_( 0 ); // clear exception flags
area = triangleArea( 1.0001, 1.0002, 2.0 );
// test for interesting exceptions:
if( FP_IEEE_EXCEPTIONS_GET_() &
(FP_IEEE_INVALID|FP_IEEE_OVERFLOW|FP_IEEE_DIVBYZERO))
{
cout << "Trouble in computation! \n";
return(1);
}
cout << "Area of the thin triangle is " << area << "\n" ;
return(0);
}