001: /*
002: * Copyright 2006 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package java.lang;
017:
018: /**
019: * Math utility methods and constants.
020: */
021: public final class Math {
022:
023: public static final double E = 2.7182818284590452354;
024: public static final double PI = 3.14159265358979323846;
025:
026: private static final double PI_OVER_180 = PI / 180.0;
027: private static final double PI_UNDER_180 = 180.0 / PI;
028:
029: public static double abs(double x) {
030: return x < 0 ? -x : x;
031: }
032:
033: public static float abs(float x) {
034: return x < 0 ? -x : x;
035: }
036:
037: public static int abs(int x) {
038: return x < 0 ? -x : x;
039: }
040:
041: public static long abs(long x) {
042: return x < 0 ? -x : x;
043: }
044:
045: public static native double acos(double x) /*-{
046: return Math.acos(x);
047: }-*/;
048:
049: public static native double asin(double x) /*-{
050: return Math.asin(x);
051: }-*/;
052:
053: public static native double atan(double x) /*-{
054: return Math.atan(x);
055: }-*/;
056:
057: public static native double atan2(double y, double x) /*-{
058: return Math.atan2(y,x);
059: }-*/;
060:
061: /* NYI: Java 1.5 includes this, but JS doesn't give us the ingredients.: Java 1.5 includes this, but JS doesn't give us the ingredients.
062: public static double cbrt (double x) {
063: return 0;
064: }; */
065:
066: public static native double ceil(double x) /*-{
067: return Math.ceil(x);
068: }-*/;
069:
070: public static double copySign(double magnitude, double sign) {
071: if (sign < 0) {
072: return (magnitude < 0) ? magnitude : -magnitude;
073: } else {
074: return (magnitude > 0) ? magnitude : -magnitude;
075: }
076: }
077:
078: public static float copySign(float magnitude, float sign) {
079: return (float) (copySign((double) magnitude, (double) sign));
080: }
081:
082: public static native double cos(double x) /*-{
083: return Math.cos(x);
084: }-*/;
085:
086: public static native double cosh(double x) /*-{
087: return (Math.exp(x) + Math.exp(-x)) / 2.0;
088: }-*/;
089:
090: public static native double exp(double x) /*-{
091: return Math.exp(x);
092: }-*/;
093:
094: public static double expm1(double d) {
095: if (d == 0.0 || Double.isNaN(d)) {
096: return d; // "a zero with same sign as argument", arg is zero, so...
097: } else if (!Double.isInfinite(d)) {
098: if (d < 0.0d) {
099: return -1.0d;
100: } else {
101: return Double.POSITIVE_INFINITY;
102: }
103: }
104: return exp(d) + 1.0d;
105: }
106:
107: public static native double floor(double x) /*-{
108: return Math.floor(x);
109: }-*/;
110:
111: /* NYI: Java 1.5 includes this, but JS doesn't give us the ingredients.
112: public static int getExponent (double d) {
113: }
114:
115: public static int getExponent (float f) {
116: }
117: */
118:
119: public static double hypot(double x, double y) {
120: return sqrt(x * x + y * y);
121: }
122:
123: /* NYI: Java 1.5 includes this, but JS doesn't give us the ingredients.
124: public static double IEEEremainder(double f1, double f2) {
125: } */
126:
127: public static native double log(double x) /*-{
128: return Math.log(x);
129: }-*/;
130:
131: public static double log10(double x) {
132: return Math.log(x) / Math.log(10);
133: }
134:
135: public static double log1p(double x) {
136: return Math.log(x + 1.0d);
137: };
138:
139: public static double max(double x, double y) {
140: return x > y ? x : y;
141: }
142:
143: public static float max(float x, float y) {
144: return x > y ? x : y;
145: }
146:
147: public static int max(int x, int y) {
148: return x > y ? x : y;
149: }
150:
151: public static long max(long x, long y) {
152: return x > y ? x : y;
153: }
154:
155: public static double min(double x, double y) {
156: return x < y ? x : y;
157: }
158:
159: public static float min(float x, float y) {
160: return x < y ? x : y;
161: }
162:
163: public static int min(int x, int y) {
164: return x < y ? x : y;
165: }
166:
167: public static long min(long x, long y) {
168: return x < y ? x : y;
169: }
170:
171: /* NYI: Java 1.5 includes this, but JS doesn't give us the ingredients.
172: public static double nextAfter(double start, double direction) {
173: }
174: public static float nextAfter(float start, float direction) {
175: }
176: public static double nextUp(double start) {
177: return nextAfter(start, 1.0d);
178: }
179: public static float nextUp(float start) {
180: return nextAfter(start,1.0f);
181: }
182: */
183:
184: public static native double pow(double x, double exp) /*-{
185: return Math.pow(x, exp);
186: }-*/;
187:
188: public static native double random() /*-{
189: return Math.random();
190: }-*/;
191:
192: public static double rint(double d) {
193: if (Double.isNaN(d)) {
194: return d;
195: } else if (Double.isInfinite(d)) {
196: return d;
197: } else if (d == 0.0d) {
198: return d;
199: } else {
200: return (double) (round(d));
201: }
202: };
203:
204: public static native long round(double x) /*-{
205: return Math.round(x);
206: }-*/;
207:
208: public static native int round(float x) /*-{
209: return Math.round(x);
210: }-*/;
211:
212: public static double scalb(double d, int scaleFactor) {
213: if (scaleFactor > 0) {
214: return d * (1 << scaleFactor);
215: } else if (scaleFactor == 0) {
216: return d;
217: } else {
218: return d * 1.0d / (1 << -scaleFactor);
219: }
220: }
221:
222: public static float scalb(float f, int scaleFactor) {
223: if (scaleFactor > 0) {
224: return f * (1 << scaleFactor);
225: } else if (scaleFactor == 0) {
226: return f;
227: } else {
228: return f * 1.0f / (1 << -scaleFactor);
229: }
230: }
231:
232: public static double signum(double d) {
233: if (d > 0.0d) {
234: return 1.0d;
235: } else if (d < 0.0d) {
236: return -1.0d;
237: } else {
238: return 0.0d;
239: }
240: }
241:
242: public static float signum(float f) {
243: if (f > 0.0f) {
244: return 1.0f;
245: } else if (f < 0.0f) {
246: return -1.0f;
247: } else {
248: return 0.0f;
249: }
250: }
251:
252: public static native double sin(double x) /*-{
253: return Math.sin(x);
254: }-*/;
255:
256: public static native double sinh(double x) /*-{
257: return Math.sinh(x);
258: }-*/;
259:
260: public static native double sqrt(double x) /*-{
261: return Math.sqrt(x);
262: }-*/;
263:
264: public static native double tan(double x) /*-{
265: return Math.tan(x);
266: }-*/;
267:
268: public static native double tanh(double x) /*-{
269: return Math.tanh(x);
270: }-*/;
271:
272: public static double toDegrees(double x) {
273: return x * PI_UNDER_180;
274: }
275:
276: public static double toRadians(double x) {
277: return x * PI_OVER_180;
278: }
279:
280: /* NYI: Java 1.5 includes this, but JS doesn't give us the ingredients.
281: public static double ulp (double x) {
282: };
283: public static float ulp (float x) {
284: };
285: */
286: }
|