home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / PROG / PASCAL / MISCTI10.ZIP / TI399.ASC < prev    next >
Encoding:
Text File  |  1988-05-02  |  2.5 KB  |  85 lines

  1.  
  2. The following function was not documented in the Turbo C 1.0
  3. manuals:
  4.  
  5. _control87 - manipulates floating-point control word
  6.  
  7. Usage           unsigned int  _control87(unsigned int newvals,
  8.                         unsigned int mask);
  9.  
  10. Prototype in    float.h
  11.  
  12. Description     This function is used to retrieve or change the
  13.                         floating-point control word. 
  14.  
  15. The floating-point control word is an unsigned int that, bit by
  16. bit, specifies certain modes in the floating-point package;
  17. namely, the precision, infinity and rounding modes.  Changing
  18. these modes allows you to mask or unmask floating-point
  19. exceptions.
  20.  
  21. _control87 matches the bits in mask to the bits in newvals. If a
  22. mask bit = 1, the corresponding bit in newvals contains the new
  23. value for the same bit in the floating-point control word, and
  24. _control87 sets that bit in the control word to the new value.
  25.  
  26. Here's a simple illustration of how this works:
  27.  
  28.                 Original control word: 0100 0011 0110 0011
  29.  
  30.                               mask     1000 0001 0100 1111
  31.                               newvals  1110 1001 0000 0101
  32.  
  33.                         Changing bits  1--- ---1 -0-- 0101
  34.  
  35. If mask = 0, _control87 returns the floating-point control word
  36. without altering it.
  37.  
  38. Return value    The bits in the value returned reflect the new       
  39.                 floating-point control word.  For a complete
  40.                 definition of the bits returned by _control87,
  41.                 see float.h.
  42.  
  43. /*--------------------------------------------------------------
  44.  * floatrap.c
  45.  *------------------------------------------------------------*/
  46. #include <stdio.h>
  47. #include <float.h>
  48.  
  49.  
  50. /*   8087 control word mask  */
  51.  
  52. #define CWNEW (RC_NEAR+PC_64+EM_DENORMAL+ \
  53.                EM_UNDERFLOW+EM_OVERFLOW+EM_ZERODIVIDE+EM_INEXACT)
  54.  
  55. #define MASKALL 0xFFFF
  56.  
  57. /*--------------------------------------------------------------
  58.  * main
  59.  *------------------------------------------------------------*/
  60. main() {
  61.   double ans,ref,f;
  62.  
  63.   _control87(CWNEW,MASKALL);
  64.  
  65.  
  66.   puts("/* an underflow */");
  67.   ref = 2.0e-200;
  68.   f = 2.0e200;
  69.   ans = ref/f;
  70.   printf("ref: %le\nnum: %le\nans: %le\n",ref,f,ans);
  71.  
  72.   puts("/* an overflow */");
  73.   ref = 2.0e200;
  74.   f = 2.0e200;
  75.   ans = ref*f;
  76.   printf("ref: %le\nnum: %le\nans: %le\n",ref,f,ans);
  77.  
  78.   puts("/* a division by zero */");
  79.   ref = 2.0;
  80.   f = 0.0;
  81.   ans = ref/f;
  82.   printf("ref: %le\nnum: %le\nans: %le\n",ref,f,ans);
  83.  
  84. }
  85.