MATH.TST 11 KB

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