OSDN Git Service

3c98faf9f40b65ad6641b58e99ba3910b8b8b1e2
[nxt-jsp/lejos_nxj.git] / nxtOSEK / lejos_nxj / src / java / classes / java / lang / Math.java
1 package java.lang;
2
3 /**
4  * Mathematical functions.
5  *
6  * @author <a href="mailto:bbagnall@escape.ca">Brian Bagnall</a>
7  */
8 public final class Math {
9                 
10         // Math constants
11         public static final double E  = 2.718281828459045;
12         public static final double PI = 3.141592653589793;
13    public static final double NaN       = 0.0f / 0.0f;
14    
15    static final float PI2 = 1.570796326794897f;
16         static final double ln10      = 2.30258509299405;
17         static final double ln2       = 0.69314718055995;
18         
19         // Used by log() and exp() methods
20         private static final float LOWER_BOUND = 0.9999999f;
21         private static final float UPPER_BOUND = 1.0f;
22
23         // Used to generate random numbers.
24         private static java.util.Random RAND = new java.util.Random(System.currentTimeMillis());
25
26         //public static boolean isNaN (double d) {
27         //  return d != d;
28         //}
29         
30         private Math() {} // To make sure this class is not instantiated
31
32    // Private because it only works when -1 < x < 1 but it is used by atan2
33    private static double ArcTan(double x)  // Using a Chebyshev-Pade approximation
34    {
35      double x2=x*x;
36      return (0.7162721433f+0.2996857769f*x2)*x/(0.7163164576f+(0.5377299313f+0.3951620469e-1f*x2)*x2);
37    }
38
39
40         /**
41         * Returns the smallest (closest to negative infinity) double value that is not
42         * less than the argument and is equal to a mathematical integer.
43         */
44         public static double ceil(double a) {
45                 return ((a<0)?(int)a:(int)(a+1));       
46         }
47         
48         /**
49         * Returns the largest (closest to positive infinity) double value that is not
50         * greater than the argument and is equal to a mathematical integer.
51         */
52         public static double floor(double a) {  
53                 return ((a<0)?(int)(a-1):(int)a);       
54         }
55         
56         /**
57         * Returns the closest int to the argument.
58         */      
59         public static int round(float a) {      
60                 return (int)floor(a + 0.5f);
61         }
62         
63         /**
64         * Returns the lesser of two integer values.
65         */
66         public static int min(int a, int b) {   
67                 return ((a<b)?a:b);
68         }
69         
70         /**
71         *Returns the lesser of two double values.
72         */
73         public static double min(double a, double b) {  
74                 return ((a<b)?a:b);
75         }
76         
77         /**
78         *Returns the greater of two integer values.
79         */
80         public static int max(int a, int b) {   
81                 return ((a>b)?a:b);
82         }
83         
84         /**
85         *Returns the greater of two double values.
86         */
87         public static double max(double a, double b) {  
88                 return ((a>b)?a:b);
89         }
90         
91         /**
92         * Random number generator.
93         * Returns a double greater than 0.0 and less than 1.0
94         */
95         public static double random()
96   {
97     final int MAX_INT = 2147483647;
98     int n = MAX_INT;
99     
100     // Just to ensure it does not return 1.0
101     while(n == MAX_INT)
102         n = abs (RAND.nextInt());
103         
104     return n * (1.0 / MAX_INT);
105   }
106         
107         /**
108         * Exponential function.  Returns E^x (where E is the base of natural logarithms).
109         * Thanks to David Edwards of England for conceiving the code and Martin E. Nielsen
110         * for modifying it to handle large arguments.
111         * = sum a^n/n!, i.e. 1 + x + x^2/2! + x^3/3!
112         * <P>
113         * Seems to work better for +ve numbers so force argument to be +ve.
114         */      
115         public static double exp(double a)
116         {
117             boolean neg = a < 0 ? true : false;
118             if (a < 0)
119                 a = -a;
120             int fac = 1;
121             double term = a;
122             double sum = 0;
123             double oldsum = 0;
124             double end;
125
126             do {
127                 oldsum = sum;
128                 sum += term;
129     
130                 fac++;
131         
132                 term *= a/fac;
133                 end = sum/oldsum;
134             } while (end < LOWER_BOUND || end > UPPER_BOUND);
135
136             sum += 1.0f;
137             
138             return neg ? 1.0f/sum : sum;
139         }
140         
141         /**
142         * Natural log function.  Returns log(a) to base E
143         * Replaced with an algorithm that does not use exponents and so
144         * works with large arguments.
145         * @see <a href="http://www.geocities.com/zabrodskyvlada/aat/a_contents.html">here</a>
146         */
147         public static double log(double x)
148         {
149                 if (x < 1.0)
150                         return -log(1.0/x);
151                         
152                 double m=0.0;
153                 double p=1.0;
154                 while (p <= x) {
155                         m++;
156                         p=p*2;
157                 }
158                 
159                 m = m - 1;
160                 double z = x/(p/2);
161                 
162                 double zeta = (1.0 - z)/(1.0 + z);
163                 double n=zeta;
164                 double ln=zeta;
165                 double zetasup = zeta * zeta;
166                 
167                 for (int j=1; true; j++)
168                 {
169                         n = n * zetasup;
170                         double newln = ln + n / (2 * j + 1);
171                         double term = ln/newln;
172                         if (term >= LOWER_BOUND && term <= UPPER_BOUND)
173                                 return m * ln2 - 2 * ln;
174                         ln = newln;
175                 }
176         }
177
178         /**
179         * Power function.  This is a slow but accurate method.
180         * Thanks to David Edwards of England for conceiving the code.
181         */
182         public static double pow(double a, double b) {
183                 return exp(b * log(a));
184         }
185         
186         /**
187         * Returns the absolute value of a double value. If the argument is not negative, the argument is
188    * returned. If the argument is negative, the negation of the argument is returned.
189         */
190         public static double abs(double a) {
191                 return ((a<0)?-a:a);
192         }
193
194         /**
195         * Returns the absolute value of an integer value. If the argument is not negative, the argument is
196    * returned. If the argument is negative, the negation of the argument is returned.
197         */
198         public static int abs(int a) {
199                 return ((a<0)?-a:a);
200         }
201         
202   /**
203   * Sine function using a Chebyshev-Pade approximation. Thanks to Paulo Costa for donating the code.
204   */
205   public static double sin(double x)  // Using a Chebyshev-Pade approximation
206   {
207     int n=(int)(x/PI2)+1; // reduce to the 4th and 1st quadrants
208     if(n<1)n=n-1;
209     if ((n&2)==0) x=x-(n&0xFFFFFFFE)*PI2;  // if it from the 2nd or the 3rd quadrants
210     else        x=-(x-(n&0xFFFFFFFE)*PI2);
211
212     double x2=x*x;
213     return (0.9238318854f-0.9595498071e-1f*x2)*x/(0.9238400690f+(0.5797298195e-1f+0.2031791179e-2f*x2)*x2);
214   }
215
216   /**
217   * Cosine function using a Chebyshev-Pade approximation. Thanks to Paulo Costa for donating the code.
218   */
219   public static double cos(double x)
220   {
221     int n=(int)(x/PI2)+1;
222     if(n<1)n=n-1;
223     x=x-(n&0xFFFFFFFE)*PI2;  // reduce to the 4th and 1st quadrants
224
225     double x2=x*x;
226
227     float si=1f;
228     if ((n&2)!=0) si=-1f;  // if it from the 2nd or the 3rd quadrants
229     return si*(0.9457092528f+(-0.4305320537f+0.1914993010e-1f*x2)*x2)/(0.9457093212f+(0.4232119630e-1f+0.9106317690e-3f*x2)*x2);
230   }
231
232    /**
233   * Square root - thanks to Paulo Costa for donating the code.
234   */
235   public static double sqrt(double x)
236   {
237     double root = x, guess=0;
238
239     if(x<0) return NaN;
240
241     // the accuarcy test is percentual
242     for(int i=0; (i<16) && ((guess > x*(1+5e-7f)) || (guess < x*(1-5e-7f))); i++)
243     {
244       root = (root+x/root)*0.5f; // a multiplication is faster than a division
245       guess=root*root;   // cache the square to the test
246     }
247     return root;
248   }
249         
250    /**
251    * Tangent function.
252    */
253    public static double tan(double a) {
254       return sin(a)/cos(a);
255    }
256                 
257   /**
258   * Arc tangent function. Thanks to Paulo Costa for donating the code.
259   */
260   public static double atan(double x)
261   {
262     return atan2(x,1);
263   }
264
265    /**
266   * Arc tangent function valid to the four quadrants
267   * y and x can have any value without sigificant precision loss
268   * atan2(0,0) returns 0. Thanks to Paulo Costa for donating the code.
269   */
270   public static double atan2(double y, double x)
271   {
272     float ax=(float)abs(x);
273     float ay=(float)abs(y);
274
275     if ((ax<1e-7) && (ay<1e-7)) return 0f;
276
277     if (ax>ay)
278     {
279       if (x<0)
280       {
281         if (y>=0) return ArcTan(y/x)+PI;
282         else return ArcTan(y/x)-PI;
283       }
284       else return ArcTan(y/x);
285     }
286     else
287     {
288       if (y<0) return ArcTan(-x/y)-PI/2;
289       else return ArcTan(-x/y)+PI/2;
290     }
291   }
292
293   /**
294    * Arc cosine function.
295    */
296   public static double acos(double a) {
297     if ((a<-1)||(a>1)) {
298       return Double.NaN;
299     }
300     return PI/2 - atan(a/sqrt(1 - a * a));
301   }
302         
303    /**
304    * Arc sine function.
305    */
306    public static double asin(double a) {
307       return atan(a/sqrt(1-a*a));
308    }
309         
310   /**
311    * Converts radians to degrees.
312    */
313    public static double toDegrees(double angrad) {
314       return angrad * (360.0 / (2 * PI));
315    }
316         
317    /**
318     * Converts degrees to radians.
319     */
320    public static double toRadians(double angdeg) {
321       return angdeg * ((2 * PI) / 360.0);
322    }
323 }