eexp.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* xexp.c */
  2. /* exponential function check routine */
  3. /* by Stephen L. Moshier. */
  4. #include "ehead.h"
  5. /*
  6. extern int powinited;
  7. extern short maxposint[], maxnegint[];
  8. */
  9. void eexp( x, y )
  10. unsigned short *x, *y;
  11. {
  12. unsigned short num[NE], den[NE], x2[NE];
  13. long i;
  14. unsigned short sign, expchk;
  15. /* range reduction theory: x = i + f, 0<=f<1;
  16. * e**x = e**i * e**f
  17. * e**i = 2**(i/log 2).
  18. * Let i/log2 = i1 + f1, 0<=f1<1.
  19. * Then e**i = 2**i1 * 2**f1, so
  20. * e**x = 2**i1 * e**(log 2 * f1) * e**f.
  21. */
  22. /*
  23. if( powinited == 0 )
  24. initpow();
  25. */
  26. if( ecmp(x, ezero) == 0 )
  27. {
  28. emov( eone, y );
  29. return;
  30. }
  31. emov(x, x2);
  32. expchk = x2[NE-1];
  33. sign = expchk & 0x8000;
  34. x2[NE-1] &= 0x7fff;
  35. /* Test for excessively large argument */
  36. expchk &= 0x7fff;
  37. if( expchk > (EXONE + 15) )
  38. {
  39. eclear( y );
  40. if( sign == 0 )
  41. einfin( y );
  42. return;
  43. }
  44. eifrac( x2, &i, num ); /* x = i + f */
  45. if( i != 0 )
  46. {
  47. ltoe( &i, den ); /* floating point i */
  48. ediv( elog2, den, den ); /* i/log 2 */
  49. eifrac( den, &i, den ); /* i/log 2 = i1 + f1 */
  50. emul( elog2, den, den ); /* log 2 * f1 */
  51. eadd( den, num, x2 ); /* log 2 * f1 + f */
  52. }
  53. /*x2[NE-1] -= 1;*/
  54. eldexp( x2, -1L, x2 ); /* divide by 2 */
  55. etanh( x2, x2 ); /* tanh( x/2 ) */
  56. eadd( x2, eone, num ); /* 1 + tanh */
  57. eneg( x2 );
  58. eadd( x2, eone, den ); /* 1 - tanh */
  59. ediv( den, num, y ); /* (1 + tanh)/(1 - tanh) */
  60. /*y[NE-1] += i;*/
  61. if( sign )
  62. {
  63. ediv( y, eone, y );
  64. i = -i;
  65. }
  66. eldexp( y, i, y ); /* multiply by 2**i */
  67. }