Class OpenMath
Provides high-performance, high-precision mathematical calculations.
提供高性能、高精度的数学计算,包括算术运算、统计函数、数论函数。
Features | 主要功能:
- High-precision arithmetic (add, subtract, multiply, divide) - 高精度运算
- Rounding (round, ceil, floor) - 取整
- Statistics (mean, median, variance, stdDev, sum) - 统计函数
- Number theory (gcd, lcm, factorial, isPrime, fibonacci) - 数论函数
- Power operations (pow, modPow) - 幂运算
- Utility (abs, signum, isEven, isOdd) - 工具方法
Usage Examples | 使用示例:
// High-precision division - 高精度除法
BigDecimal result = OpenMath.divide(a, b, 2);
// Statistics - 统计
double avg = OpenMath.mean(1.0, 2.0, 3.0);
double mid = OpenMath.median(1.0, 2.0, 3.0);
// Number theory - 数论
int gcd = OpenMath.gcd(12, 18); // 6
boolean prime = OpenMath.isPrime(17); // true
long fib = OpenMath.fibonacci(10); // 55
Security | 安全性:
- Thread-safe: Yes (stateless) - 线程安全: 是 (无状态)
- Null-safe: Yes - 空值安全: 是
- Since:
- JDK 25, opencode-base-core V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic intabs(int value) Absolute value (safe: throws on Integer.MIN_VALUE instead of returning negative) 绝对值 (safe: throws on Integer.MIN_VALUE instead of returning negative) 绝对值 (安全:对 Integer.MIN_VALUE 抛出异常而非返回负数)static longabs(long value) Absolute value (long, safe: throws on Long.MIN_VALUE instead of returning negative) 绝对值(long, safe: throws on Long.MIN_VALUE instead of returning negative) 绝对值(long,安全:对 Long.MIN_VALUE 抛出异常而非返回负数)static BigDecimaladd(BigDecimal a, BigDecimal b) High-precision addition of two BigDecimal values.static doubleceil(double value, int scale) Rounds a double value up to the specified number of decimal places.static BigDecimaldivide(BigDecimal a, BigDecimal b, int scale) High-precision division with half-up rounding.static BigDecimaldivide(BigDecimal a, BigDecimal b, int scale, RoundingMode mode) High-precision division with specified rounding mode.static longfactorial(int n) Calculates the factorial of n (n must be in [0, 20]).static BigIntegerfactorialBig(int n) Calculates the factorial of n as a BigInteger (arbitrary precision).static longfibonacci(int n) Returns the n-th Fibonacci number (0-indexed: F(0)=0, F(1)=1, F(2)=1, ...).static doublefloor(double value, int scale) Rounds a double value down to the specified number of decimal places.static intgcd(int a, int b) Greatest Common Divisor (GCD) 最大公约数(GCD) Safe: throws ArithmeticException for Integer.MIN_VALUE inputsstatic longgcd(long a, long b) Greatest Common Divisor (long) 最大公约数(long) Safe: throws ArithmeticException for Long.MIN_VALUE inputsstatic booleanisEven(int value) Returns true if the value is even.static booleanisNegative(int value) Returns true if the value is negative.static booleanisOdd(int value) Returns true if the value is odd.static booleanisPositive(int value) Returns true if the value is positive.static booleanisPrime(long n) Returns true if n is a prime number.static intlcm(int a, int b) Least Common Multiple (LCM) 最小公倍数(LCM) Safe: uses Math.absExact to detect overflowstatic longlcm(long a, long b) Least Common Multiple (long) 最小公倍数(long) Safe: uses Math.absExact to detect overflowstatic doublemean(double... values) Calculates the arithmetic mean of the given values.static doublemedian(double... values) Calculates the median of the given values.static longmodPow(long base, long exponent, long modulus) Computes (base ^ exponent) mod modulus using BigInteger.static BigDecimalmultiply(BigDecimal a, BigDecimal b) High-precision multiplication of two BigDecimal values.static longpow(long base, int exponent) Computes base raised to the power of exponent using overflow-safe multiplication.static doubleround(double value, int scale) Rounds a double value to the specified number of decimal places using half-up rounding.static doublesampleStdDev(double... values) Calculates the sample standard deviation of the given values (uses sample variance with Bessel's correction).static doublesampleVariance(double... values) Calculates the sample variance of the given values using Bessel's correction (divides by n-1).static intsignum(int value) Returns the signum of the value: -1, 0, or 1.static intsignum(long value) Returns the signum of the long value: -1, 0, or 1.static doublestdDev(double... values) Calculates the population standard deviation of the given values (uses population variance).static BigDecimalsubtract(BigDecimal a, BigDecimal b) High-precision subtraction of two BigDecimal values.static doublesum(double... values) Calculates the sum of the given double values.static intsum(int... values) Calculates the sum of the given int values using overflow-safe addition.static longsum(long... values) Calculates the sum of the given long values using overflow-safe addition.static doublevariance(double... values) Calculates the population variance of the given values (divides by n).
-
Method Details
-
add
High-precision addition of two BigDecimal values. 高精度加法 -
subtract
High-precision subtraction of two BigDecimal values. 高精度减法 -
multiply
High-precision multiplication of two BigDecimal values. 高精度乘法 -
divide
High-precision division with half-up rounding. 高精度除法 -
divide
High-precision division with specified rounding mode. 高精度除法(指定舍入模式) -
round
public static double round(double value, int scale) Rounds a double value to the specified number of decimal places using half-up rounding. 四舍五入 -
ceil
public static double ceil(double value, int scale) Rounds a double value up to the specified number of decimal places. 向上取整 -
floor
public static double floor(double value, int scale) Rounds a double value down to the specified number of decimal places. 向下取整 -
mean
public static double mean(double... values) Calculates the arithmetic mean of the given values. 计算平均值 -
median
public static double median(double... values) Calculates the median of the given values. 计算中位数 -
variance
public static double variance(double... values) Calculates the population variance of the given values (divides by n). 计算总体方差(除以 n)This computes the population variance, which divides the sum of squared deviations by the number of values (n). Use
sampleVariance(double...)for sample variance with Bessel's correction (divides by n-1).此方法计算总体方差,以数据个数 n 为分母。如需样本方差(贝塞尔校正,以 n-1 为分母), 请使用
sampleVariance(double...)。- Parameters:
values- the values | 数值数组- Returns:
- the population variance, or 0 if the array is null or empty 总体方差,若数组为 null 或空则返回 0
-
sampleVariance
public static double sampleVariance(double... values) Calculates the sample variance of the given values using Bessel's correction (divides by n-1). 计算样本方差(贝塞尔校正,除以 n-1)This computes the unbiased sample variance, which divides the sum of squared deviations by (n-1) instead of n. This is the appropriate estimator when computing variance from a sample rather than an entire population.
此方法计算无偏样本方差,以 (n-1) 为分母而非 n。当数据为样本而非总体时, 应使用此方法以获得无偏估计。
- Parameters:
values- the values (must contain at least 2 elements) | 数值数组(至少需要 2 个元素)- Returns:
- the sample variance, or 0 if the array is null or empty; returns 0 for a single-element array (no variance) 样本方差;若数组为 null 或空返回 0;单元素数组返回 0
-
stdDev
public static double stdDev(double... values) Calculates the population standard deviation of the given values (uses population variance). 计算总体标准差(基于总体方差)This is the square root of the population variance. Use
sampleStdDev(double...)for the sample standard deviation.此方法为 总体方差 的平方根。 如需样本标准差,请使用
sampleStdDev(double...)。- Parameters:
values- the values | 数值数组- Returns:
- the population standard deviation | 总体标准差
-
sampleStdDev
public static double sampleStdDev(double... values) Calculates the sample standard deviation of the given values (uses sample variance with Bessel's correction). 计算样本标准差(基于样本方差,贝塞尔校正)This is the square root of the sample variance.
此方法为 样本方差 的平方根。
- Parameters:
values- the values (must contain at least 2 elements) | 数值数组(至少需要 2 个元素)- Returns:
- the sample standard deviation | 样本标准差
-
sum
public static double sum(double... values) Calculates the sum of the given double values. 计算总和 -
sum
public static long sum(long... values) Calculates the sum of the given long values using overflow-safe addition. 计算总和(long) -
sum
public static int sum(int... values) Calculates the sum of the given int values using overflow-safe addition. 计算总和(int) -
gcd
public static int gcd(int a, int b) Greatest Common Divisor (GCD) 最大公约数(GCD) Safe: throws ArithmeticException for Integer.MIN_VALUE inputs -
gcd
public static long gcd(long a, long b) Greatest Common Divisor (long) 最大公约数(long) Safe: throws ArithmeticException for Long.MIN_VALUE inputs -
lcm
public static int lcm(int a, int b) Least Common Multiple (LCM) 最小公倍数(LCM) Safe: uses Math.absExact to detect overflow -
lcm
public static long lcm(long a, long b) Least Common Multiple (long) 最小公倍数(long) Safe: uses Math.absExact to detect overflow -
factorial
public static long factorial(int n) Calculates the factorial of n (n must be in [0, 20]). 阶乘 -
factorialBig
Calculates the factorial of n as a BigInteger (arbitrary precision). 阶乘(大数) -
isPrime
public static boolean isPrime(long n) Returns true if n is a prime number. 素数判断 -
fibonacci
public static long fibonacci(int n) Returns the n-th Fibonacci number (0-indexed: F(0)=0, F(1)=1, F(2)=1, ...). 返回第 n 个斐波那契数(从 0 开始:F(0)=0, F(1)=1, F(2)=1, ...)。Valid range: 0 ≤ n ≤ 92. For n > 92, the result overflows
long.有效范围:0 ≤ n ≤ 92。当 n > 92 时,结果溢出
long。- Parameters:
n- the index (must be in [0, 92]) | 索引(必须在 [0, 92] 范围内)- Returns:
- the n-th Fibonacci number | 第 n 个斐波那契数
- Throws:
IllegalArgumentException- if n is negative | 如果 n 为负数ArithmeticException- if n > 92 (result overflows long) | 如果 n > 92(结果溢出 long)
-
pow
public static long pow(long base, int exponent) Computes base raised to the power of exponent using overflow-safe multiplication. 幂运算 -
modPow
public static long modPow(long base, long exponent, long modulus) Computes (base ^ exponent) mod modulus using BigInteger. 模幂运算 -
abs
public static int abs(int value) Absolute value (safe: throws on Integer.MIN_VALUE instead of returning negative) 绝对值 (safe: throws on Integer.MIN_VALUE instead of returning negative) 绝对值 (安全:对 Integer.MIN_VALUE 抛出异常而非返回负数) -
abs
public static long abs(long value) Absolute value (long, safe: throws on Long.MIN_VALUE instead of returning negative) 绝对值(long, safe: throws on Long.MIN_VALUE instead of returning negative) 绝对值(long,安全:对 Long.MIN_VALUE 抛出异常而非返回负数) -
signum
public static int signum(int value) Returns the signum of the value: -1, 0, or 1. 符号函数 -
signum
public static int signum(long value) Returns the signum of the long value: -1, 0, or 1. 符号函数(long) -
isEven
public static boolean isEven(int value) Returns true if the value is even. 是否为偶数 -
isOdd
public static boolean isOdd(int value) Returns true if the value is odd. 是否为奇数 -
isNegative
public static boolean isNegative(int value) Returns true if the value is negative. 是否为负数 -
isPositive
public static boolean isPositive(int value) Returns true if the value is positive. 是否为正数
-