math.tst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. % Test of Math Package.
  2. % Author: Stanley L. Kameny <stan%valley.uucp@rand.org>.
  3. % Copyright (c) 1987, 1988, 1989, 1991 Stanley L. Kameny.
  4. % All Rights Reserved.
  5. %*********************************************************************
  6. %**
  7. %** This math package will compute the floating point values of **
  8. %** the usual elementary functions, namely: **
  9. %** sin asin sind asind sinh asinh **
  10. %** cos acos cosd acosd cosh acosh **
  11. %** tan atan tand atand tanh atanh **
  12. %** cot acot cotd acotd coth acoth **
  13. %** sec asec secd asecd sech asech **
  14. %** csc acsc cscd acscd csch acsch **
  15. %** atan2 atan2d **
  16. %** exp ln sqrt **
  17. %** expt log cbrt **
  18. %** logb hypot **
  19. %** log10 floor **
  20. %** ceiling **
  21. %** round **
  22. %** **
  23. %** All functions are computed to the accuracy of the floating- **
  24. %** point precision of the system set up at the time. **
  25. %** **
  26. %*********************************************************************
  27. %** File #1===Trig Function Tests===
  28. %** Trig functions are tested in both degrees and radians modes.
  29. %*********************************************************************
  30. symbolic;
  31. math!!label;
  32. symbolic procedure terpr(i,j); if remainder(i,j)=0 then terpri()$
  33. % #1: sind**2+cosd**2 test: ideal answers 1.0 for all args.
  34. for i:=0:45 do <<write " ",i,"->",sind float i**2+cosd float i**2;
  35. terpr(i,4)>>;
  36. % #2: quadrant test of sind, cosd: proper answers + +,+ -,- -,- +.
  37. begin scalar a;
  38. a:= sind 45.0;
  39. for i:= 0.0:3.0 do
  40. <<write " ",sind(i*90+45)/a," ", cosd (i*90+45)/a;terpri()>>
  41. end$
  42. % #3: scaling test: all values should be 1 exactly.
  43. begin scalar a; a:= cosd 60.0;
  44. for i:= -10.0:10.0 do write fix(cosd(60+i*360)/a)," "
  45. end$
  46. % #4: test of radians -> degrees evaluation: ideal values 1.0.
  47. array a(6)$
  48. begin
  49. for i:=1:6 do a(i):=sind(15.0*i);
  50. for i:=1:6 do <<write sin(pii2*i/6.0)/a(i)," "; terpr(i,3)>>
  51. end$
  52. % #5: test of tand*cotd: ideal values 1.0.
  53. begin
  54. for i:=5 step 5 until 85 do
  55. <<write tand float i*cotd float i," "; terpr(i,25)>>;
  56. terpri()
  57. end$
  58. % #6: test of secd**2-tand**2: ideal values 1.0.
  59. begin
  60. for i:=5 step 5 until 85 do
  61. <<write secd float i**2-tand float i**2," "; terpr(i,25)>>
  62. end$
  63. % #7: test of cscd**2-cotd**2: ideal values 1.0.
  64. begin
  65. for i:=5 step 5 until 85 do
  66. <<write cscd float i**2-cotd float i**2," "; terpr(i,25)>>
  67. end$
  68. % #8: test of asind+acosd: ideal values 1.0.
  69. begin write "sind and cosd"; terpri();
  70. for i:=-10:10 do
  71. <<write (asind(0.1*i)+acosd(0.1*i))/90," "; terpr(i,5)>>;
  72. write "sin and cos";terpri();
  73. for i:=-10:10 do
  74. <<write (acos(0.1*i)+asin(0.1*i))/pii2," "; terpr(i,5)>>
  75. end$
  76. % #9: test of atand+acotd: ideal values 1.0.
  77. begin scalar x; write "tand, atand and acotd"; terpri();
  78. for i:=-80 step 10 until 80 do
  79. <<x:=tand float i; write (atand x+acotd x)/90," "; terpr(i,50)>>;
  80. terpri();
  81. write "tan, atan and acot";terpri();
  82. for i:=-80 step 10 until 80 do
  83. <<x:= tan (pii2*i/90.0); write (atan x+acot x)/pii2," ";
  84. terpr(i,50)>>
  85. end$
  86. % #10: test of atand tand: ideal values i for i:=-9:89.
  87. begin
  88. for i:=-9:89 do
  89. <<write " ",i,"->",if i=0 then 1.0 else atand tand float i;
  90. terpr(i,4)>>
  91. end$
  92. % #11: test of acot cotd: ideal values 10*i for i:=1:17.
  93. begin
  94. for i:=10 step 10 until 170 do
  95. <<write " ",i,"->",acotd cotd i; terpr(i,40)>>; terpri();terpri() end$
  96. % #12: test of asind sind: ideal values 10*i for i:=-9:9.
  97. begin
  98. for i:=-90 step 10 until 90 do
  99. <<write " ",i,"->",asind sind float i; terpr(i,40)>>
  100. end$
  101. % #13: test of acosd cosd: ideal values 10*i for i:=1:18.
  102. begin
  103. for i:=10 step 10 until 180 do
  104. <<write " ",i,"->",acosd cosd float i; terpr(i,40)>>
  105. end$
  106. % #14: test of acscd cscd: ideal values 10*i for i:=-9:9, except
  107. % error for i=0.
  108. begin
  109. for i:=-90 step 10 until 90 do
  110. <<write " ",i,"->",if i=0 then "error" else acscd cscd float i;
  111. terpr(i,40)>>
  112. end$
  113. % #15: test of asecd secd: ideal values 10*i for i :=0:18. except
  114. % error for i=9.
  115. begin
  116. for i:=0 step 10 until 180 do
  117. <<write" ",i,"->",if i=90 then "error" else asecd secd float i;
  118. terpr(i,40)>>
  119. end$
  120. %*********************************************************************
  121. %** ===Exp,Log,Sqrt,Cbrt, and Expt Function tests===
  122. %*********************************************************************
  123. % #16: test of properties of exp function: ideal results 1.0.
  124. array b(5)$
  125. begin scalar x; x:=0;
  126. write "multiplicative property";terpri();
  127. for i:=0:5 do b(i):=1+i/6.0; for i:=0:5 do for j:=i:5 do
  128. <<write " ",exp (b(i)+b(j))/exp(b(i))/exp(b(j));
  129. terpr(x:=x+1,5)>>
  130. end$
  131. % #17: various properties of exp: ideal results 1.0.
  132. begin write "inverse property"$ terpri()$
  133. for i:=1:5 do write " ",exp(b(i))*exp(-b(i));terpri();
  134. write "squares"; terpri();
  135. for i:=-10:10 do
  136. <<write " ",sqrt(exp(0.2*i))/exp(0.1*i); terpr(i,5)>>;
  137. write "cubes"; terpri();
  138. for i:=-10:10 do
  139. <<write " ",cbrt(exp(0.3*i))/exp(0.1*i); terpr(i,5)>>
  140. end$
  141. % #18: test of log exp: ideal results 1.0.
  142. begin for i:=-5:5 do
  143. <<write if i=0 then "0/0" else (log exp float i)/i," "; terpr(i,5)>>
  144. end$
  145. % #19: test of log10 expt(10,i): ideal results 1.0.
  146. begin scalar i; write "small values i:=-5:5"; terpri();
  147. for j:=-5:5 do
  148. <<write if j neq 0 then log10 float expt(10,j)/j else "zero"," ";
  149. terpr(j,5)>>;
  150. write "large i=2**j where j:=0:6"; terpri();
  151. for j:=0:5 do
  152. <<write (log10 float expt(10,2**j))/2**j," "; terpr(j,5)>>;
  153. terpri();
  154. write "noninteger values of i=j/10.0 where j:=1:20";terpri();
  155. for j:=1:20 do
  156. <<i:=j/10.0; write (log10 float expt(10,i))/i," ";
  157. terpr(j,5)>>
  158. end$
  159. % #20: test of properties of expt(x,i)*(expt(x,-i). ideal result 1.0.
  160. begin integer j;
  161. for x:=2:6 do for i:=2:6 do
  162. <<write expt(x,i)*expt(x,-i)," "; terpr(j:=j+1,5)>>
  163. end$
  164. % #21: test of expt(-x,i)/expt(x,i) for fractional i.
  165. begin integer j,k; write "odd numerator. ideal result -1.0"; terpri();
  166. for i:=1:10 do
  167. <<k:=(2*i-1.0)/(8*i+1); write expt(-8,k)/expt(8,k)," ";
  168. terpr(j:=j+1,5)>>;
  169. write "even numerator. ideal result 1.0"; terpri();
  170. for i:=1:10 do
  171. <<k:=(2.0*i)/(8*i+1); write expt(-8,k)/expt(8,k)," ";
  172. terpr(j:=j+1,5)>>
  173. end$
  174. % #22: test of properties of ln or log or logb:
  175. % inverse argument: ideal result -1.0.
  176. begin integer x;
  177. for i:=2:5 do for j:= 2:10 do
  178. <<x:=x+1; write logb(float i,float j)/logb(float i,1.0/j)," ";
  179. terpr(x,5)>>
  180. end$
  181. % #23: test of log(a*b) = log a+log b: ideal result 1.0.
  182. begin integer x;
  183. for i:=1:5 do for j:=i:5 do
  184. <<write log (i*j*0.01)/(log (i*0.1)+log(j*0.1))," ";
  185. terpr(x:=x+1,5)>>
  186. end$
  187. % #24:test of sqrt x*sqrt x/x for x:=5i*(5i/3)**i where i:=1:20
  188. % (test values strictly arbitrary): ideal results 1.0.
  189. begin scalar x,s;
  190. for i:=1:20 do
  191. <<x:= 5.0*i;s:=sqrt(x:=x*(expt(x/3,i))); write s*s/x," ";
  192. terpr(i,5)>>
  193. end$
  194. % #25: test of cbrt x**3/x for x:=5i*(5i/3)**i where i:=-9:10
  195. % (test values strictly arbitrary):ideal results 1.0.
  196. begin scalar x,s;
  197. for i:=-9:10 do
  198. <<x:= 5.0*i; if i neq 0 then s:= cbrt(x:=x*(expt(x/3,i)));
  199. write if i=0 then 1 else s*s*s/x," "; terpr(i,5)>>
  200. end$
  201. %*********************************************************************
  202. %** ===Hyperbolic Function Tests===
  203. %*********************************************************************
  204. % #26: test of sinh x+ cosh x= exp x: ideal results 1.0.
  205. begin scalar x;
  206. for i:=1:10 do
  207. <<x:=ln float i$ write (sinh x+cosh x)/exp x," "$ terpr(i,5)>>
  208. end$
  209. % #27: test of cosh x-sinh x= exp(-x): ideal results 1.0.
  210. begin scalar x;
  211. for i:=1:10 do
  212. <<x:=ln float i$ write(cosh x-sinh x)*exp x," "$ terpr(i,5)>>
  213. end$
  214. % #28: test of (cosh x)**2-(sinh x)**2: ideal results 1.0.
  215. begin scalar x$
  216. for i:=1:10 do
  217. <<x:=ln float i$write(cosh x)**2-(sinh x)**2," "; terpr(i,5)>>
  218. end$
  219. % #29: test of tanh*cosh/sinh: ideal results 1.0.
  220. begin scalar x;
  221. for i:=1:20 do
  222. <<x:=ln(i*0.1);
  223. write if i=10 then 1 else tanh x*cosh x/sinh x," ";
  224. terpr(i,5)>>
  225. end$
  226. % #30: test of tanh*coth: ideal results 1.0.
  227. begin scalar x;
  228. for i:=1:20 do
  229. <<x:=ln(i*0.1); write if i=10 then 1 else tanh x*coth x," ";
  230. terpr(i,5)>>
  231. end$
  232. % #31: test of sech*cosh: ideal results 1.0.
  233. begin scalar x;
  234. for i:=1:20 do
  235. <<x:=ln(i*0.1); write sech x*cosh x," "; terpr(i,5)>>
  236. end$
  237. % #32: test of csch*sinh: ideal results 1.0.
  238. begin scalar x;
  239. for i:=1:20 do
  240. <<x:=ln(i*0.1); write if i=10 then 1 else csch x*sinh x," ";
  241. terpr(i,5)>>
  242. end$
  243. % #33: test of asinh sinh: ideal results 1.0.
  244. begin scalar x; for i:=1:20 do
  245. <<x:=ln(i*0.1); write if i=10 then 1 else asinh sinh x/x," ";
  246. terpr(i,5)>>
  247. end$
  248. % #34: test of acosh cosh: ideal results 1.0. However, acosh x
  249. % loses accuracy as x -> 1 since d/dx cosh x -> 0.
  250. begin scalar x;
  251. for i:=1:20 do
  252. <<x:=ln(1+i*0.05); write acosh cosh x/x," "; terpr(i,5)>>
  253. end$
  254. % #35: test of cosh acosh:ideal results 1.0.
  255. begin scalar x;
  256. for i:=1:50 do
  257. <<x:=1+i/25.0; write (cosh acosh x)/x," "; terpr(i,5)>>
  258. end$
  259. % #36: test of atanh tanh: ideal results 1.0.
  260. begin scalar x;
  261. for i:=1:20 do
  262. <<x:=ln(i*0.1); write if i=10 then 1 else (atanh tanh x)/x," ";
  263. terpr(i,5)>>
  264. end$
  265. % #37: test of acoth coth: ideal results 1.0.
  266. begin scalar x;
  267. for i:=1:20 do
  268. <<x:=ln(i*0.1); write if i=10 then 1 else (acoth coth x)/x," ";
  269. terpr(i,5)>>
  270. end$
  271. % #38: test of asech sech: ideal results 1.0. However, asech x
  272. % loses accuracy as x -> 1 since d/dx sech x -> 0.
  273. begin scalar x;
  274. for i:=1:20 do
  275. <<x:=ln(1+i*0.05); write (asech sech x)/x," "; terpr(i,5)>>
  276. end$
  277. % #39: test of acsch csch: ideal results 1.0.
  278. begin scalar x;
  279. for i:=1:20 do
  280. <<x:=ln(i*0.1); write if i=10 then 1 else (acsch csch x)/x," ";
  281. terpr(i,5)>>
  282. end$
  283. % End of Test.
  284. end;