home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / c / other / math / coproc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-27  |  2.9 KB  |  87 lines

  1. /* COPROC.C illustrates use of the status and control words of a floating
  2.  * point coprocessor (or emulator). Functions illustrated include:
  3.  *      _clear87            _status87           _control87
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <conio.h>
  8. #include <float.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11.  
  12. double dx = 1e-40, dy;
  13. float fx, fy;
  14. unsigned status, control;
  15. char tmpstr[20];
  16. char *binstr( int num, char *buffer );
  17.  
  18. main()
  19. {
  20.     printf( "Status Word Key:\n" );
  21.     printf( "B\tBusy flag\n0-3\tCondition codes\nS\tStack top pointer\n" );
  22.     printf( "E\tError summary\nF\tStack flag\nP\tPrecision exception\n" );
  23.     printf( "U\tUnderflow exception\nO\tOverflow exception\n" );
  24.     printf( "Z\tZero divide exception\nD\tDenormalized exception\n" );
  25.     printf( "I\tInvalid operation exception\n\n" );
  26.  
  27.     binstr(  _clear87(), tmpstr );
  28.     printf( "B3SSS210EFPUOZDI  Function\tCondition\n\n" );
  29.     printf( "%16s  _clear87\tAfter clearing\n", tmpstr );
  30.  
  31.     /* Storing double to float that hasn't enough precision for it
  32.      * causes underflow and precision exceptions.
  33.      */
  34.     fx = dx;
  35.     binstr(  _status87(), tmpstr );
  36.     printf( "%16s  _status87\tAfter moving double to float\n", tmpstr );
  37.  
  38.     /* Storing float with lost precision back to double adds denormalized
  39.      * exception (previous exceptions remain).
  40.      */
  41.     dy = fx;
  42.     binstr(  _clear87(), tmpstr );
  43.     printf( "%16s  _clear87\tAfter moving float to double\n", tmpstr );
  44.  
  45.     /* Using clear87() erases previous exceptions. */
  46.     fy = dy;
  47.     binstr(  _status87(), tmpstr );
  48.     printf( "%16s  _status87\tAfter moving double to float\n\n", tmpstr );
  49.  
  50.     getch();
  51.     printf( "Control Word Key:\n" );
  52.     printf( "i\tInfinity control\nr\tRounding control\n" );
  53.     printf( "p\tPrecision control\ne\tInterrupt enable mask\n" );
  54.     printf( "U\tUnderflow mask\nO\tOverflow mask\n" );
  55.     printf( "Z\tZero divide mask\nD\tDenormalized mask\n" );
  56.     printf( "I\tInvalid operation mask\n\n" );
  57.     printf( "???irrppe?PUOZDI  Result\n" );
  58.     fy = .1;
  59.  
  60.     /* Show current control word. */
  61.     binstr( _control87( 0, 0 ), tmpstr );
  62.     printf( "%16s  %.1f * %.1f = %.15e with initial precision\n",
  63.             tmpstr, fy, fy, fy * fy );
  64.  
  65.     /* Set precision to 24 bits. */
  66.     binstr( _control87( PC_24, MCW_PC ), tmpstr );
  67.     printf( "%16s  %.1f * %.1f = %.15e with 24 bit precision\n",
  68.             tmpstr, fy, fy, fy * fy );
  69.  
  70.     /* Restore default. */
  71.     binstr( _control87( CW_DEFAULT, 0xffff ), tmpstr );
  72.     printf( "%16s  %.1f * %.1f = %.15e with default precision\n",
  73.             tmpstr, fy, fy, fy * fy );
  74. }
  75.  
  76. /* Convert integer to string of 16 binary characters. */
  77. char *binstr( int num, char *buffer )
  78. {
  79.     char tmp[17];
  80.     int  len;
  81.  
  82.     memset( buffer, '0', 16 );
  83.     len = strlen( itoa( num, tmp, 2 ) );
  84.     strcpy( buffer + 16 - len, tmp );
  85.     return buffer;
  86. }
  87.