home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / math / cephes / qfloat / qtanh.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-17  |  1014 b   |  67 lines

  1. /*                            qtanh.c        */
  2. /* hyperbolic tangent check routine */
  3. /* this subroutine is used by the exponential function routine */
  4.  
  5. /* loop count adjusted for convergence to 9 word mantissa if x <= 2 */
  6.  
  7. #include "qhead.h"
  8.  
  9. extern short qone[], qtwo[];
  10.  
  11. int qtanh( x, y )
  12. short *x, *y;
  13. {
  14. short e[NQ], r[NQ], j[NQ], xx[NQ], m2[NQ];
  15. short i, n;
  16. long lj;
  17.  
  18. qmov( x, r );
  19. r[0] = 0;
  20. if( qcmp(r, qone) >= 0 )
  21.     {
  22. /* tanh(x) = (exp(x) - exp(-x)) / (exp(x) + exp(-x))
  23.  * Note qexp() calls qtanh, but with an argument less than (1 + log 2)/2.
  24.  */
  25.     qexp( r, e );
  26.     qdiv( e, qone, r );
  27.     qsub( r, e, xx );
  28.     qadd( r, e, j );
  29.     qdiv( j, xx, y );
  30.     goto done;
  31.     }
  32.  
  33. qmov( qtwo, m2 );
  34. qneg( m2 );
  35.  
  36. n = NBITS/9; /*10;*/
  37. lj = 2 * n + 1;
  38. ltoq( &lj, j );
  39.  
  40. /* constant 31:
  41. qmov( qone, j );
  42. j[1] = 040005;
  43. j[3] = 0174000;
  44. */
  45.  
  46. qmov( j, e );
  47. qmul( x, x, xx );
  48.  
  49. /* continued fraction */
  50.  
  51. /*for( i=0; i<15; i++)*/
  52.  
  53.  
  54. for( i=0; i<n; i++)
  55.     {
  56.     qdiv( e, xx, r );
  57.     qadd( m2, j, j );
  58.     qadd( r, j, e );
  59.     }
  60.  
  61. qdiv( e, x, y );
  62.  
  63. done:
  64. if( x[0] < 0 )
  65.     y[0] = -1;
  66. }
  67.