数学和数值操作相关
- Math
- 一、基础算术运算
- 1. `int abs(int a)`
- 2. `long abs(long a)`
- 3. `float abs(float a)`
- 4. `double abs(double a)`
- 5. `int max(int a, int b)`
- 6. `long max(long a, long b)`
- 7. `float max(float a, float b)`
- 8. `double max(double a, double b)`
- 9. `int min(int a, int b)`
- 10. `long min(long a, long b)`
- 11. `float min(float a, float b)`
- 12. `double min(double a, double b)`
- 13. `double pow(double a, double b)`
- 14. `double sqrt(double a)`
- 二、指数运算与对数运算
- 三、三角函数运算
- 四、舍入运算与其他运算
- 五、其他数学运算
- 六、角度与弧度之间的转换
- 七、特殊运算
- 八、位操作
- 总结
- StrictMath
- 一、基础算术运算
- 1. `int abs(int a)`
- 2. `long abs(long a)`
- 3. `float abs(float a)`
- 4. `double abs(double a)`
- 5. `int max(int a, int b)`
- 6. `long max(long a, long b)`
- 7. `float max(float a, float b)`
- 8. `double max(double a, double b)`
- 9. `int min(int a, int b)`
- 10. `long min(long a, long b)`
- 11. `float min(float a, float b)`
- 12. `double min(double a, double b)`
- 13. `double pow(double a, double b)`
- 14. `double sqrt(double a)`
- 二、指数运算与对数运算
- 三、三角函数运算
- 四、舍入运算与其他运算
- 五、其他数学运算
- 六、角度与弧度之间的转换
- 七、特殊运算
- 八、位操作
- 总结
- RoundingMode
- MathContext
- BigInteger
- BigDecimal
Math
Math
类是 Java 中的一个内置类,提供了一些基本的数学运算方法。它包含了许多静态方法,用于执行数学运算,如基本的算术运算、指数运算、对数运算、三角函数运算、舍入运算等。这些方法是不可变的,且不需要实例化 Math
类即可使用。
以下是对 Math
类中所有方法的详细讲解,包括参数、作用、用途、使用场景,并提供详细的示例代码,帮助理解和熟练运用这些方法。
一、基础算术运算
1. int abs(int a)
功能: 返回一个 int
值的绝对值。
参数:
a
- 要操作的int
值。
返回值: int
- a
的绝对值。
示例:
public class MathExample {
public static void main(String[] args) {
int result = Math.abs(-10);
System.out.println("Absolute value: " + result); // 输出: 10
}
}
2. long abs(long a)
功能: 返回一个 long
值的绝对值。
参数:
a
- 要操作的long
值。
返回值: long
- a
的绝对值。
示例:
public class MathExample {
public static void main(String[] args) {
long result = Math.abs(-10L);
System.out.println("Absolute value: " + result); // 输出: 10
}
}
3. float abs(float a)
功能: 返回一个 float
值的绝对值。
参数:
a
- 要操作的float
值。
返回值: float
- a
的绝对值。
示例:
public class MathExample {
public static void main(String[] args) {
float result = Math.abs(-10.5f);
System.out.println("Absolute value: " + result); // 输出: 10.5
}
}
4. double abs(double a)
功能: 返回一个 double
值的绝对值。
参数:
a
- 要操作的double
值。
返回值: double
- a
的绝对值。
示例:
public class MathExample {
public static void main(String[] args) {
double result = Math.abs(-10.5);
System.out.println("Absolute value: " + result); // 输出: 10.5
}
}
5. int max(int a, int b)
功能: 返回两个 int
值中的较大值。
参数:
a
- 第一个int
值。b
- 第二个int
值。
返回值: int
- a
和 b
中的较大值。
示例:
public class MathExample {
public static void main(String[] args) {
int result = Math.max(10, 20);
System.out.println("Max value: " + result); // 输出: 20
}
}
6. long max(long a, long b)
功能: 返回两个 long
值中的较大值。
参数:
a
- 第一个long
值。b
- 第二个long
值。
返回值: long
- a
和 b
中的较大值。
示例:
public class MathExample {
public static void main(String[] args) {
long result = Math.max(10L, 20L);
System.out.println("Max value: " + result); // 输出: 20
}
}
7. float max(float a, float b)
功能: 返回两个 float
值中的较大值。
参数:
a
- 第一个float
值。b
- 第二个float
值。
返回值: float
- a
和 b
中的较大值。
示例:
public class MathExample {
public static void main(String[] args) {
float result = Math.max(10.5f, 20.5f);
System.out.println("Max value: " + result); // 输出: 20.5
}
}
8. double max(double a, double b)
功能: 返回两个 double
值中的较大值。
参数:
a
- 第一个double
值。b
- 第二个double
值。
返回值: double
- a
和 b
中的较大值。
示例:
public class MathExample {
public static void main(String[] args) {
double result = Math.max(10.5, 20.5);
System.out.println("Max value: " + result); // 输出: 20.5
}
}
9. int min(int a, int b)
功能: 返回两个 int
值中的较小值。
参数:
a
- 第一个int
值。b
- 第二个int
值。
返回值: int
- a
和 b
中的较小值。
示例:
public class MathExample {
public static void main(String[] args) {
int result = Math.min(10, 20);
System.out.println("Min value: " + result); // 输出: 10
}
}
10. long min(long a, long b)
功能: 返回两个 long
值中的较小值。
参数:
a
- 第一个long
值。b
- 第二个long
值。
返回值: long
- a
和 b
中的较小值。
示例:
public class MathExample {
public static void main(String[] args) {
long result = Math.min(10L, 20L);
System.out.println("Min value: " + result); // 输出: 10
}
}
11. float min(float a, float b)
功能: 返回两个 float
值中的较小值。
参数:
a
- 第一个float
值。b
- 第二个float
值。
返回值: float
- a
和 b
中的较小值。
示例:
public class MathExample {
public static void main(String[] args) {
float result = Math.min(10.5f, 20.5f);
System.out.println("Min value: " + result); // 输出: 10.5
}
}
12. double min(double a, double b)
功能: 返回两个 double
值中的较小值。
参数:
a
- 第一个double
值。b
- 第二个double
值。
返回值: double
- a
和 b
中的较小值。
示例:
public class MathExample {
public static void main(String[] args) {
double result = Math.min(10.5, 20.5);
System.out.println("Min value: " + result); // 输出: 10.5
}
}
13. double pow(double a, double b)
功能: 返回第一个参数的第二个参数次幂的值。
参数:
a
- 底数。b
- 指数。
返回值: double
- a
的 b
次幂。
示例:
public class MathExample {
public static void main(String[] args) {
double result = Math.pow(2, 3);
System.out.println("Power: " + result); // 输出: 8.0
}
}
14. double sqrt(double a)
功能: 返回一个数的平方根。
参数:
a
- 要操作的double
值。
返回值: double
- a
的平方根。
示例:
public class MathExample {
public static void main(String[] args) {
double result = Math.sqrt(16);
System.out.println("Square root: " + result); // 输出: 4.0
}
}
二、指数运算与对数运算
1. double exp(double a)
功能: 返回 e
的指定次幂的值。
参数:
a
- 指定次幂的值。
返回值: double
- e
的 a
次幂。
示例:
public class MathExample {
public static void main(String[] args) {
double result = Math.exp(1);
System.out.println("Exponent: " + result); // 输出: 2.718281828459045
}
}
2. double log(double a)
功能: 返回一个数的自然对数(底为 e
)。
参数:
a
- 要操作的double
值。
返回值: double
- a
的自然对数。
示例:
public class MathExample {
public static void main(String[] args) {
double result = Math.log(2.718281828459045);
System.out.println("Natural log: " + result); // 输出: 1.0
}
}
3. double log10(double a)
功能: 返回一个数的常用对数(底为 10
)。
参数:
a
- 要操作的double
值。
返回值: double
- a
的常用对数。
示例:
public class MathExample {
public static void main(String[] args) {
double result = Math.log10(100);
System.out.println("Log base 10: " + result); // 输出: 2.0
}
}
三、三角函数运算
1. double sin(double a)
功能: 返回一个角度的正弦值。
参数:
a
- 要操作的double
值,单位为弧度。
返回值: double
- a
的正弦值。
示例:
public class MathExample {
public static void main(String[] args) {
double result = Math.sin(Math.PI / 2);
System.out.println("Sine: " + result); // 输出: 1.0
}
}
2. double cos(double a)
功能: 返回一个角度的余弦值。
参数:
a
- 要操作的double
值,单位为弧度。
返回值: double
- a
的余弦值。
示例:
public class MathExample {
public static void main(String[] args) {
double result = Math.cos(Math.PI);
System.out.println("Cosine: " + result); // 输出: -1.0
}
}
3. double tan(double a)
功能: 返回一个角度的正切值。
参数:
a
- 要操作的double
值,单位为弧度。
返回值: double
- a
的正切值。
示例:
public class MathExample {
public static void main(String[] args) {
double result = Math.tan(Math.PI / 4);
System.out.println("Tangent: " + result); // 输出: 1.0
}
}
4. double asin(double a)
功能: 返回一个值的反正弦值。
参数:
a
- 要操作的double
值。
返回值: double
- a
的反正弦值,单位为弧度。
示例:
public class MathExample {
public static void main(String[] args) {
double result = Math.asin(1);
System.out.println("Arcsine: " + result); // 输出: 1.5707963267948966 (PI/2)
}
}
5. double acos(double a)
功能: 返回一个值的反余弦值。
参数:
a
- 要操作的double
值。
返回值: double
- a
的反余弦值,单位为弧度。
示例:
public class MathExample {
public static void main(String[] args) {
double result = Math.acos(-1);
System.out.println("Arccosine: " + result); // 输出: 3.141592653589793 (PI)
}
}
6. double atan(double a)
功能: 返回一个值的反正切值。
参数:
a
- 要操作的double
值。
返回值: double
- a
的反正切值,单位为弧度。
示例:
public class MathExample {
public static void main(String[] args) {
double result = Math.atan(1);
System.out.println("Arctangent: " + result); // 输出: 0.7853981633974483 (PI/4)
}
}
7. double atan2(double y, double x)
功能: 返回从原点到点 (x, y)
的方位角,表示为从 x 轴到点的角度,单位为弧度。
参数:
y
- y 坐标。x
- x 坐标。
返回值: double
- 从原点到点 (x, y)
的方位角。
示例:
public class MathExample {
public static void main(String[] args) {
double result = Math.atan2(1, 1);
System.out.println("Atan2: " + result); // 输出: 0.7853981633974483 (PI/4)
}
}
四、舍入运算与其他运算
1. double ceil(double a)
功能: 返回大于或等于参数的最小整数值。
参数:
a
- 要操作的double
值。
返回值: double
- 大于或等于 a
的最小整数值。
示例:
public class MathExample {
public static void main(String[] args) {
double result = Math.ceil(1.2);
System.out.println("Ceil: " + result); // 输出: 2.0
}
}
2. double floor(double a)
功能: 返回小于或等于参数的最大整数值。
参数:
a
- 要操作的double
值。
返回值: double
- 小于或等于 a
的最大整数值。
示例:
public class MathExample {
public static void main(String[] args) {
double result = Math.floor(1.8);
System.out.println("Floor: " + result); // 输出: 1.0
}
}
3. long round(double a)
功能: 返回参数值最接近的 long
类型整数。
参数:
a
- 要操作的double
值。
返回值: long
- 最接近 a
的 long
类型整数。
示例:
public class MathExample {
public static void main(String[] args) {
long result = Math.round(1.5);
System.out.println("Round: " + result); // 输出: 2
}
}
4. int round(float a)
功能: 返回参数值最接近的 int
类型整数。
参数:
a
- 要操作的float
值。
返回值: int
- 最接近 a
的 int
类型整数。
示例:
public class MathExample {
public static void main(String[] args) {
int result = Math.round(1.5f);
System.out.println("Round: " + result); // 输出: 2
}
}
5. double rint(double a)
功能: 返回最接近参数的整数值,结果为 double
类型。如果参数值与两个整数值距离相等,则返回偶数的那个。
参数:
a
- 要操作的double
值。
返回值: double
- 最接近 a
的整数值。
示例:
public class MathExample {
public static void main(String[] args) {
double result = Math.rint(2.5);
System.out.println("Rint: " + result); // 输出: 2.0
}
}
6. double random()
功能: 返回一个大于等于 0.0
且小于 1.0
的伪随机 double
值。
返回值: double
- 大于等于 0.0
且小于 1.0
的double
伪随机值。
示例:
public class MathExample {
public static void main(String[] args) {
double randomValue = Math.random();
System.out.println("Random value: " + randomValue);
}
}
五、其他数学运算
1. double cbrt(double a)
功能: 返回一个数的立方根。
参数:
a
- 要操作的double
值。
返回值: double
- a
的立方根。
示例:
public class MathExample {
public static void main(String[] args) {
double result = Math.cbrt(27);
System.out.println("Cube root: " + result); // 输出: 3.0
}
}
2. double expm1(double a)
功能: 返回 e^x - 1
的值。
参数:
a
- 要操作的double
值。
返回值: double
- e^a - 1
的值。
示例:
public class MathExample {
public static void main(String[] args) {
double result = Math.expm1(1);
System.out.println("Expm1: " + result); // 输出: 1.718281828459045
}
}
3. double log1p(double a)
功能: 返回 1 + a
的自然对数(以e
为底的1+a
的对数)。
参数:
a
- 要操作的double
值。
返回值: double
- 1 + a
的自然对数。
示例:
public class MathExample {
public static void main(String[] args) {
double result = Math.log1p(1);
System.out.println("Log1p: " + result); // 输出: 0.6931471805599453
}
}
4. double signum(double d)
功能: 返回参数的符号函数值。
参数:
d
- 要操作的double
值。
返回值: double
- 如果 d
是正数,则返回 1.0
;如果 d
是负数,则返回 -1.0
;如果 d
是零,则返回 0.0
。
示例:
public class MathExample {
public static void main(String[] args) {
double result = Math.signum(-10);
System.out.println("Signum: " + result); // 输出: -1.0
}
}
5. double ulp(double d)
功能: 返回参数的 ULP(单位最后位)。
参数:
d
- 要操作的double
值。
返回值: double
- 参数的 ULP。
示例:
public class MathExample {
public static void main(String[] args) {
double result = Math.ulp(1.0);
System.out.println("ULP: " + result); // 输出: 2.220446049250313E-16
}
}
六、角度与弧度之间的转换
1. double toDegrees(double angrad)
功能: 将角度从弧度转换为度。
参数:
angrad
- 要转换的弧度值。
返回值: double
- 对应的度数。
示例:
public class MathExample {
public static void main(String[] args) {
double degrees = Math.toDegrees(Math.PI);
System.out.println("Degrees: " + degrees); // 输出: 180.0
}
}
2. double toRadians(double angdeg)
功能: 将角度从度转换为弧度。
参数:
angdeg
- 要转换的度数值。
返回值: double
- 对应的弧度数。
示例:
public class MathExample {
public static void main(String[] args) {
double radians = Math.toRadians(180);
System.out.println("Radians: " + radians); // 输出: 3.141592653589793
}
}
七、特殊运算
1. double hypot(double x, double y)
功能: 返回 (x^2 + y^2
) 的平方根,即两个数的欧几里得距离。
参数:
x
- 第一个double
值。y
- 第二个double
值。
返回值: double
- 两个数的欧几里得距离。
示例:
public class MathExample {
public static void main(String[] args) {
double result = Math.hypot(3, 4);
System.out.println("Hypotenuse: " + result); // 输出: 5.0
}
}
2. double IEEEremainder(double f1, double f2)
功能: 返回根据 IEEE 754 标准计算的两个参数的余数。
参数:
f1
- 第一个double
值。f2
- 第二个double
值。
返回值: double
- f1
除以 f2
的余数。
示例:
public class MathExample {
public static void main(String[] args) {
double result = Math.IEEEremainder(10, 3);
System.out.println("IEEE remainder: " + result); // 输出: 1.0
}
}
八、位操作
1. double scalb(double d, int scaleFactor)
功能: 返回 d × 2^scaleFactor
的值。
参数:
d
- 要操作的double
值。scaleFactor
-2
的指数。
返回值: double
- d × 2^scaleFactor
的值。
示例:
public class MathExample {
public static void main(String[] args) {
double result = Math.scalb(3.0, 2);
System.out.println("Scalb: " + result); // 输出: 12.0
}
}
2. int getExponent(double d)
功能: 返回参数的无符号指数部分。
参数:
d
- 要操作的double
值。
返回值: int
- d
的无符号指数部分。
示例:
public class MathExample {
public static void main(String[] args) {
int result = Math.getExponent(8.0);
System.out.println("Exponent: " + result); // 输出: 3
}
}
3. int getExponent(float f)
功能: 返回参数的无符号指数部分。
参数:
f
- 要操作的float
值。
返回值: int
- f
的无符号指数部分。
示例:
public class MathExample {
public static void main(String[] args) {
int result = Math.getExponent(8.0f);
System.out.println("Exponent: " + result); // 输出: 3
}
}
总结
Math
类提供了丰富的数学运算方法,涵盖了基础算术运算、指数运算与对数运算、三角函数运算、舍入运算、特殊运算以及角度与弧度之间的转换。这些方法都属于静态方法,可以直接通过 Math
类名进行调用,并且无需实例化 Math
类。
StrictMath
StrictMath
类是 Java 中的一个内置类,提供了一些基本的数学运算方法。与 Math
类不同,StrictMath
类的方法保证在所有平台上都返回相同的结果,这使得它在需要跨平台一致性的场景中非常有用。StrictMath
类包含了许多静态方法,用于执行数学运算,如基本的算术运算、指数运算、对数运算、三角函数运算、舍入运算等。
以下是对 StrictMath
类中所有方法的详细讲解,包括参数、作用、用途、使用场景,并提供详细的示例代码,帮助理解和熟练运用这些方法。
一、基础算术运算
1. int abs(int a)
功能: 返回一个 int
值的绝对值。
参数:
a
- 要操作的int
值。
返回值: int
- a
的绝对值。
示例:
public class StrictMathExample {
public static void main(String[] args) {
int result = StrictMath.abs(-10);
System.out.println("Absolute value: " + result); // 输出: 10
}
}
2. long abs(long a)
功能: 返回一个 long
值的绝对值。
参数:
a
- 要操作的long
值。
返回值: long
- a
的绝对值。
示例:
public class StrictMathExample {
public static void main(String[] args) {
long result = StrictMath.abs(-10L);
System.out.println("Absolute value: " + result); // 输出: 10
}
}
3. float abs(float a)
功能: 返回一个 float
值的绝对值。
参数:
a
- 要操作的float
值。
返回值: float
- a
的绝对值。
示例:
public class StrictMathExample {
public static void main(String[] args) {
float result = StrictMath.abs(-10.5f);
System.out.println("Absolute value: " + result); // 输出: 10.5
}
}
4. double abs(double a)
功能: 返回一个 double
值的绝对值。
参数:
a
- 要操作的double
值。
返回值: double
- a
的绝对值。
示例:
public class StrictMathExample {
public static void main(String[] args) {
double result = StrictMath.abs(-10.5);
System.out.println("Absolute value: " + result); // 输出: 10.5
}
}
5. int max(int a, int b)
功能: 返回两个 int
值中的较大值。
参数:
a
- 第一个int
值。b
- 第二个int
值。
返回值: int
- a
和 b
中的较大值。
示例:
public class StrictMathExample {
public static void main(String[] args) {
int result = StrictMath.max(10, 20);
System.out.println("Max value: " + result); // 输出: 20
}
}
6. long max(long a, long b)
功能: 返回两个 long
值中的较大值。
参数:
a
- 第一个long
值。b
- 第二个long
值。
返回值: long
- a
和 b
中的较大值。
示例:
public class StrictMathExample {
public static void main(String[] args) {
long result = StrictMath.max(10L, 20L);
System.out.println("Max value: " + result); // 输出: 20
}
}
7. float max(float a, float b)
功能: 返回两个 float
值中的较大值。
参数:
a
- 第一个float
值。b
- 第二个float
值。
返回值: float
- a
和 b
中的较大值。
示例:
public class StrictMathExample {
public static void main(String[] args) {
float result = StrictMath.max(10.5f, 20.5f);
System.out.println("Max value: " + result); // 输出: 20.5
}
}
8. double max(double a, double b)
功能: 返回两个 double
值中的较大值。
参数:
a
- 第一个double
值。b
- 第二个double
值。
返回值: double
- a
和 b
中的较大值。
示例:
public class StrictMathExample {
public static void main(String[] args) {
double result = StrictMath.max(10.5, 20.5);
System.out.println("Max value: " + result); // 输出: 20.5
}
}
9. int min(int a, int b)
功能: 返回两个 int
值中的较小值。
参数:
a
- 第一个int
值。b
- 第二个int
值。
返回值: int
- a
和 b
中的较小值。
示例:
public class StrictMathExample {
public static void main(String[] args) {
int result = StrictMath.min(10, 20);
System.out.println("Min value: " + result); // 输出: 10
}
}
10. long min(long a, long b)
功能: 返回两个 long
值中的较小值。
参数:
a
- 第一个long
值。b
- 第二个long
值。
返回值: long
- a
和 b
中的较小值。
示例:
public class StrictMathExample {
public static void main(String[] args) {
long result = StrictMath.min(10L, 20L);
System.out.println("Min value: " + result); // 输出: 10
}
}
11. float min(float a, float b)
功能: 返回两个 float
值中的较小值。
参数:
a
- 第一个float
值。b
- 第二个float
值。
返回值: float
- a
和 b
中的较小值。
示例:
public class StrictMathExample {
public static void main(String[] args) {
float result = StrictMath.min(10.5f, 20.5f);
System.out.println("Min value: " + result); // 输出: 10.5
}
}
12. double min(double a, double b)
功能: 返回两个 double
值中的较小值。
参数:
a
- 第一个double
值。b
- 第二个double
值。
返回值: double
- a
和 b
中的较小值。
示例:
public class StrictMathExample {
public static void main(String[] args) {
double result = StrictMath.min(10.5, 20.5);
System.out.println("Min value: " + result); // 输出: 10.5
}
}
13. double pow(double a, double b)
功能: 返回第一个参数的第二个参数次幂的值。
参数:
a
- 底数。b
- 指数。
返回值: double
- a
的 b
次幂。
示例:
public class StrictMathExample {
public static void main(String[] args) {
double result = StrictMath.pow(2, 3);
System.out.println("Power: " + result); // 输出: 8.0
}
}
14. double sqrt(double a)
功能: 返回一个数的平方根。
参数:
a
- 要操作的double
值。
返回值: double
- a
的平方根。
示例:
public class StrictMathExample {
public static void main(String[] args) {
double result = StrictMath.sqrt(16);
System.out.println("Square root: " + result); // 输出: 4.0
}
}
二、指数运算与对数运算
1. double exp(double a)
功能: 返回 e
的指定次幂的值。
参数:
a
- 指定次幂的值。
返回值: double
- e
的 a
次幂。
示例:
public class StrictMathExample {
public static void main(String[] args) {
double result = StrictMath.exp(1);
System.out.println("Exponent: " + result); // 输出: 2.718281828459045
}
}
2. double log(double a)
功能: 返回一个数的自然对数(底为 e
)。
参数:
a
- 要操作的double
值。
返回值: double
- a
的自然对数。
示例:
public class StrictMathExample {
public static void main(String[] args) {
double result = StrictMath.log(2.718281828459045);
System.out.println("Natural log: " + result); // 输出: 1.0
}
}
3. double log10(double a)
功能: 返回一个数的常用对数(底为 10
)。
参数:
a
- 要操作的double
值。
返回值: double
- a
的常用对数。
示例:
public class StrictMathExample {
public static void main(String[] args) {
double result = StrictMath.log10(100);
System.out.println("Log base 10: " + result); // 输出: 2.0
}
}
三、三角函数运算
1. double sin(double a)
功能: 返回一个角度的正弦值。
参数:
a
- 要操作的double
值,单位为弧度。
返回值: double
- a
的正弦值。
示例:
public class StrictMathExample {
public static void main(String[] args) {
double result = StrictMath.sin(StrictMath.PI / 2);
System.out.println("Sine: " + result); // 输出: 1.0
}
}
2. double cos(double a)
功能: 返回一个角度的余弦值。
参数:
a
- 要操作的double
值,单位为弧度。
返回值: double
- a
的余弦值。
示例:
public class StrictMathExample {
public static void main(String[] args) {
double result = StrictMath.cos(StrictMath.PI);
System.out.println("Cosine: " + result); // 输出: -1.0
}
}
3. double tan(double a)
功能: 返回一个角度的正切值。
参数:
a
- 要操作的double
值,单位为弧度。
返回值: double
- a
的正切值。
示例:
public class StrictMathExample {
public static void main(String[] args) {
double result = StrictMath.tan(StrictMath.PI / 4);
System.out.println("Tangent: " + result); // 输出: 1.0
}
}
4. double asin(double a)
功能: 返回一个值的反正弦值。
参数:
a
- 要操作的double
值。
返回值: double
- a
的反正弦值,单位为弧度。
示例:
public class StrictMathExample {
public static void main(String[] args) {
double result = StrictMath.asin(1);
System.out.println("Arcsine: " + result); // 输出: 1.5707963267948966 (PI/2)
}
}
5. double acos(double a)
功能: 返回一个值的反余弦值。
参数:
a
- 要操作的double
值。
返回值: double
- a
的反余弦值,单位为弧度。
示例:
public class StrictMathExample {
public static void main(String[] args) {
double result = StrictMath.acos(-1);
System.out.println("Arccosine: " + result); // 输出: 3.141592653589793 (PI)
}
}
6. double atan(double a)
功能: 返回一个值的反正切值。
参数:
a
- 要操作的double
值。
返回值: double
- a
的反正切值,单位为弧度。
示例:
public class StrictMathExample {
public static void main(String[] args) {
double result = StrictMath.atan(1);
System.out.println("Arctangent: " + result); // 输出: 0.7853981633974483 (PI/4)
}
}
7. double atan2(double y, double x)
功能: 返回从原点到点 (x, y)
的方位角,表示为从 x 轴到点的角度,单位为弧度。
参数:
y
- y 坐标。x
- x 坐标。
返回值: double
- 从原点到点 (x, y)
的方位角。
示例:
public class StrictMathExample {
public static void main(String[] args) {
double result = StrictMath.atan2(1, 1);
System.out.println("Atan2: " + result); // 输出: 0.7853981633974483 (PI/4)
}
}
四、舍入运算与其他运算
1. double ceil(double a)
功能: 返回大于或等于参数的最小整数值。
参数:
a
- 要操作的double
值。
返回值: double
- 大于或等于 a
的最小整数值。
示例:
public class StrictMathExample {
public static void main(String[] args) {
double result = StrictMath.ceil(1.2);
System.out.println("Ceil: " + result); // 输出: 2.0
}
}
2. double floor(double a)
功能: 返回小于或等于参数的最大整数值。
参数:
a
- 要操作的double
值。
返回值: double
- 小于或等于 a
的最大整数值。
示例:
public class StrictMathExample {
public static void main(String[] args) {
double result = StrictMath.floor(1.8);
System.out.println("Floor: " + result); // 输出: 1.0
}
}
3. long round(double a)
功能: 返回参数值最接近的 long
类型整数。
参数:
a
- 要操作的double
值。
返回值: long
- 最接近 a
的 long
类型整数。
示例:
public class StrictMathExample {
public static void main(String[] args) {
long result = StrictMath.round(1.5);
System.out.println("Round: " + result); // 输出: 2
}
}
4. int round(float a)
功能: 返回参数值最接近的 int
类型整数。
参数:
a
- 要操作的float
值。
返回值: int
- 最接近 a
的 int
类型整数。
示例:
public class StrictMathExample {
public static void main(String[] args) {
int result = StrictMath.round(1.5f);
System.out.println("Round: " + result); // 输出: 2
}
}
5. double rint(double a)
功能: 返回最接近参数的整数值,结果为 double
类型。如果参数值与两个整数值距离相等,则返回偶数的那个。
参数:
a
- 要操作的double
值。
返回值: double
- 最接近 a
的整数值。
示例:
public class StrictMathExample {
public static void main(String[] args) {
double result = StrictMath.rint(2.5);
System.out.println("Rint: " + result); // 输出: 2.0
}
}
6. double random()
功能: 返回一个大于等于 0.0
且小于 1.0
的伪随机 double
值。
返回值: double
- 大于等于 0.0
且小于 1.0
的 double
伪随机值。
示例:
public class StrictMathExample {
public static void main(String[] args) {
double randomValue = StrictMath.random();
System.out.println("Random value: " + randomValue);
}
}
五、其他数学运算
1. double cbrt(double a)
功能: 返回一个数的立方根。
参数:
a
- 要操作的double
值。
返回值: double
- a
的立方根。
示例:
public class StrictMathExample {
public static void main(String[] args) {
double result = StrictMath.cbrt(27);
System.out.println("Cube root: " + result); // 输出: 3.0
}
}
2. double expm1(double a)
功能: 返回 e^x - 1
的值。
参数:
a
- 要操作的double
值。
返回值: double
- e^a - 1
的值。
示例:
public class StrictMathExample {
public static void main(String[] args) {
double result = StrictMath.expm1(1);
System.out.println("Expm1: " + result); // 输出: 1.718281828459045
}
}
3. double log1p(double a)
功能: 返回 1 + a
的自然对数。
参数:
a
- 要操作的double
值。
返回值: double
- 1 + a
的自然对数。
示例:
public class StrictMathExample {
public static void main(String[] args) {
double result = StrictMath.log1p(1);
System.out.println("Log1p: " + result); // 输出: 0.6931471805599453
}
}
4. double signum(double d)
功能: 返回参数的符号函数值。
参数:
d
- 要操作的double
值。
返回值: double
- 如果 d
是正数,则返回 1.0
;如果 d
是负数,则返回 -1.0
;如果 d
是零,则返回 0.0
。
示例:
public class StrictMathExample {
public static void main(String[] args) {
double result = StrictMath.signum(-10);
System.out.println("Signum: " + result); // 输出: -1.0
}
}
5. double ulp(double d)
功能: 返回参数的 ULP(单位最后位)。
参数:
d
- 要操作的double
值。
返回值: double
- 参数的 ULP。
示例:
public class StrictMathExample {
public static void main(String[] args) {
double result = StrictMath.ulp(1.0);
System.out.println("ULP: " + result); // 输出: 2.220446049250313E-16
}
}
六、角度与弧度之间的转换
1. double toDegrees(double angrad)
功能: 将角度从弧度转换为度。
参数:
angrad
- 要转换的弧度值。
返回值: double
- 对应的度数。
示例:
public class StrictMathExample {
public static void main(String[] args) {
double degrees = StrictMath.toDegrees(Math.PI);
System.out.println("Degrees: " + degrees); // 输出: 180.0
}
}
2. double toRadians(double angdeg)
功能: 将角度从度转换为弧度。
参数:
angdeg
- 要转换的度数值。
返回值: double
- 对应的弧度数。
示例:
public class StrictMathExample {
public static void main(String[] args) {
double radians = StrictMath.toRadians(180);
System.out.println("Radians: " + radians); // 输出: 3.141592653589793
}
}
七、特殊运算
1. double hypot(double x, double y)
功能: 返回 (x^2 + y^2
) 的平方根,即两个数的欧几里得距离。
参数:
x
- 第一个double
值。y
- 第二个double
值。
返回值: double
- 两个数的欧几里得距离。
示例:
public class StrictMathExample {
public static void main(String[] args) {
double result = StrictMath.hypot(3, 4);
System.out.println("Hypotenuse: " + result); // 输出: 5.0
}
}
2. double IEEEremainder(double f1, double f2)
功能: 返回根据 IEEE 754 标准计算的两个参数的余数。
参数:
f1
- 第一个double
值。f2
- 第二个double
值。
返回值: double
- f1
除以 f2
的余数。
示例:
public class StrictMathExample {
public static void main(String[] args) {
double result = StrictMath.IEEEremainder(10, 3);
System.out.println("IEEE remainder: " + result); // 输出: 1.0
}
}
八、位操作
1. double scalb(double d, int scaleFactor)
功能: 返回 d × 2^scaleFactor
的值。
参数:
d
- 要操作的double
值。scaleFactor
-2
的指数。
返回值: double
- d × 2^scaleFactor
的值。
示例:
public class StrictMathExample {
public static void main(String[] args) {
double result = StrictMath.scalb(3.0, 2);
System.out.println("Scalb: " + result); // 输出: 12.0
}
}
2. int getExponent(double d)
功能: 返回参数的无符号指数部分。
参数:
d
- 要操作的double
值。
返回值: int
- d
的无符号指数部分。
示例:
public class StrictMathExample {
public static void main(String[] args) {
int result = StrictMath.getExponent(8.0);
System.out.println("Exponent: " + result); // 输出: 3
}
}
3. int getExponent(float f)
功能: 返回参数的无符号指数部分。
参数:
f
- 要操作的float
值。
返回值: int
- f
的无符号指数部分。
示例:
public class StrictMathExample {
public static void main(String[] args) {
int result = StrictMath.getExponent(8.0f);
System.out.println("Exponent: " + result); // 输出: 3
}
}
总结
StrictMath
类提供了丰富的数学运算方法,涵盖了基础算术运算、指数运算与对数运算、三角函数运算、舍入运算、特殊运算以及角度与弧度之间的转换。这些方法都属于静态方法,可以直接通过 StrictMath
类名进行调用,并且无需实例化 StrictMath
类。
RoundingMode
RoundingMode
类是 Java 中用于指定舍入行为的枚举类。它定义了舍入的各种模式,主要用于 BigDecimal
和 MathContext
中的舍入操作。下面我将详细讲解 RoundingMode
类中的所有方法,并按照用途、功能、目的、作用、使用场景等方面进行分类整理,并提供相应的测试用例。
一、枚举常量
RoundingMode
枚举包含的舍入模式如下:
- CEILING: 向正无穷大方向舍入。
- DOWN: 向零方向舍入。
- FLOOR: 向负无穷大方向舍入。
- HALF_DOWN: 向最近的邻居方向舍入,如果距离相等,则向下舍入。
- HALF_EVEN: 向最近的邻居方向舍入,如果距离相等,则向偶数方向舍入。
- HALF_UP: 向最近的邻居方向舍入,如果距离相等,则向上舍入。** - 四舍五入**
- UNNECESSARY: 断言请求的操作具有精确的结果,因此不需要舍入。如果舍入是必要的,则抛出
ArithmeticException
。 - UP: 远离零方向舍入。
二、方法分类
1. 获取枚举常量的方法
RoundingMode
枚举提供了一些方法来获取其枚举常量。主要的方法包括:
values()
valueOf(String name)
valueOf(int rm)
**1.1 **values()
功能: 返回包含所有枚举常量的数组。
返回值: RoundingMode[]
- 一个包含所有枚举常量的数组。
示例:
import java.math.RoundingMode;
public class RoundingModeExample {
public static void main(String[] args) {
RoundingMode[] modes = RoundingMode.values();
for (RoundingMode mode : modes) {
System.out.println("RoundingMode: " + mode);
}
}
}
**1.2 **valueOf(String name)
功能: 返回带指定名称的枚举常量。
参数:
name
- 枚举常量的名称。
返回值: RoundingMode
- 带指定名称的枚举常量。
示例:
import java.math.RoundingMode;
public class RoundingModeExample {
public static void main(String[] args) {
RoundingMode mode = RoundingMode.valueOf("HALF_UP");
System.out.println("RoundingMode: " + mode);
}
}
**1.3 **valueOf(int rm)
功能: 返回与指定整数值对应的枚举常量。
参数:
rm
- 整数值。
返回值: RoundingMode
- 与指定整数值对应的枚举常量。
示例:
import java.math.RoundingMode;
public class RoundingModeExample {
public static void main(String[] args) {
RoundingMode mode = RoundingMode.valueOf(4); // 4 对应的是 HALF_DOWN
System.out.println("RoundingMode: " + mode);
}
}
三、用途、功能、目的与使用场景
1. 财务计算
在财务计算中,舍入模式非常重要,因为这会影响到最终结果。例如,向上舍入、向下舍入等会导致不同的金额结果。
示例:
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
public class FinancialCalculationExample {
public static void main(String[] args) {
BigDecimal amount = new BigDecimal("123.456");
MathContext mc = new MathContext(4, RoundingMode.HALF_UP);
BigDecimal roundedAmount = amount.round(mc);
System.out.println("Rounded Amount (HALF_UP): " + roundedAmount); // 输出: 123.5
}
}
2. 科学计算
在科学计算中,高精度和舍入方式非常重要,以确保计算结果的准确性和可靠性。
示例:
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
public class ScientificCalculationExample {
public static void main(String[] args) {
BigDecimal value = new BigDecimal("123.456789");
MathContext mc = new MathContext(6, RoundingMode.HALF_EVEN);
BigDecimal roundedValue = value.round(mc);
System.out.println("Rounded Value (HALF_EVEN): " + roundedValue); // 输出: 123.457
}
}
3. 数据处理
在处理大量数据时,舍入方式可以影响到数据的统计结果和精度。
示例:
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
public class DataProcessingExample {
public static void main(String[] args) {
BigDecimal dataValue = new BigDecimal("123.4567");
MathContext mc = new MathContext(3, RoundingMode.FLOOR);
BigDecimal roundedDataValue = dataValue.round(mc);
System.out.println("Rounded Data Value (FLOOR): " + roundedDataValue); // 输出: 123
}
}
四、综合示例
下面是一个综合示例,展示了各种舍入模式在不同上下文中的应用:
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
public class RoundingModeComprehensiveExample {
public static void main(String[] args) {
BigDecimal value = new BigDecimal("123.456");
// 向正无穷大方向舍入
MathContext mcCeiling = new MathContext(4, RoundingMode.CEILING);
BigDecimal resultCeiling = value.round(mcCeiling);
System.out.println("Rounded (CEILING): " + resultCeiling); // 输出: 123.5
// 向零方向舍入
MathContext mcDown = new MathContext(4, RoundingMode.DOWN);
BigDecimal resultDown = value.round(mcDown);
System.out.println("Rounded (DOWN): " + resultDown); // 输出: 123.4
// 向负无穷大方向舍入
MathContext mcFloor = new MathContext(4, RoundingMode.FLOOR);
BigDecimal resultFloor = value.round(mcFloor);
System.out.println("Rounded (FLOOR): " + resultFloor); // 输出: 123.4
// 向最近的邻居方向舍入,如果距离相等,则向下舍入
MathContext mcHalfDown = new MathContext(4, RoundingMode.HALF_DOWN);
BigDecimal resultHalfDown = value.round(mcHalfDown);
System.out.println("Rounded (HALF_DOWN): " + resultHalfDown); // 输出: 123.5
// 向最近的邻居方向舍入,如果距离相等,则向偶数方向舍入
MathContext mcHalfEven = new MathContext(4, RoundingMode.HALF_EVEN);
BigDecimal resultHalfEven = value.round(mcHalfEven);
System.out.println("Rounded (HALF_EVEN): " + resultHalfEven); // 输出: 123.5
// 向最近的邻居方向舍入,如果距离相等,则向上舍入
MathContext mcHalfUp = new MathContext(4, RoundingMode.HALF_UP);
BigDecimal resultHalfUp = value.round(mcHalfUp);
System.out.println("Rounded (HALF_UP): " + resultHalfUp); // 输出: 123.5
// 断言请求的操作具有精确的结果,因此不需要舍入
try {
MathContext mcUnnecessary = new MathContext(4, RoundingMode.UNNECESSARY);
BigDecimal resultUnnecessary = value.round(mcUnnecessary);
System.out.println("Rounded (UNNECESSARY): " + resultUnnecessary);
} catch (ArithmeticException e) {
System.out.println("Rounded (UNNECESSARY): ArithmeticException - " + e.getMessage());
}
// 远离零方向舍入
MathContext mcUp = new MathContext(4, RoundingMode.UP);
BigDecimal resultUp = value.round(mcUp);
System.out.println("Rounded (UP): " + resultUp); // 输出: 123.5
}
}
总结
RoundingMode
枚举提供了丰富的舍入模式来处理 BigDecimal
运算的舍入行为。通过详细了解和掌握这些方法,您可以在 Java 编程中灵活处理需要舍入操作的计算需求。
- 枚举常量: 定义了多种舍入模式。
- 获取枚举常量的方法:
values()
,valueOf(String name)
,valueOf(int rm)
。 - 用途和应用场景: 财务计算、科学计算、数据处理等。
- 综合示例: 展示了各种舍入模式的实际应用。
MathContext
MathContext
类用于指定 BigDecimal
算术运算的上下文环境,包括精度和舍入模式。它帮助控制计算的精确度和舍入行为。下面我将详细讲解 MathContext
类中的所有方法,并按照用途、功能、目的、作用、使用场景等方面进行分类整理,并提供相应的测试用例。
一、构造方法
1. MathContext(int setPrecision)
功能: 创建一个指定精度的 MathContext
对象,舍入模式为 RoundingMode.HALF_UP
。
参数:
setPrecision
- 精度(有效数字的个数)。
示例:
import java.math.MathContext;
public class MathContextExample {
public static void main(String[] args) {
MathContext mc = new MathContext(5);
System.out.println("MathContext with precision 5: " + mc); // 输出: precision=5 roundingMode=HALF_UP
}
}
2. MathContext(int setPrecision, RoundingMode setRoundingMode)
功能: 创建一个具有指定精度和舍入模式的 MathContext
对象。
参数:
setPrecision
- 精度(有效数字的个数)。setRoundingMode
- 舍入模式。
示例:
import java.math.MathContext;
import java.math.RoundingMode;
public class MathContextExample {
public static void main(String[] args) {
MathContext mc = new MathContext(5, RoundingMode.FLOOR);
System.out.println("MathContext with precision 5 and rounding mode FLOOR: " + mc); // 输出: precision=5 roundingMode=FLOOR
}
}
3. MathContext(String val)
功能: 通过字符串表示来创建一个 MathContext
对象。
参数:
val
-MathContext
的字符串表示形式。
示例:
import java.math.MathContext;
public class MathContextExample {
public static void main(String[] args) {
MathContext mc = new MathContext("precision=5 roundingMode=HALF_UP");
System.out.println("MathContext from string: " + mc); // 输出: precision=5 roundingMode=HALF_UP
}
}
二、常用常量
1. MathContext.DECIMAL32
功能: 常量 MathContext
对象,表示 precision=7
和 roundingMode=HALF_EVEN
。
示例:
import java.math.MathContext;
public class MathContextConstantsExample {
public static void main(String[] args) {
MathContext mc = MathContext.DECIMAL32;
System.out.println("MathContext DECIMAL32: " + mc); // 输出: precision=7 roundingMode=HALF_EVEN
}
}
2. MathContext.DECIMAL64
功能: 常量 MathContext
对象,表示 precision=16
和 roundingMode=HALF_EVEN
。
示例:
import java.math.MathContext;
public class MathContextConstantsExample {
public static void main(String[] args) {
MathContext mc = MathContext.DECIMAL64;
System.out.println("MathContext DECIMAL64: " + mc); // 输出: precision=16 roundingMode=HALF_EVEN
}
}
3. MathContext.DECIMAL128
功能: 常量 MathContext
对象,表示 precision=34
和 roundingMode=HALF_EVEN
。
示例:
import java.math.MathContext;
public class MathContextConstantsExample {
public static void main(String[] args) {
MathContext mc = MathContext.DECIMAL128;
System.out.println("MathContext DECIMAL128: " + mc); // 输出: precision=34 roundingMode=HALF_EVEN
}
}
4. MathContext.UNLIMITED
功能: 常量 MathContext
对象,表示不受限制的数学上下文(即无限精度)。
示例:
import java.math.MathContext;
public class MathContextConstantsExample {
public static void main(String[] args) {
MathContext mc = MathContext.UNLIMITED;
System.out.println("MathContext UNLIMITED: " + mc); // 输出: precision=0 roundingMode=HALF_UP
}
}
三、常用方法
1. int getPrecision()
功能: 返回此 MathContext
对象的精度。
返回值: int
- 精度(有效数字的个数)。
示例:
import java.math.MathContext;
public class MathContextMethodsExample {
public static void main(String[] args) {
MathContext mc = new MathContext(5);
int precision = mc.getPrecision();
System.out.println("Precision: " + precision); // 输出: 5
}
}
2. RoundingMode getRoundingMode()
功能: 返回此 MathContext
对象的舍入模式。
返回值: RoundingMode
- 舍入模式。
示例:
import java.math.MathContext;
import java.math.RoundingMode;
public class MathContextMethodsExample {
public static void main(String[] args) {
MathContext mc = new MathContext(5, RoundingMode.FLOOR);
RoundingMode roundingMode = mc.getRoundingMode();
System.out.println("Rounding mode: " + roundingMode); // 输出: FLOOR
}
}
3. String toString()
功能: 返回此 MathContext
对象的字符串表示形式。
返回值: String
- 此 MathContext
对象的字符串表示形式。
示例:
import java.math.MathContext;
public class MathContextMethodsExample {
public static void main(String[] args) {
MathContext mc = new MathContext(5, java.math.RoundingMode.FLOOR);
String str = mc.toString();
System.out.println("MathContext string representation: " + str); // 输出: precision=5 roundingMode=FLOOR
}
}
4. boolean equals(Object x)
功能: 比较此 MathContext
与指定对象的相等性。
参数:
x
- 要比较的对象。
返回值: boolean
- 如果指定对象是与此 MathContext
值相同的 MathContext
,则返回 true
。
示例:
import java.math.MathContext;
public class MathContextMethodsExample {
public static void main(String[] args) {
MathContext mc1 = new MathContext(5, java.math.RoundingMode.FLOOR);
MathContext mc2 = new MathContext(5, java.math.RoundingMode.FLOOR);
boolean isEqual = mc1.equals(mc2);
System.out.println("Equals: " + isEqual); // 输出: true
}
}
5. int hashCode()
功能: 返回此 MathContext
的哈希码。
返回值: int
- 此 MathContext
的哈希码。
示例:
import java.math.MathContext;
public class MathContextMethodsExample {
public static void main(String[] args) {
MathContext mc = new MathContext(5, java.math.RoundingMode.FLOOR);
int hashCode = mc.hashCode();
System.out.println("Hash code: " + hashCode); // 输出: 哈希码值
}
}
四、使用场景和应用
- 高精度计算: 在涉及金融、科学计算等领域时,使用
MathContext
可以确保计算的精确度和结果的一致性。 - 控制舍入行为: 在对
BigDecimal
进行除法等运算时,控制舍入行为以避免不必要的误差。 - 统一设置计算精度: 在整个应用程序中统一设置计算精度和舍入模式,以确保计算结果的一致性。
五、综合示例
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
public class MathContextComprehensiveExample {
public static void main(String[] args) {
// 使用 MathContext.DECIMAL32 进行计算
MathContext mc32 = MathContext.DECIMAL32;
BigDecimal bd1 = new BigDecimal("1234.56789");
BigDecimal result1 = bd1.divide(new BigDecimal("3"), mc32);
System.out.println("DECIMAL32: " + result1); // 输出: 411.52264
// 自定义 MathContext
MathContext mcCustom = new MathContext(5, RoundingMode.CEILING);
BigDecimal bd2 = new BigDecimal("1234.56789");
BigDecimal result2 = bd2.divide(new BigDecimal("3"), mcCustom);
System.out.println("Custom MathContext: " + result2); // 输出: 411.53
// 获取 MathContext 的属性
int precision = mcCustom.getPrecision();
RoundingMode roundingMode = mcCustom.getRoundingMode();
System.out.println("Custom MathContext Precision: " + precision); // 输出: 5
System.out.println("Custom MathContext Rounding Mode: " + roundingMode); // 输出: CEILING
// 比较两个 MathContext 对象
MathContext mcEqual = new MathContext(5, RoundingMode.CEILING);
boolean isEqual = mcCustom.equals(mcEqual);
System.out.println("Equals: " + isEqual); // 输出: true
// Hash code
int hashCode = mcCustom.hashCode();
System.out.println("Hash code: " + hashCode); // 输出: 哈希码值
}
}
总结
MathContext
类提供了丰富的功能来控制 BigDecimal
运算的精确度和舍入行为,通过详细了解和掌握这些方法,您可以在 Java 编程中灵活处理各种高精度计算需求。
- 构造方法: 创建
MathContext
对象的不同方式。 - 常用常量: 预定义的
MathContext
常量。 - 常用方法: 获取
MathContext
对象的属性、比较和哈希码等。 - 使用场景: 高精度计算、控制舍入行为和统一设置计算精度等。
BigInteger
BigInteger
类是 Java 中用于处理任意精度整数运算的类。它不仅可以表示非常大的整数,还提供了丰富的运算方法,包括基本的算术运算、位运算、比较运算、取模运算、素数运算等。下面我们将详细讲解 BigInteger
类中的所有方法,并按照用途、功能、目的、作用、使用场景等方面进行分类整理,并给出相应的测试用例。
一、构造方法与常用常量
1. 构造方法
BigInteger(String val)
: 通过将指定的字符串解释为十进制整数来创建一个BigInteger
对象。BigInteger(String val, int radix)
: 通过将指定基数的字符串解释为整数来创建一个BigInteger
对象。BigInteger(byte[] val)
: 通过将包含大端字节顺序的指定字节数组解释为二进制补码表示的整数来创建一个BigInteger
对象。BigInteger(int signum, byte[] magnitude)
: 通过指定符号和包含大端字节顺序的指定字节数组的绝对值来创建一个BigInteger
对象。BigInteger(int numBits, Random rnd)
: 创建一个随机生成的BigInteger
,其值均匀分布在0
到(2^numBits - 1)
的范围内。BigInteger(int bitLength, int certainty, Random rnd)
: 返回一个可能是素数的BigInteger
,其值均匀分布在0
到(2^bitLength - 1)
的范围内。
示例:
import java.math.BigInteger;
import java.util.Random;
public class BigIntegerExample {
public static void main(String[] args) {
// 通过字符串创建
BigInteger bi1 = new BigInteger("123456789");
System.out.println("BigInteger (String): " + bi1); // 输出: 123456789
// 通过指定基数的字符串创建
BigInteger bi2 = new BigInteger("1010", 2); // 二进制表示
System.out.println("BigInteger (Binary String): " + bi2); // 输出: 10
// 通过字节数组创建
BigInteger bi3 = new BigInteger(new byte[]{1, 2, 3, 4});
System.out.println("BigInteger (Byte Array): " + bi3); // 输出: 16909060
// 通过指定符号和字节数组创建
BigInteger bi4 = new BigInteger(-1, new byte[]{1, 2, 3, 4});
System.out.println("BigInteger (Signum and Byte Array): " + bi4); // 输出: -16909060
// 通过随机数创建
Random rnd = new Random();
BigInteger bi5 = new BigInteger(5, rnd);
System.out.println("BigInteger (Random): " + bi5);
// 通过可能是素数的值创建
BigInteger bi6 = new BigInteger(5, 10, rnd);
System.out.println("BigInteger (Probable Prime): " + bi6);
}
}
2. 常用常量
BigInteger.ZERO
: 常量 BigInteger 值为 0。BigInteger.ONE
: 常量 BigInteger 值为 1。BigInteger.TEN
: 常量 BigInteger 值为 10。
示例:
import java.math.BigInteger;
public class BigIntegerConstantsExample {
public static void main(String[] args) {
System.out.println("BigInteger ZERO: " + BigInteger.ZERO); // 输出: 0
System.out.println("BigInteger ONE: " + BigInteger.ONE); // 输出: 1
System.out.println("BigInteger TEN: " + BigInteger.TEN); // 输出: 10
}
}
二、基本算术运算
1. BigInteger add(BigInteger val)
功能: 返回一个 BigInteger
,其值为 (this + val)
。
参数:
val
- 要加到此BigInteger
的值。
返回值: BigInteger
- this + val
的值。
示例:
import java.math.BigInteger;
public class BigIntegerArithmeticExample {
public static void main(String[] args) {
BigInteger bi1 = new BigInteger("123");
BigInteger bi2 = new BigInteger("456");
BigInteger result = bi1.add(bi2);
System.out.println("Addition: " + result); // 输出: 579
}
}
2. BigInteger subtract(BigInteger val)
功能: 返回一个 BigInteger
,其值为 (this - val)
。
参数:
val
- 要从此BigInteger
中减去的值。
返回值: BigInteger
- this - val
的值。
示例:
import java.math.BigInteger;
public class BigIntegerArithmeticExample {
public static void main(String[] args) {
BigInteger bi1 = new BigInteger("456");
BigInteger bi2 = new BigInteger("123");
BigInteger result = bi1.subtract(bi2);
System.out.println("Subtraction: " + result); // 输出: 333
}
}
3. BigInteger multiply(BigInteger val)
功能: 返回一个 BigInteger
,其值为 (this * val)
。
参数:
val
- 要与此BigInteger
相乘的值。
返回值: BigInteger
- this * val
的值。
示例:
import java.math.BigInteger;
public class BigIntegerArithmeticExample {
public static void main(String[] args) {
BigInteger bi1 = new BigInteger("123");
BigInteger bi2 = new BigInteger("456");
BigInteger result = bi1.multiply(bi2);
System.out.println("Multiplication: " + result); // 输出: 56088
}
}
4. BigInteger divide(BigInteger val)
功能: 返回一个 BigInteger
,其值为 (this / val)
。
参数:
val
- 要将此BigInteger
除以的值。
返回值: BigInteger
- this / val
的值。
示例:
import java.math.BigInteger;
public class BigIntegerArithmeticExample {
public static void main(String[] args) {
BigInteger bi1 = new BigInteger("456");
BigInteger bi2 = new BigInteger("123");
BigInteger result = bi1.divide(bi2);
System.out.println("Division: " + result); // 输出: 3
}
}
5. BigInteger[] divideAndRemainder(BigInteger val)
功能: 返回一个数组,包含此 BigInteger
除以指定值后的商和余数。
参数:
val
- 要将此BigInteger
除以的值。
返回值: BigInteger[]
- 包含商和余数的数组。
示例:
import java.math.BigInteger;
public class BigIntegerArithmeticExample {
public static void main(String[] args) {
BigInteger bi1 = new BigInteger("456");
BigInteger bi2 = new BigInteger("123");
BigInteger[] result = bi1.divideAndRemainder(bi2);
System.out.println("Quotient: " + result[0]); // 输出: 3
System.out.println("Remainder: " + result[1]); // 输出: 87
}
}
6. BigInteger remainder(BigInteger val)
功能: 返回一个 BigInteger
,其值为 (this % val)
。
参数:
val
- 要将此BigInteger
除以并取余的值。
返回值: BigInteger
- this % val
的值。
示例:
import java.math.BigInteger;
public class BigIntegerArithmeticExample {
public static void main(String[] args) {
BigInteger bi1 = new BigInteger("456");
BigInteger bi2 = new BigInteger("123");
BigInteger result = bi1.remainder(bi2);
System.out.println("Remainder: " + result); // 输出: 87
}
}
三、幂运算与开方运算
1. BigInteger pow(int exponent)
功能: 返回一个 BigInteger
,其值为 (this^exponent)
。
参数:
exponent
- 指数。
返回值: BigInteger
- this^exponent
的值。
示例:
import java.math.BigInteger;
public class BigIntegerPowerExample {
public static void main(String[] args) {
BigInteger bi = new BigInteger("2");
BigInteger result = bi.pow(10);
System.out.println("Power: " + result); // 输出: 1024
}
}
2. BigInteger sqrt()
功能: 返回一个 BigInteger
,其值为 this
的整数平方根。
返回值: BigInteger
- this
的整数平方根。
示例:
import java.math.BigInteger;
public class BigIntegerSquareRootExample {
public static void main(String[] args) {
BigInteger bi = new BigInteger("1024");
BigInteger result = bi.sqrt();
System.out.println("Square root: " + result); // 输出: 32
}
}
四、比较运算
1. int compareTo(BigInteger val)
功能: 比较两个 BigInteger
对象。
参数:
val
- 要与此BigInteger
比较的值。
返回值: int
- 如果此 BigInteger
小于、等于或大于指定的 BigInteger
,则分别返回负整数、零或正整数。
示例:
import java.math.BigInteger;
public class BigIntegerComparisonExample {
public static void main(String[] args) {
BigInteger bi1 = new BigInteger("123");
BigInteger bi2 = new BigInteger("456");
int result = bi1.compareTo(bi2);
System.out.println("Comparison: " + result); // 输出: -1
}
}
2. boolean equals(Object x)
功能: 比较此 BigInteger
与指定对象是否相等。
参数:
x
- 要比较的对象。
返回值: boolean
- 如果指定对象是与此 BigInteger
值相同的 BigInteger
,则返回 true
。
示例:
import java.math.BigInteger;
public class BigIntegerComparisonExample {
public static void main(String[] args) {
BigInteger bi1 = new BigInteger("123");
BigInteger bi2 = new BigInteger("123");
boolean result = bi1.equals(bi2);
System.out.println("Equals: " + result); // 输出: true
}
}
五、取模运算
1. BigInteger mod(BigInteger m)
功能: 返回一个 BigInteger
,其值为 (this mod m)
。
参数:
m
- 模数。
返回值: BigInteger
- this mod m
的值。
示例:
import java.math.BigInteger;
public class BigIntegerModulusExample {
public static void main(String[] args) {
BigInteger bi = new BigInteger("123");
BigInteger mod = new BigInteger("10");
BigInteger result = bi.mod(mod);
System.out.println("Modulus: " + result); // 输出: 3
}
}
2. BigInteger modInverse(BigInteger m)
功能: 返回一个 BigInteger
,其值为 this
在给定模数 m
下的模逆元。
参数:
m
- 模数。
返回值: BigInteger
- this
在 m
下的模逆元。
示例:
import java.math.BigInteger;
public class BigIntegerModulusExample {
public static void main(String[] args) {
BigInteger bi = new BigInteger("3");
BigInteger mod = new BigInteger("11");
BigInteger result = bi.modInverse(mod);
System.out.println("Modular Inverse: " + result); // 输出: 4
}
}
3. BigInteger modPow(BigInteger exponent, BigInteger m)
功能: 返回一个 BigInteger
,其值为 (this^exponent mod m)
。
参数:
exponent
- 指数。m
- 模数。
返回值: BigInteger
- this^exponent mod m
的值。
示例:
import java.math.BigInteger;
public class BigIntegerModulusExample {
public static void main(String[] args) {
BigInteger bi = new BigInteger("3");
BigInteger exponent = new BigInteger("2");
BigInteger mod = new BigInteger("5");
BigInteger result = bi.modPow(exponent, mod);
System.out.println("Modular Exponentiation: " + result); // 输出: 4
}
}
六、位运算
1. BigInteger and(BigInteger val)
功能: 返回一个 BigInteger
,其值为 this & val
。
参数:
val
- 要与此BigInteger
执行按位与操作的值。
返回值: BigInteger
- this & val
的值。
示例:
import java.math.BigInteger;
public class BigIntegerBitwiseExample {
public static void main(String[] args) {
BigInteger bi1 = new BigInteger("12"); // 1100 二进制
BigInteger bi2 = new BigInteger("10"); // 1010 二进制
BigInteger result = bi1.and(bi2);
System.out.println("Bitwise AND: " + result); // 输出: 8 (1000 二进制)
}
}
2. BigInteger or(BigInteger val)
功能: 返回一个 BigInteger
,其值为 this | val
。
参数:
val
- 要与此BigInteger
执行按位或操作的值。
返回值: BigInteger
- this | val
的值。
示例:
import java.math.BigInteger;
public class BigIntegerBitwiseExample {
public static void main(String[] args) {
BigInteger bi1 = new BigInteger("12"); // 1100 二进制
BigInteger bi2 = new BigInteger("10"); // 1010 二进制
BigInteger result = bi1.or(bi2);
System.out.println("Bitwise OR: " + result); // 输出: 14 (1110 二进制)
}
}
3. BigInteger xor(BigInteger val)
功能: 返回一个 BigInteger
,其值为 this ^ val
。
参数:
val
- 要与此BigInteger
执行按位异或操作的值。
返回值: BigInteger
- this ^ val
的值。
示例:
import java.math.BigInteger;
public class BigIntegerBitwiseExample {
public static void main(String[] args) {
BigInteger bi1 = new BigInteger("12"); // 1100 二进制
BigInteger bi2 = new BigInteger("10"); // 1010 二进制
BigInteger result = bi1.xor(bi2);
System.out.println("Bitwise XOR: " + result); // 输出: 6 (0110 二进制)
}
}
4. BigInteger not()
功能: 返回一个 BigInteger
,其值为 ~this
。
返回值: BigInteger
- ~this
的值。
示例:
import java.math.BigInteger;
public class BigIntegerBitwiseExample {
public static void main(String[] args) {
BigInteger bi = new BigInteger("12"); // 1100 二进制
BigInteger result = bi.not();
System.out.println("Bitwise NOT: " + result); // 输出: -13 (补码表示)
}
}
5. BigInteger shiftLeft(int n)
功能: 返回一个 BigInteger
,其值为 this << n
。
参数:
n
- 要左移的位数。
返回值: BigInteger
- this << n
的值。
示例:
import java.math.BigInteger;
public class BigIntegerBitwiseExample {
public static void main(String[] args) {
BigInteger bi = new BigInteger("12"); // 1100 二进制
BigInteger result = bi.shiftLeft(2);
System.out.println("Shift Left: " + result); // 输出: 48 (110000 二进制)
}
}
6. BigInteger shiftRight(int n)
功能: 返回一个 BigInteger
,其值为 this >> n
。
参数:
n
- 要右移的位数。
返回值: BigInteger
- this >> n
的值。
示例:
import java.math.BigInteger;
public class BigIntegerBitwiseExample {
public static void main(String[] args) {
BigInteger bi = new BigInteger("12"); // 1100 二进制
BigInteger result = bi.shiftRight(2);
System.out.println("Shift Right: " + result); // 输出: 3 (0011 二进制)
}
}
七、其他数学运算
当然,接下来我们继续详细讲解 BigInteger
类中的其他方法,并继续分类整理和提供测试用例。
1. BigInteger abs()
功能: 返回一个 BigInteger
,其值为此 BigInteger
的绝对值。
返回值: BigInteger
- 绝对值。
示例:
import java.math.BigInteger;
public class BigIntegerMathExample {
public static void main(String[] args) {
BigInteger bi = new BigInteger("-123");
BigInteger result = bi.abs();
System.out.println("Absolute value: " + result); // 输出: 123
}
}
2. BigInteger negate()
功能: 返回一个 BigInteger
,其值为此 BigInteger
的相反数。
返回值: BigInteger
- 相反数。
示例:
import java.math.BigInteger;
public class BigIntegerMathExample {
public static void main(String[] args) {
BigInteger bi = new BigInteger("123");
BigInteger result = bi.negate();
System.out.println("Negate: " + result); // 输出: -123
}
}
3. BigInteger gcd(BigInteger val)
功能: 返回一个 BigInteger
,其值为此 BigInteger
与指定值的最大公约数。
参数:
val
- 另一个BigInteger
值。
返回值: BigInteger
- 最大公约数。
示例:
import java.math.BigInteger;
public class BigIntegerMathExample {
public static void main(String[] args) {
BigInteger bi1 = new BigInteger("48");
BigInteger bi2 = new BigInteger("18");
BigInteger result = bi1.gcd(bi2);
System.out.println("GCD: " + result); // 输出: 6
}
}
4. BigInteger max(BigInteger val)
功能: 返回一个 BigInteger
,其值为此 BigInteger
和指定值中的最大值。
参数:
val
- 另一个BigInteger
值。
返回值: BigInteger
- 最大值。
示例:
import java.math.BigInteger;
public class BigIntegerMathExample {
public static void main(String[] args) {
BigInteger bi1 = new BigInteger("123");
BigInteger bi2 = new BigInteger("456");
BigInteger result = bi1.max(bi2);
System.out.println("Max: " + result); // 输出: 456
}
}
5. BigInteger min(BigInteger val)
功能: 返回一个 BigInteger
,其值为此 BigInteger
和指定值中的最小值。
参数:
val
- 另一个BigInteger
值。
返回值: BigInteger
- 最小值。
示例:
import java.math.BigInteger;
public class BigIntegerMathExample {
public static void main(String[] args) {
BigInteger bi1 = new BigInteger("123");
BigInteger bi2 = new BigInteger("456");
BigInteger result = bi1.min(bi2);
System.out.println("Min: " + result); // 输出: 123
}
}
6. BigInteger modInverse(BigInteger m)
功能: 返回一个 BigInteger
,其值为此 BigInteger
在给定模数下的模逆元。
参数:
m
- 模数。
返回值: BigInteger
- 模逆元。
示例:
import java.math.BigInteger;
public class BigIntegerMathExample {
public static void main(String[] args) {
BigInteger bi = new BigInteger("3");
BigInteger mod = new BigInteger("11");
BigInteger result = bi.modInverse(mod);
System.out.println("Modular Inverse: " + result); // 输出: 4
}
}
7. BigInteger modPow(BigInteger exponent, BigInteger m)
功能: 返回一个 BigInteger
,其值为此 BigInteger
的 exponent
次幂对 m
取模的结果。
参数:
exponent
- 指数。m
- 模数。
返回值: BigInteger
- 模幂运算的结果。
示例:
import java.math.BigInteger;
public class BigIntegerMathExample {
public static void main(String[] args) {
BigInteger base = new BigInteger("2");
BigInteger exponent = new BigInteger("10");
BigInteger mod = new BigInteger("1000");
BigInteger result = base.modPow(exponent, mod);
System.out.println("Modular Exponentiation: " + result); // 输出: 24
}
}
八、随机数与素数运算
1. BigInteger nextProbablePrime()
功能: 返回一个最小的大于此 BigInteger
的素数。
返回值: BigInteger
- 下一个可能是素数的数。
示例:
import java.math.BigInteger;
public class BigIntegerPrimeExample {
public static void main(String[] args) {
BigInteger bi = new BigInteger("100");
BigInteger result = bi.nextProbablePrime();
System.out.println("Next probable prime: " + result); // 输出: 101
}
}
2. boolean isProbablePrime(int certainty)
功能: 判断此 BigInteger
是否可能为素数。
参数:
certainty
- 确信度(越大越准确)。
返回值: boolean
- 如果此 BigInteger
可能为素数,则返回 true
。
示例:
import java.math.BigInteger;
public class BigIntegerPrimeExample {
public static void main(String[] args) {
BigInteger bi = new BigInteger("101");
boolean result = bi.isProbablePrime(10);
System.out.println("Is probable prime: " + result); // 输出: true
}
}
3. BigInteger probablePrime(int bitLength, Random rnd)
功能: 返回一个指定位长的可能是素数的 BigInteger
。
参数:
bitLength
- 位长。rnd
- 随机数生成器。
返回值: BigInteger
- 可能是素数的数。
示例:
import java.math.BigInteger;
import java.util.Random;
public class BigIntegerPrimeExample {
public static void main(String[] args) {
Random rnd = new Random();
BigInteger result = BigInteger.probablePrime(10, rnd);
System.out.println("Probable prime: " + result);
}
}
九、转换方法
1. byte[] toByteArray()
功能: 返回一个包含此 BigInteger
的二进制补码表示形式的字节数组。
返回值: byte[]
- 二进制补码表示形式的字节数组。
示例:
import java.math.BigInteger;
import java.util.Arrays;
public class BigIntegerConversionExample {
public static void main(String[] args) {
BigInteger bi = new BigInteger("123456789");
byte[] byteArray = bi.toByteArray();
//每个元素表示1字节(8bit)的二进制补码
System.out.println("Byte array: " + Arrays.toString(byteArray)); // 输出: [7, 91, -51, 21]
}
}
2. String toString()
功能: 返回此 BigInteger
的十进制字符串表示形式。
返回值: String
- 十进制字符串表示形式。
示例:
import java.math.BigInteger;
public class BigIntegerConversionExample {
public static void main(String[] args) {
BigInteger bi = new BigInteger("123456789");
String str = bi.toString();
System.out.println("String representation: " + str); // 输出: 123456789
}
}
3. String toString(int radix)
功能: 返回此 BigInteger
的指定基数的字符串表示形式。
参数:
radix
- 基数(2 到 36)。
返回值: String
- 指定基数的字符串表示形式。
示例:
import java.math.BigInteger;
public class BigIntegerConversionExample {
public static void main(String[] args) {
BigInteger bi = new BigInteger("123456789");
String binaryStr = bi.toString(2); // 二进制表示
System.out.println("Binary representation: " + binaryStr); // 输出: 111010110111100110100010101
}
}
4. int intValue()
功能: 以 int
类型返回此 BigInteger
的值。
返回值: int
- 此 BigInteger
的 int
值。
示例:
import java.math.BigInteger;
public class BigIntegerConversionExample {
public static void main(String[] args) {
BigInteger bi = new BigInteger("123456789");
int intValue = bi.intValue();
System.out.println("Int value: " + intValue); // 输出: 123456789
}
}
5. long longValue()
功能: 以 long
类型返回此 BigInteger
的值。
返回值: long
- 此 BigInteger
的 long
值。
示例:
import java.math.BigInteger;
public class BigIntegerConversionExample {
public static void main(String[] args) {
BigInteger bi = new BigInteger("123456789");
long longValue = bi.longValue();
System.out.println("Long value: " + longValue); // 输出: 123456789
}
}
6. float floatValue()
功能: 以 float
类型返回此 BigInteger
的值。
返回值: float
- 此 BigInteger
的 float
值。
示例:
import java.math.BigInteger;
public class BigIntegerConversionExample {
public static void main(String[] args) {
BigInteger bi = new BigInteger("123456789");
float floatValue = bi.floatValue();
System.out.println("Float value: " + floatValue); // 输出: 1.23456792E8
}
}
7. double doubleValue()
功能: 以 double
类型返回此 BigInteger
的值。
返回值: double
- 此 BigInteger
的 double
值。
示例:
import java.math.BigInteger;
public class BigIntegerConversionExample {
public static void main(String[] args) {
BigInteger bi = new BigInteger("123456789");
double doubleValue = bi.doubleValue();
System.out.println("Double value: " + doubleValue); // 输出: 1.23456789E8
}
}
十、哈希码与同等性
1. int hashCode()
功能: 返回此 BigInteger
的哈希码。
返回值: int
- 此 BigInteger
的哈希码。
示例:
import java.math.BigInteger;
public class BigIntegerHashCodeExample {
public static void main(String[] args) {
BigInteger bi = new BigInteger("123456789");
int hashCode = bi.hashCode();
System.out.println("Hash code: " + hashCode); // 输出: 哈希码值
}
}
2. boolean equals(Object x)
功能: 比较此 BigInteger
与指定对象的相等性。
参数:
x
- 要比较的对象。
返回值: boolean
- 如果指定对象是与此 BigInteger
值相同的 BigInteger
,则返回 true
。
示例:
import java.math.BigInteger;
public class BigIntegerEqualityExample {
public static void main(String[] args) {
BigInteger bi1 = new BigInteger("123456789");
BigInteger bi2 = new BigInteger("123456789");
boolean isEqual = bi1.equals(bi2);
System.out.println("Equals: " + isEqual); // 输出: true
}
}
总结
BigInteger
类提供了丰富的功能来处理任意精度的整数运算。通过详细了解和掌握这些方法,您可以在 Java 编程中灵活处理各种大数运算需求。
- 基本算术运算:
add
,subtract
,multiply
,divide
,remainder
,divideAndRemainder
- 幂运算与开方运算:
pow
,sqrt
- 比较运算:
compareTo
,equals
- 取模运算:
mod
,modInverse
,modPow
- 位运算:
and
,or
,xor
,not
,shiftLeft
,shiftRight
- 其他数学运算:
abs
,negate
,gcd
,max
,min
- 随机数与素数运算:
nextProbablePrime
,isProbablePrime
,probablePrime
- 转换方法:
toByteArray
,toString
,intValue
,longValue
,floatValue
,doubleValue
- 哈希码与同等性:
hashCode
,equals
BigDecimal
BigDecimal
类是 Java 中用于处理任意精度的十进制浮点数运算的类。它不仅可以表示非常大的数值,还提供了丰富的运算方法,包括基本的算术运算、比较运算、取整运算、舍入运算等。下面我们将详细讲解 BigDecimal
类中的所有方法,并按照用途、功能、目的、作用、使用场景等方面进行分类整理,并给出相应的测试用例。
一、构造方法与常用常量
1. 构造方法
BigDecimal(String val)
: 通过将指定的字符串解释为十进制数来创建一个BigDecimal
对象。BigDecimal(double val)
: 通过将指定的双精度浮点数解释为十进制数来创建一个BigDecimal
对象。BigDecimal(int val)
: 通过将指定的整数解释为十进制数来创建一个BigDecimal
对象。BigDecimal(long val)
: 通过将指定的长整数解释为十进制数来创建一个BigDecimal
对象。BigDecimal(BigInteger val)
: 通过将指定的大整数解释为十进制数来创建一个BigDecimal
对象。BigDecimal(BigInteger unscaledVal, int scale)
: 通过指定的大整数和比例来创建一个BigDecimal
对象。BigDecimal(char[] in)
: 通过将指定的字符数组解释为十进制数来创建一个BigDecimal
对象。BigDecimal(char[] in, int offset, int len)
: 通过将指定的字符数组的一部分解释为十进制数来创建一个BigDecimal
对象。
示例:
import java.math.BigDecimal;
public class BigDecimalExample {
public static void main(String[] args) {
// 通过字符串创建
BigDecimal bd1 = new BigDecimal("123456789.123456789");
System.out.println("BigDecimal (String): " + bd1); // 输出: 123456789.123456789
// 通过双精度浮点数创建
BigDecimal bd2 = new BigDecimal(123456789.123456789);
System.out.println("BigDecimal (double): " + bd2); // 输出: 123456789.123456789
// 通过整数创建
BigDecimal bd3 = new BigDecimal(123456789);
System.out.println("BigDecimal (int): " + bd3); // 输出: 123456789
// 通过长整数创建
BigDecimal bd4 = new BigDecimal(123456789L);
System.out.println("BigDecimal (long): " + bd4); // 输出: 123456789
// 通过大整数创建
BigDecimal bd5 = new BigDecimal(new BigInteger("123456789"));
System.out.println("BigDecimal (BigInteger): " + bd5); // 输出: 123456789
// 通过大整数和比例创建
BigDecimal bd6 = new BigDecimal(new BigInteger("123456789"), 5);
System.out.println("BigDecimal (BigInteger, scale): " + bd6); // 输出: 1234.56789
// 通过字符数组创建
BigDecimal bd7 = new BigDecimal(new char[]{'1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '1', '2', '3', '4', '5', '6', '7', '8', '9'});
System.out.println("BigDecimal (char[]): " + bd7); // 输出: 123456789.123456789
// 通过字符数组的一部分创建
BigDecimal bd8 = new BigDecimal(new char[]{'1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '1', '2', '3', '4', '5', '6', '7', '8', '9'}, 0, 10);
System.out.println("BigDecimal (char[], offset, len): " + bd8); // 输出: 1234567890
}
}
2. 常用常量
BigDecimal.ZERO
: 常量 BigDecimal 值为 0。BigDecimal.ONE
: 常量 BigDecimal 值为 1。BigDecimal.TEN
: 常量 BigDecimal 值为 10。
示例:
import java.math.BigDecimal;
public class BigDecimalConstantsExample {
public static void main(String[] args) {
System.out.println("BigDecimal ZERO: " + BigDecimal.ZERO); // 输出: 0
System.out.println("BigDecimal ONE: " + BigDecimal.ONE); // 输出: 1
System.out.println("BigDecimal TEN: " + BigDecimal.TEN); // 输出: 10
}
}
二、基本算术运算
1. BigDecimal add(BigDecimal augend)
功能: 返回一个 BigDecimal
,其值为 (this + augend)
。
参数:
augend
- 要加到此BigDecimal
的值。
返回值: BigDecimal
- this + augend
的值。
示例:
import java.math.BigDecimal;
public class BigDecimalArithmeticExample {
public static void main(String[] args) {
BigDecimal bd1 = new BigDecimal("123.45");
BigDecimal bd2 = new BigDecimal("67.89");
BigDecimal result = bd1.add(bd2);
System.out.println("Addition: " + result); // 输出: 191.34
}
}
2. BigDecimal subtract(BigDecimal subtrahend)
功能: 返回一个 BigDecimal
,其值为 (this - subtrahend)
。
参数:
subtrahend
- 要从此BigDecimal
中减去的值。
返回值: BigDecimal
- this - subtrahend
的值。
示例:
import java.math.BigDecimal;
public class BigDecimalArithmeticExample {
public static void main(String[] args) {
BigDecimal bd1 = new BigDecimal("123.45");
BigDecimal bd2 = new BigDecimal("67.89");
BigDecimal result = bd1.subtract(bd2);
System.out.println("Subtraction: " + result); // 输出: 55.56
}
}
3. BigDecimal multiply(BigDecimal multiplicand)
功能: 返回一个 BigDecimal
,其值为 (this * multiplicand)
。
参数:
multiplicand
- 要与此BigDecimal
相乘的值。
返回值: BigDecimal
- this * multiplicand
的值。
示例:
import java.math.BigDecimal;
public class BigDecimalArithmeticExample {
public static void main(String[] args) {
BigDecimal bd1 = new BigDecimal("123.45");
BigDecimal bd2 = new BigDecimal("67.89");
BigDecimal result = bd1.multiply(bd2);
System.out.println("Multiplication: " + result); // 输出: 8372.1205
}
}
4. BigDecimal divide(BigDecimal divisor)
功能: 返回一个 BigDecimal
,其值为 (this / divisor)
。
参数:
divisor
- 要将此BigDecimal
除以的值。
返回值: BigDecimal
- this / divisor
的值。
示例:
import java.math.BigDecimal;
public class BigDecimalArithmeticExample {
public static void main(String[] args) {
BigDecimal bd1 = new BigDecimal("123.45");
BigDecimal bd2 = new BigDecimal("67.89");
BigDecimal result = bd1.divide(bd2);
System.out.println("Division: " + result); // 输出: 1.818382677861246
}
}
5. BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode)
功能: 返回一个 BigDecimal
,其值为 (this / divisor)
,并指定结果的小数位数和舍入模式。
参数:
divisor
- 要将此BigDecimal
除以的值。scale
- 结果的小数位数。roundingMode
- 舍入模式。
返回值: BigDecimal
- this / divisor
的值,并按指定的小数位数和舍入模式进行舍入。
示例:
import java.math.BigDecimal;
import java.math.RoundingMode;
public class BigDecimalArithmeticExample {
public static void main(String[] args) {
BigDecimal bd1 = new BigDecimal("123.45");
BigDecimal bd2 = new BigDecimal("67.89");
BigDecimal result = bd1.divide(bd2, 2, RoundingMode.HALF_UP);
System.out.println("Division with scale and rounding mode: " + result); // 输出: 1.82
}
}
三、比较运算
1. int compareTo(BigDecimal val)
功能: 比较两个 BigDecimal
对象。
参数:
val
- 要与此BigDecimal
比较的值。
返回值: int
- 如果此 BigDecimal
小于、等于或大于指定的 BigDecimal
,则分别返回负整数、零或正整数。
示例:
import java.math.BigDecimal;
public class BigDecimalComparisonExample {
public static void main(String[] args) {
BigDecimal bd1 = new BigDecimal("123.45");
BigDecimal bd2 = new BigDecimal("67.89");
int result = bd1.compareTo(bd2);
System.out.println("Comparison: " + result); // 输出: 1
}
}
2. boolean equals(Object x)
功能: 比较此 BigDecimal
与指定对象是否相等。
参数:
x
- 要比较的对象。
返回值: boolean
- 如果指定对象是与此 BigDecimal
值相同的 BigDecimal
,则返回 true
。
示例:
import java.math.BigDecimal;
public class BigDecimalComparisonExample {
public static void main(String[] args) {
BigDecimal bd1 = new BigDecimal("123.45");
BigDecimal bd2 = new BigDecimal("123.45");
boolean result = bd1.equals(bd2);
System.out.println("Equals: " + result); // 输出: true
}
}
四、取整运算
1. BigDecimal setScale(int newScale, RoundingMode roundingMode)
功能: 返回一个 BigDecimal
,其值为此 BigDecimal
的值,并指定新的小数位数和舍入模式。
参数:
newScale
- 新的小数位数。roundingMode
- 舍入模式。
返回值: BigDecimal
- 新的 BigDecimal
对象。
示例:
import java.math.BigDecimal;
import java.math.RoundingMode;
public class BigDecimalRoundingExample {
public static void main(String[] args) {
BigDecimal bd = new BigDecimal("123.456");
BigDecimal result = bd.setScale(2, RoundingMode.HALF_UP);
System.out.println("Set scale: " + result); // 输出: 123.46
}
}
2. BigDecimal stripTrailingZeros()
功能: 返回一个 BigDecimal
,其值与此 BigDecimal
的值相同,但去掉了尾部的零。
返回值: BigDecimal
- 新的 BigDecimal
对象。
示例:
import java.math.BigDecimal;
public class BigDecimalRoundingExample {
public static void main(String[] args) {
BigDecimal bd = new BigDecimal("123.4500");
BigDecimal result = bd.stripTrailingZeros();
System.out.println("Strip trailing zeros: " + result); // 输出: 123.45
}
}
五、其他数学运算
1. BigDecimal abs()
功能: 返回一个 BigDecimal
,其值为此 BigDecimal
的绝对值。
返回值: BigDecimal
- 绝对值。
示例:
import java.math.BigDecimal;
public class BigDecimalMathExample {
public static void main(String[] args) {
BigDecimal bd = new BigDecimal("-123.45");
BigDecimal result = bd.abs();
System.out.println("Absolute value: " + result); // 输出: 123.45
}
}
2. BigDecimal negate()
功能: 返回一个 BigDecimal
,其值为此 BigDecimal
的相反数。
返回值: BigDecimal
- 相反数。
示例:
import java.math.BigDecimal;
public class BigDecimalMathExample {
public static void main(String[] args) {
BigDecimal bd = new BigDecimal("123.45");
BigDecimal result = bd.negate();
System.out.println("Negate: " + result); // 输出: -123.45
}
}
3. BigDecimal pow(int n)
功能: 返回一个 BigDecimal
,其值为此 BigDecimal
的 n
次幂。
参数:
n
- 指数。
返回值: BigDecimal
- this^n
的值。
示例:
import java.math.BigDecimal;
public class BigDecimalMathExample {
public static void main(String[] args) {
BigDecimal bd = new BigDecimal("2");
BigDecimal result = bd.pow(10);
System.out.println("Power: " + result); // 输出: 1024
}
}
4. BigDecimal max(BigDecimal val)
功能: 返回一个 BigDecimal
,其值为此 BigDecimal
和指定值中的最大值。
参数:
val
- 另一个BigDecimal
值。
返回值: BigDecimal
- 最大值。
示例:
import java.math.BigDecimal;
public class BigDecimalMathExample {
public static void main(String[] args) {
BigDecimal bd1 = new BigDecimal("123.45");
BigDecimal bd2 = new BigDecimal("67.89");
BigDecimal result = bd1.max(bd2);
System.out.println("Max: " + result); // 输出: 123.45
}
}
5. BigDecimal min(BigDecimal val)
功能: 返回一个 BigDecimal
,其值为此 BigDecimal
和指定值中的最小值。
参数:
val
- 另一个BigDecimal
值。
返回值: BigDecimal
- 最小值。
示例:
import java.math.BigDecimal;
public class BigDecimalMathExample {
public static void main(String[] args) {
BigDecimal bd1 = new BigDecimal("123.45");
BigDecimal bd2 = new BigDecimal("67.89");
BigDecimal result = bd1.min(bd2);
System.out.println("Min: " + result); // 输出: 67.89
}
}
六、转换方法
当然,接下来我们继续详细讲解 BigDecimal
类中的其他方法,并继续分类整理和提供测试用例。
1. BigDecimal toBigInteger()
功能: 返回一个 BigInteger
,其值为此 BigDecimal
的整数部分。
返回值: BigInteger
- 此 BigDecimal
的整数部分。
示例:
import java.math.BigDecimal;
import java.math.BigInteger;
public class BigDecimalConversionExample {
public static void main(String[] args) {
BigDecimal bd = new BigDecimal("123.45");
BigInteger bi = bd.toBigInteger();
System.out.println("To BigInteger: " + bi); // 输出: 123
}
}
2. BigInteger toBigIntegerExact()
功能: 返回一个 BigInteger
,其值为此 BigDecimal
的整数部分(如果此 BigDecimal
有非零的小数部分,将抛出 ArithmeticException
)。
返回值: BigInteger
- 此 BigDecimal
的整数部分。
异常:
ArithmeticException
- 如果此BigDecimal
有非零的小数部分。
示例:
import java.math.BigDecimal;
import java.math.BigInteger;
public class BigDecimalConversionExample {
public static void main(String[] args) {
BigDecimal bd = new BigDecimal("123.00");
try {
BigInteger bi = bd.toBigIntegerExact();
System.out.println("To BigIntegerExact: " + bi); // 输出: 123
} catch (ArithmeticException e) {
System.out.println("ArithmeticException: " + e.getMessage());
}
}
}
3. double doubleValue()
功能: 以 double
类型返回此 BigDecimal
的值。
返回值: double
- 此 BigDecimal
的 double
值。
示例:
import java.math.BigDecimal;
public class BigDecimalConversionExample {
public static void main(String[] args) {
BigDecimal bd = new BigDecimal("123.45");
double doubleValue = bd.doubleValue();
System.out.println("Double value: " + doubleValue); // 输出: 123.45
}
}
4. float floatValue()
功能: 以 float
类型返回此 BigDecimal
的值。
返回值: float
- 此 BigDecimal
的 float
值。
示例:
import java.math.BigDecimal;
public class BigDecimalConversionExample {
public static void main(String[] args) {
BigDecimal bd = new BigDecimal("123.45");
float floatValue = bd.floatValue();
System.out.println("Float value: " + floatValue); // 输出: 123.45
}
}
5. int intValue()
功能: 以 int
类型返回此 BigDecimal
的值(可能会丢失精度)。
返回值: int
- 此 BigDecimal
的 int
值。
示例:
import java.math.BigDecimal;
public class BigDecimalConversionExample {
public static void main(String[] args) {
BigDecimal bd = new BigDecimal("123.45");
int intValue = bd.intValue();
System.out.println("Int value: " + intValue); // 输出: 123
}
}
6. int intValueExact()
功能: 以 int
类型返回此 BigDecimal
的值(如果此 BigDecimal
不能准确表示为 int
类型,则抛出 ArithmeticException
)。
返回值: int
- 此 BigDecimal
的 int
值。
异常:
ArithmeticException
- 如果此BigDecimal
的值太大或太小,无法表示为int
类型。
示例:
import java.math.BigDecimal;
public class BigDecimalConversionExample {
public static void main(String[] args) {
BigDecimal bd = new BigDecimal("123.00");
try {
int intValueExact = bd.intValueExact();
System.out.println("Int value exact: " + intValueExact); // 输出: 123
} catch (ArithmeticException e) {
System.out.println("ArithmeticException: " + e.getMessage());
}
}
}
7. long longValue()
功能: 以 long
类型返回此 BigDecimal
的值(可能会丢失精度)。
返回值: long
- 此 BigDecimal
的 long
值。
示例:
import java.math.BigDecimal;
public class BigDecimalConversionExample {
public static void main(String[] args) {
BigDecimal bd = new BigDecimal("123456789.45");
long longValue = bd.longValue();
System.out.println("Long value: " + longValue); // 输出: 123456789
}
}
8. long longValueExact()
功能: 以 long
类型返回此 BigDecimal
的值(如果此 BigDecimal
不能准确表示为 long
类型,则抛出 ArithmeticException
)。
返回值: long
- 此 BigDecimal
的 long
值。
异常:
ArithmeticException
- 如果此BigDecimal
的值太大或太小,无法表示为long
类型。
示例:
import java.math.BigDecimal;
public class BigDecimalConversionExample {
public static void main(String[] args) {
BigDecimal bd = new BigDecimal("123456789.00");
try {
long longValueExact = bd.longValueExact();
System.out.println("Long value exact: " + longValueExact); // 输出: 123456789
} catch (ArithmeticException e) {
System.out.println("ArithmeticException: " + e.getMessage());
}
}
}
9. String toString()
功能: 返回此 BigDecimal
的字符串表示形式。
返回值: String
- 此 BigDecimal
的字符串表示形式。
示例:
import java.math.BigDecimal;
public class BigDecimalConversionExample {
public static void main(String[] args) {
BigDecimal bd = new BigDecimal("123.456");
String str = bd.toString();
System.out.println("String representation: " + str); // 输出: 123.456
}
}
七、舍入运算
1. BigDecimal round(MathContext mc)
功能: 返回一个 BigDecimal
,其值为此 BigDecimal
按指定的 MathContext
进行舍入的结果。
参数:
mc
- 用于舍入的MathContext
。
返回值: BigDecimal
- 按指定的舍入规则舍入后的 BigDecimal
。
示例:
import java.math.BigDecimal;
import java.math.MathContext;
public class BigDecimalRoundingExample {
public static void main(String[] args) {
BigDecimal bd = new BigDecimal("123.456789");
MathContext mc = new MathContext(5); // 精度为 5
BigDecimal result = bd.round(mc);
System.out.println("Rounded value: " + result); // 输出: 123.46
}
}
2. BigDecimal setScale(int newScale)
功能: 返回一个 BigDecimal
,其值为此 BigDecimal
的值,并指定新的小数位数。
参数:
newScale
- 新的小数位数。
返回值: BigDecimal
- 新的 BigDecimal
对象。
示例:
import java.math.BigDecimal;
public class BigDecimalRoundingExample {
public static void main(String[] args) {
BigDecimal bd = new BigDecimal("123.456");
BigDecimal result = bd.setScale(2);
System.out.println("Set scale: " + result); // 输出: 123.46
}
}
3. BigDecimal setScale(int newScale, int roundingMode)
功能: 返回一个 BigDecimal
,其值为此 BigDecimal
的值,并指定新的小数位数和舍入模式。
参数:
newScale
- 新的小数位数。roundingMode
- 舍入模式(已废弃,建议使用RoundingMode
枚举)。
返回值: BigDecimal
- 新的 BigDecimal
对象。
示例:
import java.math.BigDecimal;
public class BigDecimalRoundingExample {
public static void main(String[] args) {
BigDecimal bd = new BigDecimal("123.456");
BigDecimal result = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
System.out.println("Set scale with rounding mode: " + result); // 输出: 123.46
}
}
4. BigDecimal setScale(int newScale, RoundingMode roundingMode)
功能: 返回一个 BigDecimal
,其值为此 BigDecimal
的值,并指定新的小数位数和舍入模式。
参数:
newScale
- 新的小数位数。roundingMode
- 舍入模式。
返回值: BigDecimal
- 新的 BigDecimal
对象。
示例:
import java.math.BigDecimal;
import java.math.RoundingMode;
public class BigDecimalRoundingExample {
public static void main(String[] args) {
BigDecimal bd = new BigDecimal("123.456");
BigDecimal result = bd.setScale(2, RoundingMode.HALF_UP);
System.out.println("Set scale with rounding mode: " + result); // 输出: 123.46
}
}
八、其他工具方法
1. BigDecimal scale()
功能: 返回此 BigDecimal
的当前比例(即小数位数)。
返回值: int
- 当前比例。
示例:
import java.math.BigDecimal;
public class BigDecimalUtilityExample {
public static void main(String[] args) {
BigDecimal bd = new BigDecimal("123.456");
int scale = bd.scale();
System.out.println("Scale: " + scale); // 输出: 3
}
}
2. int precision()
功能: 返回此 BigDecimal
的精度(即有效数字的个数)。
返回值: int
- 精度。
示例:
import java.math.BigDecimal;
public class BigDecimalUtilityExample {
public static void main(String[] args) {
BigDecimal bd = new BigDecimal("123.456");
int precision = bd.precision();
System.out.println("Precision: " + precision); // 输出: 6
}
}
3. BigDecimal ulp()
功能: 返回此 BigDecimal
的最小单位(即单位在最后一位上的值)。
返回值: BigDecimal
- 最小单位。
示例:
import java.math.BigDecimal;
public class BigDecimalUtilityExample {
public static void main(String[] args) {
BigDecimal bd = new BigDecimal("123.456");
BigDecimal ulp = bd.ulp();
System.out.println("ULP: " + ulp); // 输出: 0.001
}
}
4. BigDecimal movePointLeft(int n)
功能: 返回一个 BigDecimal
,其值为将此 BigDecimal
的小数点左移指定的位数后的值。
参数:
n
- 要左移的小数点位数。
返回值: BigDecimal
- 新的 BigDecimal
对象。
示例:
import java.math.BigDecimal;
public class BigDecimalUtilityExample {
public static void main(String[] args) {
BigDecimal bd = new BigDecimal("123.456");
BigDecimal result = bd.movePointLeft(2);
System.out.println("Move point left: " + result); // 输出: 1.23456
}
}
5. BigDecimal movePointRight(int n)
功能: 返回一个 BigDecimal
,其值为将此 BigDecimal
的小数点右移指定的位数后的值。
参数:
n
- 要右移的小数点位数。
返回值: BigDecimal
- 新的 BigDecimal
对象。
示例:
import java.math.BigDecimal;
public class BigDecimalUtilityExample {
public static void main(String[] args) {
BigDecimal bd = new BigDecimal("123.456");
BigDecimal result = bd.movePointRight(2);
System.out.println("Move point right: " + result); // 输出: 12345.6
}
}
九、哈希码与同等性
1. int hashCode()
功能: 返回此 BigDecimal
的哈希码。
返回值: int
- 此 BigDecimal
的哈希码。
示例:
import java.math.BigDecimal;
public class BigDecimalHashCodeExample {
public static void main(String[] args) {
BigDecimal bd = new BigDecimal("123.456");
int hashCode = bd.hashCode();
System.out.println("Hash code: " + hashCode); // 输出: 哈希码值
}
}
2. boolean equals(Object x)
功能: 比较此 BigDecimal
与指定对象的相等性。
参数:
x
- 要比较的对象。
返回值: boolean
- 如果指定对象是与此 BigDecimal
值相同的 BigDecimal
,则返回 true
。
示例:
import java.math.BigDecimal;
public class BigDecimalEqualityExample {
public static void main(String[] args) {
BigDecimal bd1 = new BigDecimal("123.456");
BigDecimal bd2 = new BigDecimal("123.456");
boolean isEqual = bd1.equals(bd2);
System.out.println("Equals: " + isEqual); // 输出: true
}
}
总结
BigDecimal
类提供了丰富的功能来处理任意精度的十进制浮点数运算。通过详细了解和掌握这些方法,您可以在 Java 编程中灵活处理各种高精度计算需求。
- 构造方法与常用常量: 创建
BigDecimal
对象的不同方式以及常用常量。 - 基本算术运算:
add
,subtract
,multiply
,divide
- 比较运算:
compareTo
,equals
- 取整运算:
setScale
,stripTrailingZeros
- 其他数学运算:
abs
,negate
,pow
,max
,min
- 转换方法:
toBigInteger
,toBigIntegerExact
,doubleValue
,floatValue
,intValue
,intValueExact
,longValue
,longValueExact
,toString
- 舍入运算:
round
,setScale
- 其他工具方法:
scale
,precision
,ulp
,movePointLeft
,movePointRight
- 哈希码与同等性:
hashCode
,equals