DOC: Floating Point Control Function Sample Code Incorrect

ID: Q98988


The information in this article applies to:


SUMMARY

The online help provided with Visual C++ contains an incorrect code sample to demonstrate the floating point control word functions _clear87(), _control87(), and _status87(). An attempt to view the code sample for these functions fails; the help system displays the code sample that demonstrates _getcwd() instead.


MORE INFORMATION

The code example below demonstrates the _clear87(), _control87(), and _status87() functions.

Sample Code


/* COPROC.C illustrates use of the status and control words of a
 * floating-point coprocessor (or emulator). Functions illustrated
 * include:   _clear87      _status87           _control87
 */ 

#include <stdio.h>
#include <conio.h>
#include <float.h>
#include <stdlib.h>
#include <string.h>

double dx = 1e-40, dy;
float fx, fy;
unsigned status, control;
char tmpstr[20];
char *binstr( int num, char *buffer );

void main()
{
   printf( "Status Word Key:\n" );
   printf( "B\tBusy flag\n0-3\tCondition codes\nS\tStack top pointer\n" );
   printf( "E\tError summary\nF\tStack flag\nP\tPrecision exception\n" );
   printf( "U\tUnderflow exception\nO\tOverflow exception\n" );
   printf( "Z\tZero divide exception\nD\tDenormalized exception\n" );
   printf( "I\tInvalid operation exception\n\n" );

   binstr(  _clear87(), tmpstr );
   printf( "B3SSS210EFPUOZDI  Function\tCondition\n\n" );
   printf( "%16s  _clear87\tAfter clearing\n", tmpstr );

   /* Storing double to float that hasn't enough precision for it
    * causes underflow and precision exceptions.
    */ 
   fx = dx;
   binstr(  _status87(), tmpstr );
   printf( "%16s  _status87\tAfter moving double to float\n", tmpstr );

   /* Storing float with lost precision back to double adds
    * denormalized exception (previous exceptions remain).
    */ 
   dy = fx;
   binstr(  _clear87(), tmpstr );
   printf( "%16s  _clear87\tAfter moving float to double\n", tmpstr );

   /* Using _clear87() erases previous exceptions. */ 
   fy = dy;
   binstr(  _status87(), tmpstr );
   printf( "%16s  _status87\tAfter moving double to float\n\n", tmpstr );

   _getch();
   printf( "Control Word Key:\n" );
   printf( "i\tInfinity control\nr\tRounding control\n" );
   printf( "p\tPrecision control\ne\tInterrupt enable mask\n" );
   printf( "U\tUnderflow mask\nO\tOverflow mask\n" );
   printf( "Z\tZero divide mask\nD\tDenormalized mask\n" );
   printf( "I\tInvalid operation mask\n\n" );
   printf( "???irrppe?PUOZDI  Result\n" );
   fy = .1;

   /* Show current control word. */ 
   binstr( _control87( 0, 0 ), tmpstr );
   printf( "%16s  %.1f * %.1f = %.15e with initial precision\n",
      tmpstr, fy, fy, fy * fy );

   /* Set precision to 24 bits. */ 
   binstr( _control87( _PC_24, _MCW_PC ), tmpstr );
   printf( "%16s  %.1f * %.1f = %.15e with 24-bit precision\n",
      tmpstr, fy, fy, fy * fy );

   /* Restore default. */ 
   binstr( _control87( _CW_DEFAULT, 0xffff ), tmpstr );
   printf( "%16s  %.1f * %.1f = %.15e with default precision\n",
      tmpstr, fy, fy, fy * fy );
}

/* Converts integer to string of 16 binary characters. */ 
char *binstr( int num, char *buffer )
{
   char tmp[17];
   int  len;

   memset( buffer, '0', 16 );
   len = strlen( _itoa( num, tmp, 2 ) );
   strcpy( buffer + 16 - len, tmp );
   return buffer;
} 

Additional query words: 1.00 1.50 1.51 1.52


Keywords          : kbdocerr kb16bitonly kbCRT kbVC kbVC100 kbVC150 kbVC151 kbVC152 
Version           : 1.00 1.50 1.51 1.52
Platform          : WINDOWS 
Issue type        : 

Last Reviewed: July 27, 1999