exp10.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* exp10.c
  2. *
  3. * Base 10 exponential function
  4. * (Common antilogarithm)
  5. *
  6. *
  7. *
  8. * SYNOPSIS:
  9. *
  10. * double x, y, exp10();
  11. *
  12. * y = exp10( x );
  13. *
  14. *
  15. *
  16. * DESCRIPTION:
  17. *
  18. * Returns 10 raised to the x power.
  19. *
  20. * Range reduction is accomplished by expressing the argument
  21. * as 10**x = 2**n 10**f, with |f| < 0.5 log10(2).
  22. * The Pade' form
  23. *
  24. * 1 + 2x P(x**2)/( Q(x**2) - P(x**2) )
  25. *
  26. * is used to approximate 10**f.
  27. *
  28. *
  29. *
  30. * ACCURACY:
  31. *
  32. * Relative error:
  33. * arithmetic domain # trials peak rms
  34. * IEEE -307,+307 30000 2.2e-16 5.5e-17
  35. * Test result from an earlier version (2.1):
  36. * DEC -38,+38 70000 3.1e-17 7.0e-18
  37. *
  38. * ERROR MESSAGES:
  39. *
  40. * message condition value returned
  41. * exp10 underflow x < -MAXL10 0.0
  42. * exp10 overflow x > MAXL10 MAXNUM
  43. *
  44. * DEC arithmetic: MAXL10 = 38.230809449325611792.
  45. * IEEE arithmetic: MAXL10 = 308.2547155599167.
  46. *
  47. */
  48. /*
  49. Cephes Math Library Release 2.8: June, 2000
  50. Copyright 1984, 1991, 2000 by Stephen L. Moshier
  51. */
  52. #include <math.h>
  53. #ifdef UNK
  54. static double P[] = {
  55. 4.09962519798587023075E-2,
  56. 1.17452732554344059015E1,
  57. 4.06717289936872725516E2,
  58. 2.39423741207388267439E3,
  59. };
  60. static double Q[] = {
  61. /* 1.00000000000000000000E0,*/
  62. 8.50936160849306532625E1,
  63. 1.27209271178345121210E3,
  64. 2.07960819286001865907E3,
  65. };
  66. /* static double LOG102 = 3.01029995663981195214e-1; */
  67. static double LOG210 = 3.32192809488736234787e0;
  68. static double LG102A = 3.01025390625000000000E-1;
  69. static double LG102B = 4.60503898119521373889E-6;
  70. /* static double MAXL10 = 38.230809449325611792; */
  71. static double MAXL10 = 308.2547155599167;
  72. #endif
  73. #ifdef DEC
  74. static unsigned short P[] = {
  75. 0037047,0165657,0114061,0067234,
  76. 0041073,0166243,0123052,0144643,
  77. 0042313,0055720,0024032,0047443,
  78. 0043025,0121714,0070232,0050007,
  79. };
  80. static unsigned short Q[] = {
  81. /*0040200,0000000,0000000,0000000,*/
  82. 0041652,0027756,0071216,0050075,
  83. 0042637,0001367,0077263,0136017,
  84. 0043001,0174673,0024157,0133416,
  85. };
  86. /*
  87. static unsigned short L102[] = {0037632,0020232,0102373,0147770};
  88. #define LOG102 *(double *)L102
  89. */
  90. static unsigned short L210[] = {0040524,0115170,0045715,0015613};
  91. #define LOG210 *(double *)L210
  92. static unsigned short L102A[] = {0037632,0020000,0000000,0000000,};
  93. #define LG102A *(double *)L102A
  94. static unsigned short L102B[] = {0033632,0102373,0147767,0114220,};
  95. #define LG102B *(double *)L102B
  96. static unsigned short MXL[] = {0041430,0166131,0047761,0154130,};
  97. #define MAXL10 ( *(double *)MXL )
  98. #endif
  99. #ifdef IBMPC
  100. static unsigned short P[] = {
  101. 0x2dd4,0xf306,0xfd75,0x3fa4,
  102. 0x5934,0x74c5,0x7d94,0x4027,
  103. 0x49e4,0x0503,0x6b7a,0x4079,
  104. 0x4a01,0x8e13,0xb479,0x40a2,
  105. };
  106. static unsigned short Q[] = {
  107. /*0x0000,0x0000,0x0000,0x3ff0,*/
  108. 0xca08,0xce51,0x45fd,0x4055,
  109. 0x7782,0xefd6,0xe05e,0x4093,
  110. 0xf6e2,0x650d,0x3f37,0x40a0,
  111. };
  112. /*
  113. static unsigned short L102[] = {0x79ff,0x509f,0x4413,0x3fd3};
  114. #define LOG102 *(double *)L102
  115. */
  116. static unsigned short L210[] = {0xa371,0x0979,0x934f,0x400a};
  117. #define LOG210 *(double *)L210
  118. static unsigned short L102A[] = {0x0000,0x0000,0x4400,0x3fd3,};
  119. #define LG102A *(double *)L102A
  120. static unsigned short L102B[] = {0xf312,0x79fe,0x509f,0x3ed3,};
  121. #define LG102B *(double *)L102B
  122. static double MAXL10 = 308.2547155599167;
  123. #endif
  124. #ifdef MIEEE
  125. static unsigned short P[] = {
  126. 0x3fa4,0xfd75,0xf306,0x2dd4,
  127. 0x4027,0x7d94,0x74c5,0x5934,
  128. 0x4079,0x6b7a,0x0503,0x49e4,
  129. 0x40a2,0xb479,0x8e13,0x4a01,
  130. };
  131. static unsigned short Q[] = {
  132. /*0x3ff0,0x0000,0x0000,0x0000,*/
  133. 0x4055,0x45fd,0xce51,0xca08,
  134. 0x4093,0xe05e,0xefd6,0x7782,
  135. 0x40a0,0x3f37,0x650d,0xf6e2,
  136. };
  137. /*
  138. static unsigned short L102[] = {0x3fd3,0x4413,0x509f,0x79ff};
  139. #define LOG102 *(double *)L102
  140. */
  141. static unsigned short L210[] = {0x400a,0x934f,0x0979,0xa371};
  142. #define LOG210 *(double *)L210
  143. static unsigned short L102A[] = {0x3fd3,0x4400,0x0000,0x0000,};
  144. #define LG102A *(double *)L102A
  145. static unsigned short L102B[] = {0x3ed3,0x509f,0x79fe,0xf312,};
  146. #define LG102B *(double *)L102B
  147. static double MAXL10 = 308.2547155599167;
  148. #endif
  149. #ifdef ANSIPROT
  150. extern double floor ( double );
  151. extern double ldexp ( double, int );
  152. extern double polevl ( double, void *, int );
  153. extern double p1evl ( double, void *, int );
  154. extern int isnan ( double );
  155. extern int isfinite ( double );
  156. #else
  157. double floor(), ldexp(), polevl(), p1evl();
  158. int isnan(), isfinite();
  159. #endif
  160. extern double MAXNUM;
  161. #ifdef INFINITIES
  162. extern double INFINITY;
  163. #endif
  164. double exp10(x)
  165. double x;
  166. {
  167. double px, xx;
  168. short n;
  169. #ifdef NANS
  170. if( isnan(x) )
  171. return(x);
  172. #endif
  173. if( x > MAXL10 )
  174. {
  175. #ifdef INFINITIES
  176. return( INFINITY );
  177. #else
  178. mtherr( "exp10", OVERFLOW );
  179. return( MAXNUM );
  180. #endif
  181. }
  182. if( x < -MAXL10 ) /* Would like to use MINLOG but can't */
  183. {
  184. #ifndef INFINITIES
  185. mtherr( "exp10", UNDERFLOW );
  186. #endif
  187. return(0.0);
  188. }
  189. /* Express 10**x = 10**g 2**n
  190. * = 10**g 10**( n log10(2) )
  191. * = 10**( g + n log10(2) )
  192. */
  193. px = floor( LOG210 * x + 0.5 );
  194. n = px;
  195. x -= px * LG102A;
  196. x -= px * LG102B;
  197. /* rational approximation for exponential
  198. * of the fractional part:
  199. * 10**x = 1 + 2x P(x**2)/( Q(x**2) - P(x**2) )
  200. */
  201. xx = x * x;
  202. px = x * polevl( xx, P, 3 );
  203. x = px/( p1evl( xx, Q, 3 ) - px );
  204. x = 1.0 + ldexp( x, 1 );
  205. /* multiply by power of 2 */
  206. x = ldexp( x, n );
  207. return(x);
  208. }