DOCERR: Floating Point Control Function Sample Code Incorrect

Last reviewed: July 17, 1997
Article ID: Q98988
1.00 1.50 1.51 1.52 WINDOWS kbprg kbdocerr

The information in this article applies to:

   The C Run-time (CRT) included with:
    - Microsoft Visual C++ for Windows, versions 1.0 and 1.5

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 reference words: 1.00 1.50 1.51 1.52
KBCategory: kbprg kbdocerr
KBSubcategory: CRTIss
Keywords : CRTIss kb16bitonly kbdocerr kbprg
Version : 1.00 1.50 1.51 1.52
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 17, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.