Extend library classes
[IRC.git] / Robust / src / ClassLibrary / Math.java
1 public class Math {
2
3   public static double setPI() {
4     double PI = 3.14159265358979323846;
5     return PI;
6   }
7
8   // an alias for setPI()
9   public static double PI() {
10     double PI = 3.14159265358979323846;
11     return PI;
12   }
13
14   public static double fabs(double x) {
15     if (x < 0) {
16       return -x;
17     } else {
18       return x;
19     }
20   }
21
22   public static double abs(double x) {
23     if (x < 0) {
24       return -x;
25     } else {
26       return x;
27     }
28   }
29
30   public static float abs(float a) {
31     if (a<0)
32       return -a;
33     else return a;
34   }
35
36   public static double max(double a, double b) {
37     if(a == b)
38       return a;
39     if(a > b) {
40       return a;
41     } else {
42       return b;
43     }
44   }
45
46   public static int imax(int a, int b) {
47     if(a == b)
48       return a;
49     if(a > b) {
50       return a;
51     } else {
52       return b;
53     }
54   }
55
56   public static int imin(int a, int b) {
57     if(a == b)
58       return a;
59     if(a > b) {
60       return b;
61     } else {
62       return a;
63     }
64   }
65
66   /** sqrt(a^2 + b^2) without under/overflow. **/
67   public static double hypot(double a, double b) {
68     double r;
69     if (fabs(a) > fabs(b)) {
70       r = b/a;
71       r = fabs(a)*sqrt(1+r*r);
72     } else if (b != 0) {
73       r = a/b;
74       r = fabs(b)*sqrt(1+r*r);
75     } else {
76       r = 0.0;
77     }
78     return r;
79   }
80
81   public static double rint(double x) {
82     double y = ceil(x);
83     double d = y - x;
84     if( d == 0.5 ) {
85       if( ((int)y)%2 == 0 ) {
86         return y;
87       } else {
88         return y - 1.0;
89       }
90     } else if( d < 0.5 ) {
91       return y;
92     }
93     return y - 1.0;
94   }
95
96   public static native double sin(double a);
97   public static native double cos(double a);
98   public static native double asin(double a);
99   public static native double acos(double a);
100   public static native double tan(double a);
101   public static native double atan(double a);
102   public static native double atan2(double a, double b);
103   public static native double exp(double a);
104   public static native double sqrt(double a);
105   public static native double log(double a);
106   public static native double pow(double a, double b);
107
108   public static native double ceil(double a);
109   public static native double floor(double a);
110
111   public static native float sinf(float a);
112   public static native float cosf(float a);
113   public static native float expf(float a);
114   public static native float sqrtf(float a);
115   public static native float logf(float a);
116   public static native float powf(float a, float b);
117 }