PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/src/xmlvm2csharp/test/org/xmlvm/test/nativeImpl/MathTest.java

https://github.com/tisoft/xmlvm
Java | 259 lines | 222 code | 7 blank | 30 comment | 59 complexity | 9e17c45f2f98b7bfe2d5e9e63a9bf73d MD5 | raw file
Possible License(s): LGPL-2.1
  1. /* Copyright (c) 2002-2011 by XMLVM.org
  2. *
  3. * Project Info: http://www.xmlvm.org
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  13. * License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  18. * USA.
  19. */
  20. package org.xmlvm.test.nativeImpl;
  21. /**
  22. *
  23. * tests native implementation of methods in java.lang.Math
  24. */
  25. public class MathTest {
  26. public static void main(String[] args) {
  27. int numTests = 40;
  28. String[] description = new String[numTests];
  29. boolean[] result = new boolean[numTests];
  30. int i = -1;
  31. long diff;
  32. // acos:
  33. i++;
  34. result[i] = Double.isNaN(Math.acos(Double.NaN));
  35. description[i] = "acos(NaN) == NaN";
  36. i++;
  37. result[i] = Double.isNaN(Math.acos(1.1D));
  38. description[i] = "acos(x) == NaN, for some x>1";
  39. i++;
  40. result[i] = Double.isNaN(Math.acos(-1.1D));
  41. description[i] = "acos(x) == NaN, for some x<-1";
  42. i++;
  43. diff = Double.doubleToLongBits(
  44. Math.cos(Math.acos(3.0D/(Math.sqrt(3.0D)*2.0D)))
  45. ) - Double.doubleToLongBits(
  46. Math.sqrt(3.0D)/2.0D
  47. );
  48. result[i] = (diff == 0L || diff == -1L || diff == 1L);
  49. description[i] = "|cos(acos(x))-x|<= 1ulp, for some |x|<=1";
  50. i++;
  51. diff = Double.doubleToLongBits(
  52. Math.acos(Math.cos(Math.PI/7.0D))
  53. ) - Double.doubleToLongBits(
  54. Math.PI/7.0D
  55. );
  56. result[i] = (diff == 0L || diff == -1L || diff == 1L);
  57. description[i] = "|acos(cos(y))-y|<= 1ulp, for some 0<=y<pi";
  58. i++;
  59. diff = Double.doubleToLongBits(
  60. Math.acos(3.0D/(Math.sqrt(3.0D)*2.0D))
  61. ) - Double.doubleToLongBits(
  62. Math.PI/6.0D
  63. );
  64. result[i] = (diff == 0L || diff == -1L || diff == 1L);
  65. description[i] = "|acos(x)-y|<= 1ulp, for some x,y s.t. cos(y)=x";
  66. i++;
  67. result[i] = Double.compare(
  68. Math.acos(3.0D/(Math.sqrt(3.0D)*2.0D)),
  69. Math.acos(Double.longBitsToDouble(
  70. Double.doubleToLongBits(3.0D/(Math.sqrt(3.0D)*2.0D))+1)
  71. )
  72. )>=0;
  73. description[i] = "acos(x+epsilon)=<acos(x), for some 0<x<1";
  74. i++;
  75. result[i] = Double.compare(
  76. Math.acos(-3.0D/(Math.sqrt(3.0D)*2.0D)),
  77. Math.acos(Double.longBitsToDouble(
  78. // to add epsilon we actually subtract a bit...
  79. Double.doubleToLongBits(-3.0D/(Math.sqrt(3.0D)*2.0D))-1)
  80. )
  81. )>=0;
  82. description[i] = "acos(x+epsilon)=<acos(x), for some -1<x<0";
  83. // asin:
  84. i++;
  85. result[i] = Double.isNaN(Math.asin(Double.NaN));
  86. description[i] = "asin(NaN) == NaN";
  87. i++;
  88. result[i] = Double.isNaN(Math.asin(1.1D));
  89. description[i] = "asin(x) == NaN, for some x>1";
  90. i++;
  91. result[i] = Double.isNaN(Math.asin(-1.1D));
  92. description[i] = "asin(x) == NaN, for some x<1";
  93. i++;
  94. result[i] = 1/Math.asin(+0D) == 1/+0D;
  95. description[i] = "asin(+0D) == +0D";
  96. i++;
  97. result[i] = 1/Math.asin(-0D) == 1/-0D;
  98. description[i] = "asin(-0D) == -0D";
  99. i++;
  100. diff = Double.doubleToLongBits(
  101. Math.sin(Math.asin(3.0D/(Math.sqrt(3.0D)*2.0D)))
  102. ) - Double.doubleToLongBits(
  103. 3.0D/(Math.sqrt(3.0D)*2.0D)
  104. );
  105. result[i] = (diff == 0L || diff == -1L || diff == 1L);
  106. description[i] = "|sin(asin(x))-x|<= 1ulp, for some x";
  107. i++;
  108. diff = Double.doubleToLongBits(
  109. Math.asin(Math.sin(Math.PI/7.0D))
  110. ) - Double.doubleToLongBits(
  111. Math.PI/7.0D
  112. );
  113. result[i] = (diff == 0L || diff == -1L || diff == 1L);
  114. description[i] = "|asin(sin(y))-y|<= 1ulp, for some y";
  115. i++;
  116. diff = Double.doubleToLongBits(
  117. Math.asin(3.0D/(Math.sqrt(3.0D)*2.0D))
  118. ) - Double.doubleToLongBits(
  119. Math.PI/3.0D
  120. );
  121. result[i] = (diff == 0L || diff == -1L || diff == 1L);
  122. description[i] = "|asin(x)-y|<= 1ulp, for some x,y s.t. sin(y)=x"+diff;
  123. i++;
  124. result[i] = Double.compare(
  125. Math.asin(3.0D/(Math.sqrt(3.0D)*2.0D)),
  126. Math.asin(Double.longBitsToDouble(
  127. Double.doubleToLongBits(3.0D/(Math.sqrt(3.0D)*2.0D))+1)
  128. )
  129. )<=0;
  130. description[i] = "asin(x+epsilon)>=asin(x), for some 0<x<1";
  131. i++;
  132. result[i] = Double.compare(
  133. Math.asin(-3.0D/(Math.sqrt(3.0D)*2.0D)),
  134. Math.asin(Double.longBitsToDouble(
  135. Double.doubleToLongBits(-3.0D/(Math.sqrt(3.0D)*2.0D))-1)
  136. )
  137. )<=0;
  138. description[i] = "asin(x+epsilon)>=asin(x), for some -1<x<0";
  139. // atan:
  140. i++;
  141. result[i] = Double.isNaN(Math.atan(Double.NaN));
  142. description[i] = "Math.atan(Double.NaN) == NaN";
  143. i++;
  144. result[i] = 1/Math.atan(+0D) == 1/+0D;
  145. description[i] = "Math.atan(+0D) == +0D";
  146. i++;
  147. result[i] = 1/Math.atan(-0D) == 1/-0D;
  148. description[i] = "Math.atan(-0D) == -0D";
  149. i++;
  150. diff = Double.doubleToLongBits(
  151. Math.tan(Math.atan(1.0D/Math.sqrt(3.0D)))
  152. ) - Double.doubleToLongBits(
  153. 1.0D/Math.sqrt(3.0D)
  154. );
  155. result[i] = (diff == 0L || diff == -1L || diff == 1L);
  156. description[i] = "|tan(atan(x))-x|<= 1ulp, for some x";
  157. i++;
  158. diff = Double.doubleToLongBits(
  159. Math.atan(Math.tan(Math.PI/7.0D))
  160. ) - Double.doubleToLongBits(
  161. Math.PI/7.0D
  162. );
  163. result[i] = (diff == 0L || diff == -1L || diff == 1L);
  164. description[i] = "|atan(tan(y))-y|<= 1ulp, for some 0<y<pi/2";
  165. i++;
  166. diff = Double.doubleToLongBits(
  167. Math.atan(1.0D/Math.sqrt(3.0D))
  168. ) - Double.doubleToLongBits(
  169. Math.PI/6.0D
  170. );
  171. result[i] = (diff == 0L || diff == -1L || diff == 1L);
  172. description[i] = "|atan(x)-y|<= 1ulp, for some x,y s.t. tan(y)=x"+diff;
  173. i++;
  174. result[i] = Double.compare(
  175. Math.atan(3.0D/(Math.sqrt(3.0D)*2.0D)),
  176. Math.atan(Double.longBitsToDouble(
  177. Double.doubleToLongBits(3.0D/(Math.sqrt(3.0D)*2.0D))+1)
  178. )
  179. )<=0;
  180. description[i] = "atan(x+epsilon)>=atan(x), for some 0<x<1";
  181. i++;
  182. result[i] = Double.compare(
  183. Math.atan(-3.0D/(Math.sqrt(3.0D)*2.0D)),
  184. Math.atan(Double.longBitsToDouble(
  185. Double.doubleToLongBits(-3.0D/(Math.sqrt(3.0D)*2.0D))-1)
  186. )
  187. )<=0;
  188. description[i] = "atan(x+epsilon)>=atan(x), for some -1<x<0";
  189. // atan2:
  190. i++;
  191. result[i] = Double.isNaN(Math.atan2(Double.NaN,1.546D));
  192. description[i] = "Math.atan2(NaN,x) == NaN, for some x";
  193. i++;
  194. result[i] = Double.isNaN(Math.atan2(1.546D,Double.NaN));
  195. description[i] = "Math.atan2(y,NaN) == NaN, for some y";
  196. i++;
  197. result[i] = 1/Math.atan2(+0D,1.546D) == 1/+0D;
  198. description[i] = "Math.atan2(+0D,1.546D) == +0D";
  199. i++;
  200. result[i] = 1/Math.atan2(1D,Double.POSITIVE_INFINITY) == 1/+0D;
  201. description[i] = "Math.atan2(1D,Double.POSITIVE_INFINITY) == +0D";
  202. i++;
  203. result[i] = 1/Math.atan2(-0D,1.546D) == 1/-0D;
  204. description[i] = "Math.atan2(-0D,1.546D) == -0D";
  205. i++;
  206. result[i] = 1/Math.atan2(-1D,Double.POSITIVE_INFINITY) == 1/-0D;
  207. description[i] = "Math.atan2(-1D,Double.POSITIVE_INFINITY) == -0D";
  208. // log
  209. i++;
  210. result[i] = Double.isNaN(Math.log(Double.NaN));
  211. description[i] = "log(NaN)==NaN";
  212. i++;
  213. result[i] = Double.isNaN(Math.log(Double.NaN));
  214. description[i] = "log(-1.546)==NaN";
  215. i++;
  216. result[i] = Double.compare(Math.log(+0D), Double.NEGATIVE_INFINITY)==0;
  217. description[i] = "log(+0)==-Infinity";
  218. i++;
  219. result[i] = Double.compare(Math.log(-0D), Double.NEGATIVE_INFINITY)==0;
  220. description[i] = "log(-0)==-Infinity";
  221. i++;
  222. result[i] = Double.compare(Math.log(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY)==0;
  223. description[i] = "log(+Infinity)==+Infinity";
  224. i++;
  225. result[i] = Double.compare(
  226. Math.log(3.0D/(Math.sqrt(3.0D)*2.0D)),
  227. Math.log(Double.longBitsToDouble(
  228. Double.doubleToLongBits(3.0D/(Math.sqrt(3.0D)*2.0D))+1)
  229. )
  230. )<=0;
  231. description[i] = "log(x+epsilon)>=log(x), for some 0<x<1";
  232. i++;
  233. result[i] = Double.compare(
  234. Math.log(1.546),
  235. Math.log(Double.longBitsToDouble(
  236. Double.doubleToLongBits(1.546)+1)
  237. )
  238. )<=0;
  239. description[i] = "log(x+epsilon)>=log(x), for some 1<x<+infinity";
  240. i++;
  241. diff = Double.doubleToLongBits(
  242. Math.log(33.11545195869231)
  243. ) - Double.doubleToLongBits(3.5D);
  244. result[i] = (diff == 0L || diff == -1L || diff == 1L);
  245. description[i] = "|log(x)-y|<= 1ulp, for some x s.t. y=e^x";
  246. // print results
  247. for (int j=0; j<numTests; j++) {
  248. System.out.println(j + ") "
  249. + (result[j] ? "passed" : "failed") + ": "+ description[j]);
  250. }
  251. }
  252. }