home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Moscow ML 1.31 / source code / mosml / src / mosmllib / test / math.sml < prev    next >
Encoding:
Text File  |  1996-07-03  |  3.6 KB  |  112 lines  |  [TEXT/R*ch]

  1. (* test/math.sml 
  2.    PS 1995-02-25
  3. *)
  4.  
  5. use "auxil.sml";
  6.  
  7. local
  8.     open Math
  9.     val MAXDOUBLE = 8.98846567431157E307;
  10.     val MINDOUBLE = 4.94065645841246544E~324
  11.     val PI = 3.14159265358979323846;
  12.     val E = 2.7182818284590452354;
  13.  
  14.     val eps = 1E~7;
  15.     fun check1 (opr, a, r) =
  16.     let val res = opr a
  17.     in
  18.         if r = 0.0 andalso abs res <= eps orelse abs(res/r - 1.0) <= eps
  19.         then "OK" else "WRONG"
  20.     end;
  21.     fun check2 (opr, a1, a2, r) =
  22.     let val res = opr(a1, a2)
  23.     in
  24.         if r = 0.0 andalso abs res <= eps orelse abs(res/r - 1.0) <= eps
  25.         then "OK" else "WRONG"
  26.     end;
  27. in
  28.  
  29. val test0a = check(abs(PI - pi) <= eps);
  30. val test0b = check(abs(E - e) <= eps);
  31.  
  32. val test1a = check1(sqrt, 64.0, 8.0);
  33. val test1b = check1(sqrt, 0.0, 0.0);
  34.  
  35. val test2a = check1(sin, 0.0, 0.0);
  36. val test2b = check1(sin, pi/2.0, 1.0);
  37. val test2c = check1(sin, pi, 0.0);
  38. val test2d = check1(sin, 3.0*pi/2.0, ~1.0);
  39.  
  40. val test3a = check1(cos, 0.0, 1.0);
  41. val test3b = check1(cos, pi/2.0, 0.0);
  42. val test3c = check1(cos, pi, ~1.0);
  43. val test3d = check1(cos, 3.0*pi/2.0, 0.0);
  44.  
  45. val test4a = check1(tan, 0.0, 0.0);
  46. val test4b = check1(tan, pi/4.0, 1.0);
  47. val test4c = check1(tan, pi, 0.0);
  48. val test4d = check1(tan, 3.0*pi/4.0, ~1.0);
  49. val test4e = check1(tan, ~pi/4.0, ~1.0);
  50. val test4f = check((abs(tan (pi/2.0))  > 1E8) handle _ => true);
  51. val test4g = check((abs(tan (~pi/2.0)) > 1E8) handle _ => true);
  52.  
  53. val test5a = check1(asin, 0.0, 0.0);
  54. val test5b = check1(asin, 1.0, pi/2.0);
  55. val test5c = check1(asin, ~1.0, ~pi/2.0);
  56. val test5d = (asin 1.1 seq "WRONG") handle Trig => "OK" | _ => "WRONG";
  57. val test5e = (asin ~1.1 seq "WRONG") handle Trig => "OK" | _ => "WRONG";
  58.  
  59. val test6a = check1(acos, 1.0, 0.0);
  60. val test6b = check1(acos, 0.0, pi/2.0);
  61. val test6c = check1(acos, ~1.0, pi);
  62. val test6d = (acos 1.1 seq "WRONG") handle Trig => "OK" | _ => "WRONG";
  63. val test6e = (acos ~1.1 seq "WRONG") handle Trig => "OK" | _ => "WRONG";
  64.  
  65. val test7a = check1(atan, 0.0, 0.0);
  66. val test7b = check1(atan, 1.0, pi/4.0);
  67. val test7c = check1(atan, ~1.0, ~pi/4.0);
  68. val test7d = check1(atan, 1E8, pi/2.0);
  69. val test7e = check1(atan, ~1E8, ~pi/2.0);
  70.  
  71. (* atan2 -- here I am in doubt over the argument order, since the New
  72. Basis document is inconsistent with itself and with atan2 in the C
  73. libraries. *)
  74.  
  75. val test8a = check2(atan2, 0.0, 0.0, 0.0);
  76. val test8b = check2(atan2, 1.0, 0.0, pi/2.0);
  77. val test8c = check2(atan2, ~1.0, 0.0, ~pi/2.0);
  78. val test8d = check2(atan2, 1.0, 1.0, pi/4.0);
  79. val test8e = check2(atan2, ~1.0, 1.0, ~pi/4.0);
  80. val test8f = check2(atan2, ~1.0, ~1.0, ~3.0*pi/4.0);
  81. val test8g = check2(atan2, 1.0, ~1.0, 3.0*pi/4.0);
  82. val test8h = check2(atan2, 1E8, 1.0, pi/2.0);
  83. val test8i = check2(atan2, ~1E8, 1.0, ~pi/2.0);
  84. val test8j = check2(atan2, 1.0, 1E8, 0.0);
  85. val test8k = check2(atan2, 1.0, ~1E8, pi);
  86. val test8l = check2(atan2, ~1.0, ~1E8, ~pi);
  87.  
  88. val test9a = check1(exp, 0.0, 1.0);
  89. val test9b = check1(exp, 1.0, e);
  90. val test9c = check1(exp, ~1.0, 1.0/e);
  91.  
  92. val test10a = check1(ln, 1.0, 0.0);
  93. val test10b = check1(ln, e, 1.0);
  94. val test10c = check1(ln, 1.0/e, ~1.0);
  95. val test10d = (ln 0.0 seq "WRONG") handle Ln => "OK" | _ => "WRONG";
  96. val test11e = (ln ~1.0 seq "WRONG") handle Ln => "OK" | _ => "WRONG";
  97.  
  98. val test12a = check2(pow, 0.0, 0.0, 1.0); (* arbitrary, might be 0.0 *)
  99. val test12b = check2(pow, 7.0, 0.0, 1.0); 
  100. val test12c = check2(pow, 0.0, 7.0, 0.0); 
  101. val test12d = check2(pow, 64.0, 0.5, 8.0); 
  102. val test12e = check2(pow, ~9.0, 2.0, 81.0); 
  103. val test12f = check2(pow, 10.0, ~2.0, 0.01); 
  104. val test12g = check2(pow, ~10.0, ~2.0, 0.01); 
  105.  
  106. val test13a = check1(log10, 1.0, 0.0);
  107. val test13b = check1(log10, 10.0, 1.0);
  108. val test13c = check1(log10, 100.0, 2.0);
  109. val test13d = check1(log10, 0.1, ~1.0);
  110. val test13e = check1(log10, 0.01, ~2.0);
  111. end
  112.