little bug fix
[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 int abs(int x) {
15     if (x < 0) {
16       return -x;
17     } else {
18       return x;
19     }
20   }
21
22   public static double fabs(double x) {
23     if (x < 0) {
24       return -x;
25     } else {
26       return x;
27     }
28   }
29
30   public static double abs(double x) {
31     if (x < 0) {
32       return -x;
33     } else {
34       return x;
35     }
36   }
37
38   public static float abs(float a) {
39     if (a<0)
40       return -a;
41     else return a;
42   }
43
44   public static double max(double a, double b) {
45     if(a == b)
46       return a;
47     if(a > b) {
48       return a;
49     } else {
50       return b;
51     }
52   }
53
54   public static int max(int a, int b) {
55     if(a == b)
56       return a;
57     if(a > b) {
58       return a;
59     } else {
60       return b;
61     }
62   }
63
64   public static int imax(int a, int b) {
65     if(a == b)
66       return a;
67     if(a > b) {
68       return a;
69     } else {
70       return b;
71     }
72   }
73
74   public static int imin(int a, int b) {
75     if(a == b)
76       return a;
77     if(a > b) {
78       return b;
79     } else {
80       return a;
81     }
82   }
83
84   /** sqrt(a^2 + b^2) without under/overflow. **/
85   public static double hypot(double a, double b) {
86     double r;
87     if (fabs(a) > fabs(b)) {
88       r = b/a;
89       r = fabs(a)*sqrt(1+r*r);
90     } else if (b != 0) {
91       r = a/b;
92       r = fabs(b)*sqrt(1+r*r);
93     } else {
94       r = 0.0;
95     }
96     return r;
97   }
98
99   public static double rint(double x) {
100     double y = ceil(x);
101     double d = y - x;
102     if( d == 0.5 ) {
103       if( ((int)y)%2 == 0 ) {
104         return y;
105       } else {
106         return y - 1.0;
107       }
108     } else if( d < 0.5 ) {
109       return y;
110     }
111     return y - 1.0;
112   }
113
114   public static native double sin(double a);
115   public static native double cos(double a);
116   public static native double asin(double a);
117   public static native double acos(double a);
118   public static native double tan(double a);
119   public static native double atan(double a);
120   public static native double atan2(double a, double b);
121   public static native double exp(double a);
122   public static native double sqrt(double a);
123   public static native double log(double a);
124   public static native double pow(double a, double b);
125
126   public static native double ceil(double a);
127   public static native double floor(double a);
128
129   public static native float sinf(float a);
130   public static native float cosf(float a);
131   public static native float expf(float a);
132   public static native float sqrtf(float a);
133   public static native float logf(float a);
134   public static native float powf(float a, float b);
135   public static native float ceilf(float a);
136 }