Microsoft KB Archive/98988

{|
 * width="100%"|

DOC: Floating Point Control Function Sample Code Incorrect

 * }

Q98988

-

The information in this article applies to:


 * The C Run-Time (CRT), included with:
 * Microsoft Visual C++, versions 1.0, 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 */


 * 1) include 
 * 2) include 
 * 3) include 
 * 4) include 
 * 5) include 

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

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

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

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

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

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

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

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

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

/* Restore default. */   binstr( _control87( _CW_DEFAULT, 0xffff ), tmpstr ); printf( &quot;%16s %.1f * %.1f = %.15e with default precision\n&quot;,      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

Issue type :

Technology : kbVCsearch kbAudDeveloper kbCRT