b6f4a13011213e37be08e5c6e52dc17d7b06cb97
[IRC.git] / Robust / src / ClassLibrary / SSJava / Math.java
1 @LATTICE("B<T")
2 @METHODDEFAULT("OUT<IN,THISLOC=IN")
3 public class Math {
4   @LOC("T")
5   static final double PI = 3.14159265358979323846;
6
7   // an alias for setPI()
8   public static double PI() {
9     double PI = 3.14159265358979323846;
10     return PI;
11   }
12
13   public static int abs(int x) {
14     return (x < 0) ? -x : x;
15   }
16
17   public static long abs(long x) {
18     return (x < 0) ? -x : x;
19   }
20
21   public static double abs(double x) {
22     return (x < 0) ? -x : x;
23   }
24
25   public static float abs(float x) {
26     return (x < 0) ? -x : x;
27   }
28
29   public static double max(double a, double b) {
30     return (a > b) ? a : b;
31   }
32
33   public static float max(float a, float b) {
34     return (a > b) ? a : b;
35   }
36
37   public static int max(int a, int b) {
38     return (a > b) ? a : b;
39   }
40
41   public static long max(long a, long b) {
42     return (a > b) ? a : b;
43   }
44
45   @RETURNLOC("IN")
46   public static double min(@LOC("IN") double a, @LOC("IN") double b) {
47     return (a < b) ? a : b;
48   }
49
50   @RETURNLOC("IN")
51   public static float min(@LOC("IN") float a, @LOC("IN") float b) {
52     return (a < b) ? a : b;
53   }
54
55   @RETURNLOC("IN")
56   public static int min(@LOC("IN") int a, @LOC("IN") int b) {
57     return (a < b) ? a : b;
58   }
59
60   @RETURNLOC("IN")
61   public static long min(@LOC("IN") long a, @LOC("IN") long b) {
62     return (a < b) ? a : b;
63   }
64
65   /** sqrt(a^2 + b^2) without under/overflow. **/
66   public static double hypot(double a, double b) {
67     double r;
68     if (abs(a) > abs(b)) {
69       r = b / a;
70       r = abs(a) * sqrt(1 + r * r);
71     } else if (b != 0) {
72       r = a / b;
73       r = abs(b) * sqrt(1 + r * r);
74     } else {
75       r = 0.0;
76     }
77     return r;
78   }
79
80   public static int round(float a) {
81     // this check for NaN, from JLS 15.21.1, saves a method call
82     return (int) floor(a + 0.5f);
83   }
84
85   public static double rint(double x) {
86     double y = ceil(x);
87     double d = y - x;
88     if (d == 0.5) {
89       if (((int) y) % 2 == 0) {
90         return y;
91       } else {
92         return y - 1.0;
93       }
94     } else if (d < 0.5) {
95       return y;
96     }
97     return y - 1.0;
98   }
99
100   public static native double sin(double a);
101
102   public static native double cos(double a);
103
104   public static native double asin(double a);
105
106   public static native double acos(double a);
107
108   public static native double tan(double a);
109
110   public static native double atan(double a);
111
112   public static native double atan2(double a, double b);
113
114   public static native double exp(double a);
115
116   public static native double sqrt(double a);
117
118   public static native double log(double a);
119
120   @RETURNLOC("OUT")
121   public static native double pow(@LOC("IN") double a, @LOC("IN") double b);
122
123   public static native double ceil(double a);
124
125   public static native double floor(double a);
126
127   public static native float sinf(float a);
128
129   public static native float cosf(float a);
130
131   public static native float expf(float a);
132
133   public static native float sqrtf(float a);
134
135   public static native float logf(float a);
136
137   public static native float powf(float a, float b);
138
139   public static native float ceilf(float a);
140
141   public static native float IEEEremainder(float f1, float f2);
142 }