001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package java.lang;
028:
029: /**
030: * The class <code>Math</code> contains methods for performing basic
031: * numeric operations.
032: *
033: * @version 12/17/01 (CLDC 1.1)
034: * @since JDK1.0, CLDC 1.0
035: */
036:
037: public final strictfp class Math {
038:
039: /**
040: * Don't let anyone instantiate this class.
041: */
042: private Math() {
043: }
044:
045: /**
046: * The <code>double</code> value that is closer than any other to
047: * <code>e</code>, the base of the natural logarithms.
048: * @since CLDC 1.1
049: */
050: public static final double E = 2.7182818284590452354;
051:
052: /**
053: * The <code>double</code> value that is closer than any other to
054: * <i>pi</i>, the ratio of the circumference of a circle to its diameter.
055: * @since CLDC 1.1
056: */
057: public static final double PI = 3.14159265358979323846;
058:
059: /**
060: * Returns the trigonometric sine of an angle. Special cases:
061: * <ul><li>If the argument is NaN or an infinity, then the
062: * result is NaN.
063: * <li>If the argument is positive zero, then the result is
064: * positive zero; if the argument is negative zero, then the
065: * result is negative zero.</ul>
066: *
067: * @param a an angle, in radians.
068: * @return the sine of the argument.
069: * @since CLDC 1.1
070: */
071: public static native double sin(double a);
072:
073: /**
074: * Returns the trigonometric cosine of an angle. Special case:
075: * <ul><li>If the argument is NaN or an infinity, then the
076: * result is NaN.</ul>
077: *
078: * @param a an angle, in radians.
079: * @return the cosine of the argument.
080: * @since CLDC 1.1
081: */
082: public static native double cos(double a);
083:
084: /**
085: * Returns the trigonometric tangent of an angle. Special cases:
086: * <ul><li>If the argument is NaN or an infinity, then the result
087: * is NaN.
088: * <li>If the argument is positive zero, then the result is
089: * positive zero; if the argument is negative zero, then the
090: * result is negative zero</ul>
091: *
092: * @param a an angle, in radians.
093: * @return the tangent of the argument.
094: * @since CLDC 1.1
095: */
096: public static native double tan(double a);
097:
098: /**
099: * Converts an angle measured in degrees to the equivalent angle
100: * measured in radians.
101: *
102: * @param angdeg an angle, in degrees
103: * @return the measurement of the angle <code>angdeg</code>
104: * in radians.
105: * @since CLDC 1.1
106: */
107: public static double toRadians(double angdeg) {
108: return angdeg / 180.0 * PI;
109: }
110:
111: /**
112: * Converts an angle measured in radians to the equivalent angle
113: * measured in degrees.
114: *
115: * @param angrad an angle, in radians
116: * @return the measurement of the angle <code>angrad</code>
117: * in degrees.
118: * @since CLDC 1.1
119: */
120: public static double toDegrees(double angrad) {
121: return angrad * 180.0 / PI;
122: }
123:
124: /**
125: * Returns the correctly rounded positive square root of a
126: * <code>double</code> value.
127: * Special cases:
128: * <ul><li>If the argument is NaN or less than zero, then the result
129: * is NaN.
130: * <li>If the argument is positive infinity, then the result is positive
131: * infinity.
132: * <li>If the argument is positive zero or negative zero, then the
133: * result is the same as the argument.</ul>
134: *
135: * @param a a <code>double</code> value.
136: * @return the positive square root of <code>a</code>.
137: * If the argument is NaN or less than zero, the result is NaN.
138: * @since CLDC 1.1
139: */
140: public static native double sqrt(double a);
141:
142: /**
143: * Returns the smallest (closest to negative infinity)
144: * <code>double</code> value that is not less than the argument and is
145: * equal to a mathematical integer. Special cases:
146: * <ul><li>If the argument value is already equal to a mathematical
147: * integer, then the result is the same as the argument.
148: * <li>If the argument is NaN or an infinity or positive zero or negative
149: * zero, then the result is the same as the argument.
150: * <li>If the argument value is less than zero but greater than -1.0,
151: * then the result is negative zero.</ul>
152: * Note that the value of <code>Math.ceil(x)</code> is exactly the
153: * value of <code>-Math.floor(-x)</code>.
154: *
155: * @param a a <code>double</code> value.
156: * <!--@return the value ⌈ <code>a</code> ⌉.-->
157: * @return the smallest (closest to negative infinity)
158: * <code>double</code> value that is not less than the argument
159: * and is equal to a mathematical integer.
160: * @since CLDC 1.1
161: */
162: public static native double ceil(double a);
163:
164: /**
165: * Returns the largest (closest to positive infinity)
166: * <code>double</code> value that is not greater than the argument and
167: * is equal to a mathematical integer. Special cases:
168: * <ul><li>If the argument value is already equal to a mathematical
169: * integer, then the result is the same as the argument.
170: * <li>If the argument is NaN or an infinity or positive zero or
171: * negative zero, then the result is the same as the argument.</ul>
172: *
173: * @param a a <code>double</code> value.
174: * <!--@return the value ⌊ <code>a</code> ⌋.-->
175: * @return the largest (closest to positive infinity)
176: * <code>double</code> value that is not greater than the argument
177: * and is equal to a mathematical integer.
178: * @since CLDC 1.1
179: */
180: public static native double floor(double a);
181:
182: /**
183: * Returns the absolute value of an <code>int</code> value.
184: * If the argument is not negative, the argument is returned.
185: * If the argument is negative, the negation of the argument is returned.
186: * <p>
187: * Note that if the argument is equal to the value of
188: * <code>Integer.MIN_VALUE</code>, the most negative representable
189: * <code>int</code> value, the result is that same value, which is
190: * negative.
191: *
192: * @param a an <code>int</code> value.
193: * @return the absolute value of the argument.
194: * @see java.lang.Integer#MIN_VALUE
195: */
196: public static int abs(int a) {
197: return (a < 0) ? -a : a;
198: }
199:
200: /**
201: * Returns the absolute value of a <code>long</code> value.
202: * If the argument is not negative, the argument is returned.
203: * If the argument is negative, the negation of the argument is returned.
204: * <p>
205: * Note that if the argument is equal to the value of
206: * <code>Long.MIN_VALUE</code>, the most negative representable
207: * <code>long</code> value, the result is that same value, which is
208: * negative.
209: *
210: * @param a a <code>long</code> value.
211: * @return the absolute value of the argument.
212: * @see java.lang.Long#MIN_VALUE
213: */
214: public static long abs(long a) {
215: return (a < 0) ? -a : a;
216: }
217:
218: /**
219: * Returns the absolute value of a <code>float</code> value.
220: * If the argument is not negative, the argument is returned.
221: * If the argument is negative, the negation of the argument is returned.
222: * Special cases:
223: * <ul><li>If the argument is positive zero or negative zero, the
224: * result is positive zero.
225: * <li>If the argument is infinite, the result is positive infinity.
226: * <li>If the argument is NaN, the result is NaN.</ul>
227: * In other words, the result is equal to the value of the expression:
228: * <p><pre>Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))</pre>
229: *
230: * @param a a <code>float</code> value.
231: * @return the absolute value of the argument.
232: * @since CLDC 1.1
233: */
234: public static float abs(float a) {
235: return (a <= 0.0F) ? 0.0F - a : a;
236: }
237:
238: /**
239: * Returns the absolute value of a <code>double</code> value.
240: * If the argument is not negative, the argument is returned.
241: * If the argument is negative, the negation of the argument is returned.
242: * Special cases:
243: * <ul><li>If the argument is positive zero or negative zero, the result
244: * is positive zero.
245: * <li>If the argument is infinite, the result is positive infinity.
246: * <li>If the argument is NaN, the result is NaN.</ul>
247: * In other words, the result is equal to the value of the expression:
248: * <p><pre>Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1)</pre>
249: *
250: * @param a a <code>double</code> value.
251: * @return the absolute value of the argument.
252: * @since CLDC 1.1
253: */
254: public static double abs(double a) {
255: return (a <= 0.0D) ? 0.0D - a : a;
256: }
257:
258: /**
259: * Returns the greater of two <code>int</code> values. That is, the
260: * result is the argument closer to the value of
261: * <code>Integer.MAX_VALUE</code>. If the arguments have the same value,
262: * the result is that same value.
263: *
264: * @param a an <code>int</code> value.
265: * @param b an <code>int</code> value.
266: * @return the larger of <code>a</code> and <code>b</code>.
267: * @see java.lang.Long#MAX_VALUE
268: */
269: public static int max(int a, int b) {
270: return (a >= b) ? a : b;
271: }
272:
273: /**
274: * Returns the greater of two <code>long</code> values. That is, the
275: * result is the argument closer to the value of
276: * <code>Long.MAX_VALUE</code>. If the arguments have the same value,
277: * the result is that same value.
278: *
279: * @param a a <code>long</code> value.
280: * @param b a <code>long</code> value.
281: * @return the larger of <code>a</code> and <code>b</code>.
282: * @see java.lang.Long#MAX_VALUE
283: */
284: public static long max(long a, long b) {
285: return (a >= b) ? a : b;
286: }
287:
288: private static long negativeZeroFloatBits = Float
289: .floatToIntBits(-0.0f);
290: private static long negativeZeroDoubleBits = Double
291: .doubleToLongBits(-0.0d);
292:
293: /**
294: * Returns the greater of two <code>float</code> values. That is, the
295: * result is the argument closer to positive infinity. If the
296: * arguments have the same value, the result is that same value. If
297: * either value is <code>NaN</code>, then the result is <code>NaN</code>.
298: * Unlike the the numerical comparison operators, this method considers
299: * negative zero to be strictly smaller than positive zero. If one
300: * argument is positive zero and the other negative zero, the result
301: * is positive zero.
302: *
303: * @param a a <code>float</code> value.
304: * @param b a <code>float</code> value.
305: * @return the larger of <code>a</code> and <code>b</code>.
306: */
307: public static float max(float a, float b) {
308: if (a != a)
309: return a; // a is NaN
310: if ((a == 0.0f) && (b == 0.0f)
311: && (Float.floatToIntBits(a) == negativeZeroFloatBits)) {
312: return b;
313: }
314: return (a >= b) ? a : b;
315: }
316:
317: /**
318: * Returns the greater of two <code>double</code> values. That is, the
319: * result is the argument closer to positive infinity. If the
320: * arguments have the same value, the result is that same value. If
321: * either value is <code>NaN</code>, then the result is <code>NaN</code>.
322: * Unlike the the numerical comparison operators, this method considers
323: * negative zero to be strictly smaller than positive zero. If one
324: * argument is positive zero and the other negative zero, the result
325: * is positive zero.
326: *
327: * @param a a <code>double</code> value.
328: * @param b a <code>double</code> value.
329: * @return the larger of <code>a</code> and <code>b</code>.
330: */
331: public static double max(double a, double b) {
332: if (a != a)
333: return a; // a is NaN
334: if ((a == 0.0d)
335: && (b == 0.0d)
336: && (Double.doubleToLongBits(a) == negativeZeroDoubleBits)) {
337: return b;
338: }
339: return (a >= b) ? a : b;
340: }
341:
342: /**
343: * Returns the smaller of two <code>int</code> values. That is, the
344: * result the argument closer to the value of <code>Integer.MIN_VALUE</code>.
345: * If the arguments have the same value, the result is that same value.
346: *
347: * @param a an <code>int</code> value.
348: * @param b an <code>int</code> value.
349: * @return the smaller of <code>a</code> and <code>b</code>.
350: * @see java.lang.Long#MIN_VALUE
351: */
352: public static int min(int a, int b) {
353: return (a <= b) ? a : b;
354: }
355:
356: /**
357: * Returns the smaller of two <code>long</code> values. That is, the
358: * result is the argument closer to the value of
359: * <code>Long.MIN_VALUE</code>. If the arguments have the same value,
360: * the result is that same value.
361: *
362: * @param a a <code>long</code> value.
363: * @param b a <code>long</code> value.
364: * @return the smaller of <code>a</code> and <code>b</code>.
365: * @see java.lang.Long#MIN_VALUE
366: */
367: public static long min(long a, long b) {
368: return (a <= b) ? a : b;
369: }
370:
371: /**
372: * Returns the smaller of two <code>float</code> values. That is, the
373: * result is the value closer to negative infinity. If the arguments
374: * have the same value, the result is that same value. If either value
375: * is <code>NaN</code>, then the result is <code>NaN</code>. Unlike the
376: * the numerical comparison operators, this method considers negative zero
377: * to be strictly smaller than positive zero. If one argument is
378: * positive zero and the other is negative zero, the result is negative
379: * zero.
380: *
381: * @param a a <code>float</code> value.
382: * @param b a <code>float</code> value.
383: * @return the smaller of <code>a</code> and <code>b.</code>
384: * @since CLDC 1.1
385: */
386: public static float min(float a, float b) {
387: if (a != a)
388: return a; // a is NaN
389: if ((a == 0.0f) && (b == 0.0f)
390: && (Float.floatToIntBits(b) == negativeZeroFloatBits)) {
391: return b;
392: }
393: return (a <= b) ? a : b;
394: }
395:
396: /**
397: * Returns the smaller of two <code>double</code> values. That is, the
398: * result is the value closer to negative infinity. If the arguments have
399: * the same value, the result is that same value. If either value
400: * is <code>NaN</code>, then the result is <code>NaN</code>. Unlike the
401: * the numerical comparison operators, this method considers negative zero
402: * to be strictly smaller than positive zero. If one argument is
403: * positive zero and the other is negative zero, the result is negative
404: * zero.
405: *
406: * @param a a <code>double</code> value.
407: * @param b a <code>double</code> value.
408: * @return the smaller of <code>a</code> and <code>b</code>.
409: * @since CLDC 1.1
410: */
411: public static double min(double a, double b) {
412: if (a != a)
413: return a; // a is NaN
414: if ((a == 0.0d)
415: && (b == 0.0d)
416: && (Double.doubleToLongBits(b) == negativeZeroDoubleBits)) {
417: return b;
418: }
419: return (a <= b) ? a : b;
420: }
421: }
|