math.java
来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 531 行 · 第 1/2 页
JAVA
531 行
* is not equal to a whole number.
* @since JDK1.0
*/
public static native double pow(double a, double b);
/**
* Returns the closest <code>int</code> to the argument.
* <p>
* If the argument is negative infinity or any value less than or
* equal to the value of <code>Integer.MIN_VALUE</code>, the result is
* equal to the value of <code>Integer.MIN_VALUE</code>.
* <p>
* If the argument is positive infinity or any value greater than or
* equal to the value of <code>Integer.MAX_VALUE</code>, the result is
* equal to the value of <code>Integer.MAX_VALUE</code>.
*
* @param a a <code>float</code> value.
* @return the value of the argument rounded to the nearest
* <code>int</code> value.
* @see java.lang.Integer#MAX_VALUE
* @see java.lang.Integer#MIN_VALUE
* @since JDK1.0
*/
public static int round(float a) {
return (int)floor(a + 0.5f);
}
/**
* Returns the closest <code>long</code> to the argument.
* <p>
* If the argument is negative infinity or any value less than or
* equal to the value of <code>Long.MIN_VALUE</code>, the result is
* equal to the value of <code>Long.MIN_VALUE</code>.
* <p>
* If the argument is positive infinity or any value greater than or
* equal to the value of <code>Long.MAX_VALUE</code>, the result is
* equal to the value of <code>Long.MAX_VALUE</code>.
*
* @param a a <code>double</code> value.
* @return the value of the argument rounded to the nearest
* <code>long</code> value.
* @see java.lang.Long#MAX_VALUE
* @see java.lang.Long#MIN_VALUE
* @since JDK1.0
*/
public static long round(double a) {
return (long)floor(a + 0.5d);
}
private static Random randomNumberGenerator;
/**
* Returns a random number between <code>0.0</code> and <code>1.0</code>.
* Random number generators are often referred to as pseudorandom number
* generators because the numbers produced tend to repeat themselves after
* a period of time.
*
* @return a pseudorandom <code>double</code> between <code>0.0</code>
* and <code>1.0</code>.
* @see java.util.Random#nextDouble()
* @since JDK1.0
*/
public static synchronized double random() {
if (randomNumberGenerator == null)
randomNumberGenerator = new Random();
return randomNumberGenerator.nextDouble();
}
/**
* Returns the absolute value of an <code>int</code> value.
* If the argument is not negative, the argument is returned.
* If the argument is negative, the negation of the argument is returned.
* <p>
* Note that if the argument is equal to the value of
* <code>Integer.MIN_VALUE</code>, the most negative representable
* <code>int</code> value, the result is that same value, which is
* negative.
*
* @param a an <code>int</code> value.
* @return the absolute value of the argument.
* @see java.lang.Integer#MIN_VALUE
* @since JDK1.0
*/
public static int abs(int a) {
return (a < 0) ? -a : a;
}
/**
* Returns the absolute value of a <code>long</code> value.
* If the argument is not negative, the argument is returned.
* If the argument is negative, the negation of the argument is returned.
* <p>
* Note that if the argument is equal to the value of
* <code>Long.MIN_VALUE</code>, the most negative representable
* <code>long</code> value, the result is that same value, which is
* negative.
*
* @param a a <code>long</code> value.
* @return the absolute value of the argument.
* @see java.lang.Long#MIN_VALUE
* @since JDK1.0
*/
public static long abs(long a) {
return (a < 0) ? -a : a;
}
/**
* Returns the absolute value of a <code>float</code> value.
* If the argument is not negative, the argument is returned.
* If the argument is negative, the negation of the argument is returned.
*
* @param a a <code>float</code> value.
* @return the absolute value of the argument.
* @since JDK1.0
*/
public static float abs(float a) {
return (a < 0) ? -a : a;
}
/**
* Returns the absolute value of a <code>double</code> value.
* If the argument is not negative, the argument is returned.
* If the argument is negative, the negation of the argument is returned.
*
* @param a a <code>double</code> value.
* @return the absolute value of the argument.
* @since JDK1.0
*/
public static double abs(double a) {
return (a < 0) ? -a : a;
}
/**
* Returns the greater of two <code>int</code> values.
*
* @param a an <code>int</code> value.
* @param b an <code>int</code> value.
* @return the larger of <code>a</code> and <code>b</code>.
* @since JDK1.0
*/
public static int max(int a, int b) {
return (a >= b) ? a : b;
}
/**
* Returns the greater of two <code>long</code> values.
*
* @param a a <code>long</code> value.
* @param b a <code>long</code> value.
* @return the larger of <code>a</code> and <code>b</code>.
* @since JDK1.0
*/
public static long max(long a, long b) {
return (a >= b) ? a : b;
}
private static long negativeZeroFloatBits = Float.floatToIntBits(-0.0f);
private static long negativeZeroDoubleBits = Double.doubleToLongBits(-0.0d);
/**
* Returns the greater of two <code>float</code> values. If either value
* is <code>NaN</code>, then the result is <code>NaN</code>. Unlike the
* the numerical comparison operators, this method considers negative zero
* to be strictly smaller than positive zero.
*
* @param a a <code>float</code> value.
* @param b a <code>float</code> value.
* @return the larger of <code>a</code> and <code>b</code>.
* @since JDK1.0
*/
public static float max(float a, float b) {
if (a != a) return a; // a is NaN
if ((a == 0.0f) && (b == 0.0f)
&& (Float.floatToIntBits(a) == negativeZeroFloatBits)) {
return b;
}
return (a >= b) ? a : b;
}
/**
* Returns the greater of two <code>double</code> values. If either value
* is <code>NaN</code>, then the result is <code>NaN</code>. Unlike the
* the numerical comparison operators, this method considers negative zero
* to be strictly smaller than positive zero.
*
* @param a a <code>double</code> value.
* @param b a <code>double</code> value.
* @return the larger of <code>a</code> and <code>b</code>.
* @since JDK1.0
*/
public static double max(double a, double b) {
if (a != a) return a; // a is NaN
if ((a == 0.0d) && (b == 0.0d)
&& (Double.doubleToLongBits(a) == negativeZeroDoubleBits)) {
return b;
}
return (a >= b) ? a : b;
}
/**
* Returns the smaller of two <code>int</code> values.
*
* @param a an <code>int</code> value.
* @param b an <code>int</code> value.
* @return the smaller of <code>a</code> and <code>b</code>.
* @since JDK1.0
*/
public static int min(int a, int b) {
return (a <= b) ? a : b;
}
/**
* Returns the smaller of two <code>long</code> values.
*
* @param a a <code>long</code> value.
* @param b a <code>long</code> value.
* @return the smaller of <code>a</code> and <code>b</code>.
* @since JDK1.0
*/
public static long min(long a, long b) {
return (a <= b) ? a : b;
}
/**
* Returns the smaller of two <code>float</code> values. If either value
* is <code>NaN</code>, then the result is <code>NaN</code>. Unlike the
* the numerical comparison operators, this method considers negative zero
* to be strictly smaller than positive zero.
*
* @param a a <code>float</code> value.
* @param b a <code>float</code> value.
* @return the smaller of <code>a</code> and <code>b.</code>
* @since JDK1.0
*/
public static float min(float a, float b) {
if (a != a) return a; // a is NaN
if ((a == 0.0f) && (b == 0.0f)
&& (Float.floatToIntBits(b) == negativeZeroFloatBits)) {
return b;
}
return (a <= b) ? a : b;
}
/**
* Returns the smaller of two <code>double</code> values. If either value
* is <code>NaN</code>, then the result is <code>NaN</code>. Unlike the
* the numerical comparison operators, this method considers negative zero
* to be strictly smaller than positive zero.
*
* @param a a <code>double</code> value.
* @param b a <code>double</code> value.
* @return the smaller of <code>a</code> and <code>b</code>.
* @since JDK1.0
*/
public static double min(double a, double b) {
if (a != a) return a; // a is NaN
if ((a == 0.0d) && (b == 0.0d)
&& (Double.doubleToLongBits(b) == negativeZeroDoubleBits)) {
return b;
}
return (a <= b) ? a : b;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?