home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 1.ddi / MATHSRC.ZIP / _MATHERL.C next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  4.4 KB  |  123 lines

  1. /*------------------------------------------------------------------------
  2.  * filename - _matherl.c
  3.  *
  4.  * function(s)
  5.  *        __matherrl - long double floating-point error handling routine
  6.  *-----------------------------------------------------------------------*/
  7.  
  8. /*
  9.  *      C/C++ Run Time Library - Version 5.0
  10.  *
  11.  *      Copyright (c) 1987, 1992 by Borland International
  12.  *      All Rights Reserved.
  13.  *
  14.  */
  15.  
  16. #include <_math.h>
  17. #include <math.h>
  18. #include <stdio.h>
  19. #include <errno.h>
  20.  
  21. #ifdef _Windows
  22. #include <_win.h>
  23. #endif
  24.  
  25. /*-------------------------------------------------------------------------*
  26.  
  27. Name            __matherrl - long double floating-point error handling routine
  28.  
  29. Usage           #include<math.h>
  30.                 long double  __matherrl (_mexcep why,
  31.                                    char    *fun,
  32.                                    long double  *arg1p,
  33.                                    long double  *arg2p,
  34.                                    long double  retval);
  35.  
  36.  
  37. Prototype in    math.h
  38.  
  39. Description     __matherrl serves as a focal  point for error handling in all
  40.                 long double math library functions; it  calls _matherrl and
  41.                 processes the return value from _matherrl.  __matherrl should
  42.                 never be called directly by user programs.
  43.  
  44.                 Whenever  an  error  occurs  in  one  of  the  math library
  45.                 routines __matherrl is called with several arguments.
  46.  
  47.                 __matherrl does four things :
  48.                         . It  uses its arguments  to fill out  an exception
  49.                           structure.
  50.  
  51.                         . It  calls _matherrl  with  e,  a  pointer  to the
  52.                           _exceptionl  structure,  to see if _matherrl can
  53.                           resolve the error.
  54.  
  55.                         . It  examines  the  return  value from  _matherrl as
  56.                           follows:
  57.                                 If _matherrl  returns 0, (indicating  that it
  58.                                 was not able to resolve the error) __matherrl
  59.                                 sets errno and prints an error message
  60.  
  61.                                 If  _matherrl  returns  non-zero, (indicating
  62.                                 that  it  was  able  to  resolve the error)
  63.                                 __matherrl is  silent; it does not  set errno
  64.                                 or print any messages.
  65.  
  66.                         . It returns e->retval to the original caller. Note
  67.                           that  _matherrl might  modify e->retval  to specify
  68.                           the  value  it  wants   propagated  back  to  the
  69.                           original caller.
  70.  
  71.                 When  __matherrl sets  errno  (based  on  a  0  return  from
  72.                 _matherrl), it maps the kind of error that occurred (from the
  73.                 type field in the exception  structure) onto an errno value
  74.                 of either EDOM or ERANGE.
  75.  
  76. Return value    __matherrl returns the value, e->retval.
  77.  
  78. *-------------------------------------------------------------------------*/
  79.  
  80. long double _Cdecl _FARFUNC __matherrl(_mexcep why,
  81.                 char    *fun,
  82.                 long double     *arg1p,
  83.                 long double     *arg2p,
  84.                 long double     retval)
  85. {
  86.         struct  _exceptionl     e;
  87.  
  88.         e.type = why;
  89.         e.name = fun;
  90.         e.arg1 = (NULL == arg1p) ? 0 : *arg1p;
  91.         e.arg2 = (NULL == arg2p) ? 0 : *arg2p;
  92.         e.retval = retval;
  93.  
  94.         if (_matherrl (& e) == 0)
  95.                 {
  96. #ifdef _Windows
  97.                 char errMsg[ 80 ];
  98. #endif
  99.  
  100. #ifdef  UNIX__matherrl
  101. #    ifdef _Windows
  102.                 sprintf (errMsg, "%s (%8Lg,%8Lg): %s\n",
  103.                          fun, *arg1p, *arg2p, _mathwhy [why - 1]);
  104. #    else
  105.                 fprintf (stderr, "%s (%8Lg,%8Lg): %s\n",
  106.                          fun, *arg1p, *arg2p, _mathwhy [why - 1]);
  107. #    endif
  108. #else
  109. #    ifdef _Windows
  110.                 sprintf (errMsg, "%s: %s error\n", fun, _mathwhy [why - 1]);
  111. #    else
  112.                 fprintf (stderr, "%s: %s error\n", fun, _mathwhy [why - 1]);
  113. #    endif
  114. #endif
  115.  
  116.                 errno = ((OVERFLOW == why) || (UNDERFLOW == why)) ? ERANGE : EDOM;
  117. #ifdef _Windows
  118.                 _errorBox( errMsg );
  119. #endif
  120.                 }
  121.         return  e.retval;
  122. }
  123.