PageRenderTime 21ms CodeModel.GetById 2ms app.highlight 15ms RepoModel.GetById 2ms 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
  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 */
 20package org.xmlvm.test.nativeImpl;
 21
 22/**
 23 *
 24 * tests native implementation of methods in java.lang.Math
 25 */
 26public class MathTest {
 27    public static void main(String[] args) {
 28        int numTests = 40;
 29        String[] description = new String[numTests];
 30        boolean[] result = new boolean[numTests];
 31        int i = -1;
 32        long diff;
 33        // acos:
 34        i++;
 35        result[i] = Double.isNaN(Math.acos(Double.NaN));
 36        description[i] = "acos(NaN) == NaN";
 37        i++;
 38        result[i] = Double.isNaN(Math.acos(1.1D));
 39        description[i] = "acos(x) == NaN, for some x>1";
 40        i++;
 41        result[i] = Double.isNaN(Math.acos(-1.1D));
 42        description[i] = "acos(x) == NaN, for some x<-1";
 43        i++;
 44        diff = Double.doubleToLongBits(
 45                   Math.cos(Math.acos(3.0D/(Math.sqrt(3.0D)*2.0D)))
 46               ) - Double.doubleToLongBits(
 47                   Math.sqrt(3.0D)/2.0D
 48               );
 49        result[i] = (diff == 0L || diff == -1L || diff == 1L);
 50        description[i] = "|cos(acos(x))-x|<= 1ulp, for some |x|<=1";
 51        i++;
 52        diff = Double.doubleToLongBits(
 53                   Math.acos(Math.cos(Math.PI/7.0D))
 54               ) - Double.doubleToLongBits(
 55                   Math.PI/7.0D
 56               );
 57        result[i] = (diff == 0L || diff == -1L || diff == 1L);
 58        description[i] = "|acos(cos(y))-y|<= 1ulp, for some 0<=y<pi";
 59        i++;
 60        diff = Double.doubleToLongBits(
 61                   Math.acos(3.0D/(Math.sqrt(3.0D)*2.0D))
 62               ) - Double.doubleToLongBits(
 63                   Math.PI/6.0D
 64               );
 65        result[i] = (diff == 0L || diff == -1L || diff == 1L);
 66        description[i] = "|acos(x)-y|<= 1ulp, for some x,y s.t. cos(y)=x";
 67        i++;
 68        result[i] = Double.compare(
 69                        Math.acos(3.0D/(Math.sqrt(3.0D)*2.0D)),
 70                        Math.acos(Double.longBitsToDouble(
 71                            Double.doubleToLongBits(3.0D/(Math.sqrt(3.0D)*2.0D))+1)
 72                        )
 73                    )>=0;
 74        description[i] = "acos(x+epsilon)=<acos(x), for some 0<x<1";
 75        i++;
 76        result[i] = Double.compare(
 77                        Math.acos(-3.0D/(Math.sqrt(3.0D)*2.0D)),
 78                        Math.acos(Double.longBitsToDouble(
 79                            // to add epsilon we actually subtract a bit...
 80                            Double.doubleToLongBits(-3.0D/(Math.sqrt(3.0D)*2.0D))-1)
 81                        )
 82                    )>=0;
 83        description[i] = "acos(x+epsilon)=<acos(x), for some -1<x<0";
 84        // asin:
 85        i++;
 86        result[i] = Double.isNaN(Math.asin(Double.NaN));
 87        description[i] = "asin(NaN) == NaN";
 88        i++;
 89        result[i] = Double.isNaN(Math.asin(1.1D));
 90        description[i] = "asin(x) == NaN, for some x>1";
 91        i++;
 92        result[i] = Double.isNaN(Math.asin(-1.1D));
 93        description[i] = "asin(x) == NaN, for some x<1";
 94        i++;
 95        result[i] = 1/Math.asin(+0D) == 1/+0D;
 96        description[i] = "asin(+0D) == +0D";
 97        i++;
 98        result[i] = 1/Math.asin(-0D) == 1/-0D;
 99        description[i] = "asin(-0D) == -0D";
100        i++;
101        diff = Double.doubleToLongBits(
102                   Math.sin(Math.asin(3.0D/(Math.sqrt(3.0D)*2.0D)))
103               ) - Double.doubleToLongBits(
104                   3.0D/(Math.sqrt(3.0D)*2.0D)
105               );
106        result[i] = (diff == 0L || diff == -1L || diff == 1L);
107        description[i] = "|sin(asin(x))-x|<= 1ulp, for some x";
108        i++;
109        diff = Double.doubleToLongBits(
110                   Math.asin(Math.sin(Math.PI/7.0D))
111               ) - Double.doubleToLongBits(
112                   Math.PI/7.0D
113               );
114        result[i] = (diff == 0L || diff == -1L || diff == 1L);
115        description[i] = "|asin(sin(y))-y|<= 1ulp, for some y";
116        i++;
117        diff = Double.doubleToLongBits(
118                   Math.asin(3.0D/(Math.sqrt(3.0D)*2.0D))
119               ) - Double.doubleToLongBits(
120                   Math.PI/3.0D
121               );
122        result[i] = (diff == 0L || diff == -1L || diff == 1L);
123        description[i] = "|asin(x)-y|<= 1ulp, for some x,y s.t. sin(y)=x"+diff;
124        i++;
125        result[i] = Double.compare(
126                        Math.asin(3.0D/(Math.sqrt(3.0D)*2.0D)),
127                        Math.asin(Double.longBitsToDouble(
128                            Double.doubleToLongBits(3.0D/(Math.sqrt(3.0D)*2.0D))+1)
129                        )
130                    )<=0;
131        description[i] = "asin(x+epsilon)>=asin(x), for some 0<x<1";
132        i++;
133        result[i] = Double.compare(
134                        Math.asin(-3.0D/(Math.sqrt(3.0D)*2.0D)),
135                        Math.asin(Double.longBitsToDouble(
136                            Double.doubleToLongBits(-3.0D/(Math.sqrt(3.0D)*2.0D))-1)
137                        )
138                    )<=0;
139        description[i] = "asin(x+epsilon)>=asin(x), for some -1<x<0";
140        // atan:
141        i++;
142        result[i] = Double.isNaN(Math.atan(Double.NaN));
143        description[i] = "Math.atan(Double.NaN) == NaN";
144        i++;
145        result[i] = 1/Math.atan(+0D) == 1/+0D;
146        description[i] = "Math.atan(+0D) == +0D";
147        i++;
148        result[i] = 1/Math.atan(-0D) == 1/-0D;
149        description[i] = "Math.atan(-0D) == -0D";
150        i++;
151        diff = Double.doubleToLongBits(
152                   Math.tan(Math.atan(1.0D/Math.sqrt(3.0D)))
153               ) - Double.doubleToLongBits(
154                   1.0D/Math.sqrt(3.0D)
155               );
156        result[i] = (diff == 0L || diff == -1L || diff == 1L);
157        description[i] = "|tan(atan(x))-x|<= 1ulp, for some x";
158        i++;
159        diff = Double.doubleToLongBits(
160                   Math.atan(Math.tan(Math.PI/7.0D))
161               ) - Double.doubleToLongBits(
162                   Math.PI/7.0D
163               );
164        result[i] = (diff == 0L || diff == -1L || diff == 1L);
165        description[i] = "|atan(tan(y))-y|<= 1ulp, for some 0<y<pi/2";
166        i++;
167        diff = Double.doubleToLongBits(
168                   Math.atan(1.0D/Math.sqrt(3.0D))
169               ) - Double.doubleToLongBits(
170                   Math.PI/6.0D
171               );
172        result[i] = (diff == 0L || diff == -1L || diff == 1L);
173        description[i] = "|atan(x)-y|<= 1ulp, for some x,y s.t. tan(y)=x"+diff;
174        i++;
175        result[i] = Double.compare(
176                        Math.atan(3.0D/(Math.sqrt(3.0D)*2.0D)),
177                        Math.atan(Double.longBitsToDouble(
178                            Double.doubleToLongBits(3.0D/(Math.sqrt(3.0D)*2.0D))+1)
179                        )
180                    )<=0;
181        description[i] = "atan(x+epsilon)>=atan(x), for some 0<x<1";
182        i++;
183        result[i] = Double.compare(
184                        Math.atan(-3.0D/(Math.sqrt(3.0D)*2.0D)),
185                        Math.atan(Double.longBitsToDouble(
186                            Double.doubleToLongBits(-3.0D/(Math.sqrt(3.0D)*2.0D))-1)
187                        )
188                    )<=0;
189        description[i] = "atan(x+epsilon)>=atan(x), for some -1<x<0";
190        // atan2:
191        i++;
192        result[i] = Double.isNaN(Math.atan2(Double.NaN,1.546D));
193        description[i] = "Math.atan2(NaN,x) == NaN, for some x";
194        i++;
195        result[i] = Double.isNaN(Math.atan2(1.546D,Double.NaN));
196        description[i] = "Math.atan2(y,NaN) == NaN, for some y";
197        i++;
198        result[i] = 1/Math.atan2(+0D,1.546D) == 1/+0D;
199        description[i] = "Math.atan2(+0D,1.546D) == +0D";
200        i++;
201        result[i] = 1/Math.atan2(1D,Double.POSITIVE_INFINITY) == 1/+0D;
202        description[i] = "Math.atan2(1D,Double.POSITIVE_INFINITY) == +0D";
203        i++;
204        result[i] = 1/Math.atan2(-0D,1.546D) == 1/-0D;
205        description[i] = "Math.atan2(-0D,1.546D) == -0D";
206        i++;
207        result[i] = 1/Math.atan2(-1D,Double.POSITIVE_INFINITY) == 1/-0D;
208        description[i] = "Math.atan2(-1D,Double.POSITIVE_INFINITY) == -0D";
209
210        // log
211        i++;
212        result[i] = Double.isNaN(Math.log(Double.NaN));
213        description[i] = "log(NaN)==NaN";
214        i++;
215        result[i] = Double.isNaN(Math.log(Double.NaN));
216        description[i] = "log(-1.546)==NaN";
217        i++;
218        result[i] = Double.compare(Math.log(+0D), Double.NEGATIVE_INFINITY)==0;
219        description[i] = "log(+0)==-Infinity";
220        i++;
221        result[i] = Double.compare(Math.log(-0D), Double.NEGATIVE_INFINITY)==0;
222        description[i] = "log(-0)==-Infinity";
223        i++;
224        result[i] = Double.compare(Math.log(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY)==0;
225        description[i] = "log(+Infinity)==+Infinity";
226        i++;
227        result[i] = Double.compare(
228                        Math.log(3.0D/(Math.sqrt(3.0D)*2.0D)),
229                        Math.log(Double.longBitsToDouble(
230                            Double.doubleToLongBits(3.0D/(Math.sqrt(3.0D)*2.0D))+1)
231                        )
232                    )<=0;
233        description[i] = "log(x+epsilon)>=log(x), for some 0<x<1";
234        i++;
235        result[i] = Double.compare(
236                        Math.log(1.546),
237                        Math.log(Double.longBitsToDouble(
238                            Double.doubleToLongBits(1.546)+1)
239                        )
240                    )<=0;
241        description[i] = "log(x+epsilon)>=log(x), for some 1<x<+infinity";
242        i++;
243        diff = Double.doubleToLongBits(
244                   Math.log(33.11545195869231)
245               ) - Double.doubleToLongBits(3.5D);
246        result[i] = (diff == 0L || diff == -1L || diff == 1L);
247        description[i] = "|log(x)-y|<= 1ulp, for some x s.t. y=e^x";
248
249
250
251        // print results
252        for (int j=0; j<numTests; j++) {
253            System.out.println(j + ") "
254                    + (result[j] ? "passed" : "failed") + ": "+ description[j]);
255        }
256
257    }
258
259}