monotl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* monot.c
  2. Floating point function test vectors.
  3. Arguments and function values are synthesized for NPTS points in
  4. the vicinity of each given tabulated test point. The points are
  5. chosen to be near and on either side of the likely function algorithm
  6. domain boundaries. Since the function programs change their methods
  7. at these points, major coding errors or monotonicity failures might be
  8. detected.
  9. August, 1998
  10. S. L. Moshier */
  11. #include <stdio.h>
  12. /* Avoid including math.h. */
  13. long double frexpl (long double, int *);
  14. long double ldexpl (long double, int);
  15. /* Number of test points to generate on each side of tabulated point. */
  16. #define NPTS 100
  17. /* Functions of one variable. */
  18. long double expl (long double);
  19. long double logl (long double);
  20. long double sinl (long double);
  21. long double cosl (long double);
  22. long double tanl (long double);
  23. long double atanl (long double);
  24. long double asinl (long double);
  25. long double acosl (long double);
  26. long double sinhl (long double);
  27. long double coshl (long double);
  28. long double tanhl (long double);
  29. long double asinhl (long double);
  30. long double acoshl (long double);
  31. long double atanhl (long double);
  32. long double gammal (long double);
  33. long double fabsl (long double);
  34. long double floorl (long double);
  35. struct oneargument
  36. {
  37. char *name; /* Name of the function. */
  38. long double (*func) (long double);
  39. long double arg1; /* Function argument, assumed exact. */
  40. long double answer1; /* Exact, close to function value. */
  41. long double answer2; /* answer1 + answer2 has extended precision. */
  42. long double derivative; /* dy/dx evaluated at x = arg1. */
  43. int thresh; /* Error report threshold. 2 = 1 ULP approx. */
  44. };
  45. /* Add this to error threshold test[i].thresh. */
  46. #define OKERROR 2
  47. /* Unit of relative error in test[i].thresh. */
  48. static long double MACHEPL = 5.42101086242752217003726400434970855712890625E-20L;
  49. /* extern double MACHEP; */
  50. struct oneargument test1[] =
  51. {
  52. {"exp", expl, 1.0L, 2.7182769775390625L,
  53. 4.85091998273536028747e-6L, 2.71828182845904523536L, 1},
  54. {"exp", expl, -1.0L, 3.678741455078125e-1L,
  55. 5.29566362982159552377e-6L, 3.678794411714423215955e-1L, 1},
  56. {"exp", expl, 0.5L, 1.648712158203125L,
  57. 9.1124970031468486507878e-6L, 1.64872127070012814684865L, 1},
  58. {"exp", expl, -0.5L, 6.065216064453125e-1L,
  59. 9.0532673209236037995e-6L, 6.0653065971263342360e-1L, 1},
  60. {"exp", expl, 2.0L, 7.3890533447265625L,
  61. 2.75420408772723042746e-6L, 7.38905609893065022723L, 1},
  62. {"exp", expl, -2.0L, 1.353302001953125e-1L,
  63. 5.08304130019189399949e-6L, 1.3533528323661269189e-1L, 1},
  64. {"log", logl, 1.41421356237309492343L, 3.465728759765625e-1L,
  65. 7.1430341006605745676897e-7L, 7.0710678118654758708668e-1L, 1},
  66. {"log", logl, 7.07106781186547461715e-1L, -3.46588134765625e-1L,
  67. 1.45444856522566402246e-5L, 1.41421356237309517417L, 1},
  68. {"sin", sinl, 7.85398163397448278999e-1L, 7.0709228515625e-1L,
  69. 1.4496030297502751942956e-5L, 7.071067811865475460497e-1L, 1},
  70. {"sin", sinl, -7.85398163397448501044e-1L, -7.071075439453125e-1L,
  71. 7.62758764840238811175e-7L, 7.07106781186547389040e-1L, 1},
  72. {"sin", sinl, 1.570796326794896558L, 9.999847412109375e-1L,
  73. 1.52587890625e-5L, 6.12323399573676588613e-17L, 1},
  74. {"sin", sinl, -1.57079632679489678004L, -1.0L,
  75. 1.29302922820150306903e-32L, -1.60812264967663649223e-16L, 1},
  76. {"sin", sinl, 4.712388980384689674L, -1.0L,
  77. 1.68722975549458979398e-32L, -1.83697019872102976584e-16L, 1},
  78. {"sin", sinl, -4.71238898038468989604L, 9.999847412109375e-1L,
  79. 1.52587890625e-5L, 3.83475850529283315008e-17L, 1},
  80. {"cos", cosl, 3.92699081698724139500E-1L, 9.23873901367187500000E-1L,
  81. 5.63114409926198633370E-6L, -3.82683432365089757586E-1L, 1},
  82. {"cos", cosl, 7.85398163397448278999E-1L, 7.07092285156250000000E-1L,
  83. 1.44960302975460497458E-5L, -7.07106781186547502752E-1L, 1},
  84. {"cos", cosl, 1.17809724509617241850E0L, 3.82675170898437500000E-1L,
  85. 8.26146665231415693919E-6L, -9.23879532511286738554E-1L, 1},
  86. {"cos", cosl, 1.96349540849362069750E0L, -3.82690429687500000000E-1L,
  87. 6.99732241029898567203E-6L, -9.23879532511286785419E-1L, 1},
  88. {"cos", cosl, 2.35619449019234483700E0L, -7.07107543945312500000E-1L,
  89. 7.62758765040545859856E-7L, -7.07106781186547589348E-1L, 1},
  90. {"cos", cosl, 2.74889357189106897650E0L, -9.23889160156250000000E-1L,
  91. 9.62764496328487887036E-6L, -3.82683432365089870728E-1L, 1},
  92. {"cos", cosl, 3.14159265358979311600E0L, -1.00000000000000000000E0L,
  93. 7.49879891330928797323E-33L, -1.22464679914735317723E-16L, 1},
  94. {"tan", tanl, 7.85398163397448278999E-1L, 9.999847412109375e-1L,
  95. 1.52587890624387676600E-5L, 1.99999999999999987754E0L, 1},
  96. {"tan", tanl, 1.17809724509617241850E0L, 2.41419982910156250000E0L,
  97. 1.37332715322352112604E-5L, 6.82842712474618858345E0L, 1},
  98. {"tan", tanl, 1.96349540849362069750E0L, -2.41421508789062500000E0L,
  99. 1.52551752942854759743E-6L, 6.82842712474619262118E0L, 1},
  100. {"tan", tanl, 2.35619449019234483700E0L, -1.00001525878906250000E0L,
  101. 1.52587890623163029801E-5L, 2.00000000000000036739E0L, 1},
  102. {"tan", tanl, 2.74889357189106897650E0L, -4.14215087890625000000E-1L,
  103. 1.52551752982565655126E-6L, 1.17157287525381000640E0L, 1},
  104. {"atan", atanl, 4.14213562373094923430E-1L, 3.92684936523437500000E-1L,
  105. 1.41451752865477964149E-5L, 8.53553390593273837869E-1L, 1},
  106. {"atan", atanl, 1.0L, 7.85385131835937500000E-1L,
  107. 1.30315615108096156608E-5L, 0.5L, 1},
  108. {"atan", atanl, 2.41421356237309492343E0L, 1.17808532714843750000E0L,
  109. 1.19179477349460632350E-5L, 1.46446609406726250782E-1L, 1},
  110. {"atan", atanl, -2.41421356237309514547E0L, -1.17810058593750000000E0L,
  111. 3.34084132752141908545E-6L, 1.46446609406726227789E-1L, 1},
  112. {"atan", atanl, -1.0L, -7.85400390625000000000E-1L,
  113. 2.22722755169038433915E-6L, 0.5L, 1},
  114. {"atan", atanl, -4.14213562373095145475E-1L, -3.92700195312500000000E-1L,
  115. 1.11361377576267665972E-6L, 8.53553390593273703853E-1L, 1},
  116. {"asin", asinl, 3.82683432365089615246E-1L, 3.92684936523437500000E-1L,
  117. 1.41451752864854321970E-5L, 1.08239220029239389286E0L, 1},
  118. {"asin", asinl, 0.5L, 5.23590087890625000000E-1L,
  119. 8.68770767387307710723E-6L, 1.15470053837925152902E0L, 1},
  120. {"asin", asinl, 7.07106781186547461715E-1L, 7.85385131835937500000E-1L,
  121. 1.30315615107209645016E-5L, 1.41421356237309492343E0L, 1},
  122. {"asin", asinl, 9.23879532511286738483E-1L, 1.17808532714843750000E0L,
  123. 1.19179477349183147612E-5L, 2.61312592975275276483E0L, 1},
  124. {"asin", asinl, -0.5L, -5.23605346679687500000E-1L,
  125. 6.57108138862692289277E-6L, 1.15470053837925152902E0L, 1},
  126. {"acos", acosl, 1.95090322016128192573E-1L, 1.37443542480468750000E0L,
  127. 1.13611408471185777914E-5L, -1.01959115820831832232E0L, 1},
  128. {"acos", acosl, 3.82683432365089615246E-1L, 1.17808532714843750000E0L,
  129. 1.19179477351337991247E-5L, -1.08239220029239389286E0L, 1},
  130. {"acos", acosl, 0.5L, 1.04719543457031250000E0L,
  131. 2.11662628524615421446E-6L, -1.15470053837925152902E0L, 1},
  132. {"acos", acosl, 7.07106781186547461715E-1L, 7.85385131835937500000E-1L,
  133. 1.30315615108982668201E-5L, -1.41421356237309492343E0L, 1},
  134. {"acos", acosl, 9.23879532511286738483E-1L, 3.92684936523437500000E-1L,
  135. 1.41451752867009165605E-5L, -2.61312592975275276483E0L, 1},
  136. {"acos", acosl, 9.80785280403230430579E-1L, 1.96334838867187500000E-1L,
  137. 1.47019821746724723933E-5L, -5.12583089548300990774E0L, 1},
  138. {"acos", acosl, -0.5L, 2.09439086914062500000E0L,
  139. 4.23325257049230842892E-6L, -1.15470053837925152902E0L, 1},
  140. {"sinh", sinhl, 1.0L, 1.17518615722656250000E0L,
  141. 1.50364172389568823819E-5L, 1.54308063481524377848E0L, 1},
  142. {"sinh", sinhl, 7.09089565712818057364E2L, 4.49423283712885057274E307L,
  143. 4.25947714184369757620E208L, 4.49423283712885057274E307L, 1},
  144. {"sinh", sinhl, 2.22044604925031308085E-16L, 0.00000000000000000000E0L,
  145. 2.22044604925031308085E-16L, 1.00000000000000000000E0L, 1},
  146. {"cosh", coshl, 7.09089565712818057364E2L, 4.49423283712885057274E307L,
  147. 4.25947714184369757620E208L, 4.49423283712885057274E307L, 1},
  148. {"cosh", coshl, 1.0L, 1.54307556152343750000E0L,
  149. 5.07329180627847790562E-6L, 1.17520119364380145688E0L, 1},
  150. {"cosh", coshl, 0.5L, 1.12762451171875000000E0L,
  151. 1.45348763078522622516E-6L, 5.21095305493747361622E-1L, 1},
  152. {"tanh", tanhl, 0.5L, 4.62112426757812500000E-1L,
  153. 4.73050219725850231848E-6L, 7.86447732965927410150E-1L, 1},
  154. {"tanh", tanhl, 5.49306144334054780032E-1L, 4.99984741210937500000E-1L,
  155. 1.52587890624507506378E-5L, 7.50000000000000049249E-1L, 1},
  156. {"tanh", tanhl, 0.625L, 5.54595947265625000000E-1L,
  157. 3.77508375729399903910E-6L, 6.92419147969988069631E-1L, 1},
  158. {"asinh", asinhl, 0.5L, 4.81201171875000000000E-1L,
  159. 1.06531846034474977589E-5L, 8.94427190999915878564E-1L, 1},
  160. {"asinh", asinhl, 1.0L, 8.81362915039062500000E-1L,
  161. 1.06719804805252326093E-5L, 7.07106781186547524401E-1L, 1},
  162. {"asinh", asinhl, 2.0L, 1.44363403320312500000E0L,
  163. 1.44197568534249327674E-6L, 4.47213595499957939282E-1L, 1},
  164. {"acosh", acoshl, 2.0L, 1.31695556640625000000E0L,
  165. 2.33051856670862504635E-6L, 5.77350269189625764509E-1L, 1},
  166. {"acosh", acoshl, 1.5L, 9.62417602539062500000E-1L,
  167. 6.04758014439499551783E-6L, 8.94427190999915878564E-1L, 1},
  168. {"acosh", acoshl, 1.03125L, 2.49343872070312500000E-1L,
  169. 9.62177257298785143908E-6L, 3.96911150685467059809E0L, 1},
  170. {"atanh", atanhl, 0.5L, 5.49301147460937500000E-1L,
  171. 4.99687311734569762262E-6L, 1.33333333333333333333E0L, 1},
  172. #if 0
  173. {"gamma", gammal, 1.0L, 1.0L,
  174. 0.0L, -5.772156649015328606e-1L, 1},
  175. {"gamma", gammal, 2.0L, 1.0L,
  176. 0.0L, 4.2278433509846713939e-1L, 1},
  177. {"gamma", gammal, 3.0L, 2.0L,
  178. 0.0L, 1.845568670196934279L, 1},
  179. {"gamma", gammal, 4.0L, 6.0L,
  180. 0.0L, 7.536706010590802836L, 1},
  181. #endif
  182. {"null", NULL, 0.0L, 0.0L, 0.0L, 1},
  183. };
  184. /* These take care of extra-precise floating point register problems. */
  185. volatile long double volat1;
  186. volatile long double volat2;
  187. /* Return the next nearest floating point value to X
  188. in the direction of UPDOWN (+1 or -1).
  189. (Fails if X is denormalized.) */
  190. long double
  191. nextval (x, updown)
  192. long double x;
  193. int updown;
  194. {
  195. long double m;
  196. int i;
  197. volat1 = x;
  198. m = 0.25L * MACHEPL * volat1 * updown;
  199. volat2 = volat1 + m;
  200. if (volat2 != volat1)
  201. printf ("successor failed\n");
  202. for (i = 2; i < 10; i++)
  203. {
  204. volat2 = volat1 + i * m;
  205. if (volat1 != volat2)
  206. return volat2;
  207. }
  208. printf ("nextval failed\n");
  209. return volat1;
  210. }
  211. int
  212. main ()
  213. {
  214. long double (*fun1) (long double);
  215. int i, j, errs, tests;
  216. long double x, x0, y, dy, err;
  217. errs = 0;
  218. tests = 0;
  219. i = 0;
  220. for (;;)
  221. {
  222. fun1 = test1[i].func;
  223. if (fun1 == NULL)
  224. break;
  225. volat1 = test1[i].arg1;
  226. x0 = volat1;
  227. x = volat1;
  228. for (j = 0; j <= NPTS; j++)
  229. {
  230. volat1 = x - x0;
  231. dy = volat1 * test1[i].derivative;
  232. dy = test1[i].answer2 + dy;
  233. volat1 = test1[i].answer1 + dy;
  234. volat2 = (*(fun1)) (x);
  235. if (volat2 != volat1)
  236. {
  237. /* Report difference between program result
  238. and extended precision function value. */
  239. err = volat2 - test1[i].answer1;
  240. err = err - dy;
  241. err = err / volat1;
  242. if (fabsl (err) > ((OKERROR + test1[i].thresh) * MACHEPL))
  243. {
  244. printf ("%d %s(%.19Le) = %.19Le, rel err = %.3Le\n",
  245. j, test1[i].name, x, volat2, err);
  246. errs += 1;
  247. }
  248. }
  249. x = nextval (x, 1);
  250. tests += 1;
  251. }
  252. x = x0;
  253. x = nextval (x, -1);
  254. for (j = 1; j < NPTS; j++)
  255. {
  256. volat1 = x - x0;
  257. dy = volat1 * test1[i].derivative;
  258. dy = test1[i].answer2 + dy;
  259. volat1 = test1[i].answer1 + dy;
  260. volat2 = (*(fun1)) (x);
  261. if (volat2 != volat1)
  262. {
  263. err = volat2 - test1[i].answer1;
  264. err = err - dy;
  265. err = err / volat1;
  266. if (fabsl (err) > ((OKERROR + test1[i].thresh) * MACHEPL))
  267. {
  268. printf ("%d %s(%.19Le) = %.19Le, rel err = %.3Le\n",
  269. j, test1[i].name, x, volat2, err);
  270. errs += 1;
  271. }
  272. }
  273. x = nextval (x, -1);
  274. tests += 1;
  275. }
  276. i += 1;
  277. }
  278. printf ("%d errors in %d tests\n", errs, tests);
  279. }