Legacy Documentclose button

Important: The information in this document is obsolete and should not be used for new development.

Previous Book Contents Book Index Next

Inside Macintosh: PowerPC Numerics / Part 2 - The PowerPC Numerics C Implementation
Chapter 8 - Environmental Control Functions / Controlling the Rounding Direction


fesetround

You can use the fesetround function to change the rounding direction.

int fesetround (int round);
round
One of the four rounding direction constants (see Table 8-1).
DESCRIPTION
The fesetround function sets the rounding direction to the mode specified by its argument. If the value of round does not match any of the rounding direction constants, the function returns 0 and does not change the rounding direction.

By convention, if you change the rounding direction inside a function, first save the rounding direction of the calling function using fegetround and restore the saved direction at the end of the function. This way, the function does not affect the rounding direction of its caller. If the function is to be reentrant, then storage for the caller's rounding direction must be local.

One reason to change the rounding direction would be to put bounds on errors (at least for the basic arithmetic operations and square root). Suppose you want to evaluate an expression such as

x = (a×b+c×d)/(f+g)

where a, b, c, d, f, and g are positive.

To make sure that the result is always larger than the exact value, you can change the expression such that all roundings cause errors in the same direction. The example that follows changes the rounding direction to compute an upper bound for the expression, and then restores the previous rounding.

EXAMPLES
double_t big_divide(void)
{
   double_t x_up, a, b, c, d, f, g;
   int r;               /* specifies rounding direction */

   r = fegetround();    /* save caller's rounding direction */
   fesetround(FE_DOWNWARD);
                        /* downward rounding for denominator */
   x_up = f + g; 
   fesetround(FE_UPWARD);
                        /* upward rounding for expression */
   x_up = (a * b + c * d) / x_up; 
   fesetround(r);
                        /* restore caller's rounding direction */
   return(x_up);
}

Previous Book Contents Book Index Next

© Apple Computer, Inc.
13 JUL 1996