public class Maths extends Object
| 构造器和说明 |
|---|
Maths() |
| 限定符和类型 | 方法和说明 |
|---|---|
static int |
abs(int n) |
static long |
abs(long n) |
static int |
add(int a,
int b) |
static long |
add(long a,
long b) |
static long |
bitsMask(int bits)
Returns a long value of bit count mask
calculate the bit counts mask long value
a: (1 << bits) - 1
b: -1L ^ (-1L << bits)
c: ~(-1L << bits)
d: Long.MAX_VALUE >>> (63 - bits)
bitsMask(0) = 0 : 0000000000000000000000000000000000000000000000000000000000000000
bitsMask(1) = 1 : 0000000000000000000000000000000000000000000000000000000000000001
bitsMask(2) = 3 : 0000000000000000000000000000000000000000000000000000000000000011
bitsMask(10) = 1023 : 0000000000000000000000000000000000000000000000000000001111111111
bitsMask(20) = 1048575 : 0000000000000000000000000000000000000000000011111111111111111111
bitsMask(63) = 9223372036854775807: 0111111111111111111111111111111111111111111111111111111111111111
bitsMask(64) = -1 : 1111111111111111111111111111111111111111111111111111111111111111
|
static long |
ceilDiv(long x,
long y) |
static long |
downDiv(long x,
long y) |
static int |
gcd(int[] array)
Returns the greatest common divisor in array
|
static int |
gcd(int a,
int b)
Returns the greatest common divisor
|
static double |
log(double n,
double base)
求以base为底n的对数
Math.log10(double) 求以10为底n的对数(lg)
Math.log(double) 以e为底n的对数(自然对数,ln)
Math.log1p(double) 以e为底n+1的对数 |
static double |
log2(double n)
以2为底n的对数
|
static long |
pow(long base,
int exponent)
Returns a long value for
baseexponent. |
static int |
reverseBits(int n)
|
static int |
rotateLeft(int x,
int n)
rotate shift left,循环左移位操作:0<=n<=32
|
static int |
subtract(int a,
int b) |
static long |
subtract(long a,
long b) |
static long |
upDiv(long x,
long y)
upDiv( 7, 3) = 3
upDiv(-7, 3) = -3
upDiv( 7, -3) = -3
upDiv(-7, -3) = 3
upDiv(10, 5) = 2
upDiv( 1, 1) = 1
upDiv(-1, 2) = -1
upDiv( 1, -2) = -1
upDiv( 0, 5) = 0
upDiv( 0, -5) = 0
upDiv( 5, -1) = -5
upDiv(-5, 1) = -5
upDiv( 1, 3) = 1
upDiv(-1, 3) = -1
upDiv( 2, 3) = 1
upDiv(-2, 3) = -1
upDiv(x, y) : 向远于零方向取整
downDiv(x, y) : 向近于零方向取整
ceilDiv(x, y) : 向正无穷方向取整
Math.floorDiv(x, y): 向负无穷方向取整
|
public static double log2(double n)
n - the valuepublic static double log(double n,
double base)
Math.log10(double) 求以10为底n的对数(lg)
Math.log(double) 以e为底n的对数(自然对数,ln)
Math.log1p(double) 以e为底n+1的对数n - a valuebase - 底数public static int rotateLeft(int x,
int n)
x - the valuen - shift bit lenpublic static long bitsMask(int bits)
Returns a long value of bit count mask calculate the bit counts mask long value a: (1 << bits) - 1 b: -1L ^ (-1L << bits) c: ~(-1L << bits) d: Long.MAX_VALUE >>> (63 - bits) bitsMask(0) = 0 : 0000000000000000000000000000000000000000000000000000000000000000 bitsMask(1) = 1 : 0000000000000000000000000000000000000000000000000000000000000001 bitsMask(2) = 3 : 0000000000000000000000000000000000000000000000000000000000000011 bitsMask(10) = 1023 : 0000000000000000000000000000000000000000000000000000001111111111 bitsMask(20) = 1048575 : 0000000000000000000000000000000000000000000011111111111111111111 bitsMask(63) = 9223372036854775807: 0111111111111111111111111111111111111111111111111111111111111111 bitsMask(64) = -1 : 1111111111111111111111111111111111111111111111111111111111111111
bits - the bit countpublic static int reverseBits(int n)
n - the numberpublic static long pow(long base,
int exponent)
baseexponent.base - the baseexponent - the exponentbaseexponent.public static int abs(int n)
public static long abs(long n)
public static long upDiv(long x,
long y)
upDiv( 7, 3) = 3 upDiv(-7, 3) = -3 upDiv( 7, -3) = -3 upDiv(-7, -3) = 3 upDiv(10, 5) = 2 upDiv( 1, 1) = 1 upDiv(-1, 2) = -1 upDiv( 1, -2) = -1 upDiv( 0, 5) = 0 upDiv( 0, -5) = 0 upDiv( 5, -1) = -5 upDiv(-5, 1) = -5 upDiv( 1, 3) = 1 upDiv(-1, 3) = -1 upDiv( 2, 3) = 1 upDiv(-2, 3) = -1 upDiv(x, y) : 向远于零方向取整 downDiv(x, y) : 向近于零方向取整 ceilDiv(x, y) : 向正无穷方向取整 Math.floorDiv(x, y): 向负无穷方向取整
x - the numeratory - the denominatorpublic static long downDiv(long x,
long y)
public static long ceilDiv(long x,
long y)
public static int add(int a,
int b)
public static int subtract(int a,
int b)
public static long add(long a,
long b)
public static long subtract(long a,
long b)
public static int gcd(int a,
int b)
a - the first numberb - the second numberpublic static int gcd(int[] array)
array - the int arrayCopyright © 2025. All rights reserved.