public final class Maths extends Object
| Modifier and Type | Method and Description |
|---|---|
static double |
add(long a,
long b,
long c)
return a + b / c as a double
|
static long |
agitate(long l)
Performs a series of bit operations on a long value to "agitate" the bits.
|
static double |
asDouble(long value,
int exponent,
boolean negative,
int decimalPlaces)
Convert the components
(-1)^negative * value * 2^exponent * 10^-decimalPlaces to a double. |
static double |
ceilN(double d,
double digits)
Rounds up the given value to the nearest higher number with specified decimal places.
|
static double |
ceilN(double d,
int digits)
Rounds up the given double value to the nearest higher number,
accurate to the specified number of decimal places.
|
static int |
digits(long num)
Returns the number of digits in the given number.
|
static long |
divideRoundUp(long dividend,
long divisor)
Divide
dividend by divisor, if division is not integral the result is rounded up. |
static long |
fives(int decimalPlaces)
Returns the power of five for the given decimal places.
|
static double |
floorN(double d,
double digits)
Rounds down the given value to the nearest lower number with specified decimal places.
|
static double |
floorN(double d,
int digits)
Rounds down the given double value to the nearest lower number,
accurate to the specified number of decimal places.
|
static int |
hash(Object o) |
static int |
hash(Object o1,
Object o2) |
static int |
hash(Object o1,
Object o2,
Object o3) |
static int |
hash(Object o1,
Object o2,
Object o3,
Object o4) |
static int |
hash(Object o1,
Object o2,
Object o3,
Object o4,
Object o5) |
static long |
hash64(@Nullable CharSequence cs) |
static long |
hash64(long l0)
A simple hashing algorithm for a 64-bit value
|
static long |
hash64(@NotNull String s)
Computes a 64-bit hash value for the given string.
|
static long |
hash64(@NotNull StringBuilder s)
Computes a 64-bit hash value for the given StringBuilder.
|
static int |
intLog2(long num)
Returns rounded down log2
num, e. |
static boolean |
isPowerOf2(long n)
Checks if a number is a power of two.
|
static int |
nextPower2(int n,
int min)
Returns the next power of two greater than or equal to the given number, with a specified minimum.
|
static long |
nextPower2(long n,
long min)
Returns the next power of two greater than or equal to the given number, with a specified minimum.
|
static double |
round1(double d)
Rounds to one decimal place.
|
static double |
round1up(double d)
Performs a round which is accurate to within 1 ulp.
|
static double |
round2(double d)
Rounds to two decimal places.
|
static double |
round2up(double d)
Performs a round which is accurate to within 1 ulp.
|
static double |
round3(double d)
Performs a round which is accurate to within 1 ulp.
|
static double |
round3up(double d)
Performs a round which is accurate to within 1 ulp.
|
static double |
round4(double d)
Performs a round which is accurate to within 1 ulp.
|
static double |
round4up(double d)
Performs a round which is accurate to within 1 ulp.
|
static double |
round5(double d)
Performs a round which is accurate to within 1 ulp.
|
static double |
round5up(double d)
Performs a round which is accurate to within 1 ulp.
|
static double |
round6(double d)
Performs a round which is accurate to within 1 ulp.
|
static double |
round6up(double d)
Performs a round which is accurate to within 1 ulp.
|
static double |
round7(double d)
Performs a round which is accurate to within 1 ulp.
|
static double |
round7up(double d)
Performs a round which is accurate to within 1 ulp.
|
static double |
round8(double d)
Performs a round which is accurate to within 1 ulp.
|
static double |
round8up(double d)
Performs a round which is accurate to within 1 ulp.
|
static long |
roundingFactor(double digits) |
static long |
roundingFactor(int digits) |
static double |
roundN(double d,
double digits)
Rounds the value using a fractional number of decimal places.
|
static double |
roundN(double d,
int digits)
Rounds the value to a fixed number of decimal places.
|
static double |
roundNup(double d,
int digits)
Performs a round which is accurate to within 1 ulp.
|
static boolean |
same(double a,
double b)
Checks if two double values are the same.
|
static boolean |
same(float a,
float b)
Checks if two float values are the same.
|
static long |
tens(int decimalPlaces)
Returns the power of ten for the given decimal places.
|
static short |
toInt16(long value)
Returns the value of the
long argument;
throwing an exception if the value overflows a short. |
static int |
toInt32(long value)
Returns the value of the
long argument;
throwing an exception if the value overflows an int. |
static int |
toInt32(long value,
@NotNull String msg)
Returns the value of the
long argument;
throwing an exception if the value overflows an int. |
static byte |
toInt8(long value)
Returns the value of the
long argument;
throwing an exception if the value overflows a byte. |
static int |
toUInt16(long value)
Returns the value of the
long argument;
throwing an exception if the value overflows an unsigned short (0xFFFF). |
static int |
toUInt31(long value)
Returns the value of the
long argument;
throwing an exception if the value overflows an unsigned 31 bit value (0x7FFFFFFFL). |
static long |
toUInt32(long value)
Returns the value of the
long argument;
throwing an exception if the value overflows an unsigned int (0xFFFFFFFFL). |
static short |
toUInt8(long value)
Returns the value of the
long argument;
throwing an exception if the value overflows an unsigned byte (0xFF). |
public static double roundN(double d,
int digits)
d - value to rounddigits - precision from 0 to 18 digitspublic static double roundNup(double d,
int digits)
d - value to roundpublic static long roundingFactor(int digits)
public static long roundingFactor(double digits)
public static double ceilN(double d,
int digits)
d - The double value to be rounded up.digits - The number of decimal places to which the value is to be rounded.public static double floorN(double d,
int digits)
d - The double value to be rounded down.digits - The number of decimal places to which the value is to be rounded.public static double roundN(double d,
double digits)
roundN(double, int) when digits is an integer.d - value to rounddigits - fractional digits of precisionpublic static double ceilN(double d,
double digits)
d - the value to ceildigits - number of decimal placespublic static double floorN(double d,
double digits)
d - the value to floordigits - number of decimal placespublic static double round1(double d)
d - value to roundpublic static double round1up(double d)
d - value to roundpublic static double round2(double d)
d - value to roundpublic static double round2up(double d)
d - value to roundpublic static double round3(double d)
d - value to roundpublic static double round3up(double d)
d - value to roundpublic static double round4(double d)
d - value to roundpublic static double round4up(double d)
d - value to roundpublic static double round5(double d)
d - value to roundpublic static double round5up(double d)
d - value to roundpublic static double round6(double d)
d - value to roundpublic static double round6up(double d)
d - value to roundpublic static double round7(double d)
d - value to roundpublic static double round7up(double d)
d - value to roundpublic static double round8(double d)
d - value to roundpublic static double round8up(double d)
d - value to roundpublic static int nextPower2(int n,
int min)
throws IllegalArgumentException
n - the number to find the next power of two formin - the minimum power of two to return if n is less than minIllegalArgumentException - if the provided min value is not a positive power of twopublic static long nextPower2(long n,
long min)
throws IllegalArgumentException
n - the number to find the next power of two formin - the minimum power of two to return if n is less than minIllegalArgumentException - if the provided min value is not a positive power of twopublic static boolean isPowerOf2(long n)
n - the number to checkpublic static long hash64(@Nullable
@Nullable CharSequence cs)
public static long hash64(@NotNull
@NotNull String s)
s - the string to compute the hash forIllegalArgumentException - if s is nullpublic static long hash64(@NotNull
@NotNull StringBuilder s)
s - the string to compute the hash forIllegalArgumentException - if s is nullpublic static int intLog2(long num)
throws IllegalArgumentException
num, e. g.: intLog2(1) == 0,
intLog2(2) == 1, intLog2(7) == 2, intLog2(8) == 3, etc.IllegalArgumentException - if the given number <= 0public static byte toInt8(long value)
throws ArithmeticException
long argument;
throwing an exception if the value overflows a byte.value - the long valueArithmeticException - if the argument overflows a bytepublic static short toInt16(long value)
throws ArithmeticException
long argument;
throwing an exception if the value overflows a short.value - the long valueArithmeticException - if the argument overflows a shortpublic static int toInt32(long value,
@NotNull
@NotNull String msg)
throws ArithmeticException
long argument;
throwing an exception if the value overflows an int.value - the long valuemsg - to use in a potential exception messageArithmeticException - if the argument overflows an intpublic static int toInt32(long value)
throws ArithmeticException
long argument;
throwing an exception if the value overflows an int.value - the long valueArithmeticException - if the argument overflows an intpublic static short toUInt8(long value)
throws ArithmeticException
long argument;
throwing an exception if the value overflows an unsigned byte (0xFF).value - the long valueArithmeticException - if the argument overflows an unsigned bytepublic static int toUInt16(long value)
throws ArithmeticException
long argument;
throwing an exception if the value overflows an unsigned short (0xFFFF).value - the long valueArithmeticException - if the argument overflows an unsigned shortpublic static int toUInt31(long value)
throws ArithmeticException
long argument;
throwing an exception if the value overflows an unsigned 31 bit value (0x7FFFFFFFL).value - the long valueArithmeticException - if the argument overflows an unsigned intpublic static long toUInt32(long value)
throws ArithmeticException
long argument;
throwing an exception if the value overflows an unsigned int (0xFFFFFFFFL).value - the long valueArithmeticException - if the argument overflows an unsigned intpublic static long agitate(long l)
l - the long value to be agitatedpublic static long hash64(long l0)
l0 - to hashpublic static long divideRoundUp(long dividend,
long divisor)
dividend by divisor, if division is not integral the result is rounded up.
Examples: divideRoundUp(10, 5) == 2, divideRoundUp(11, 5) == 3,
divideRoundUp(-10, 5) == -2, divideRoundUp(-11, 5) == -3.ArithmeticException - if divisor is zero.public static long tens(int decimalPlaces)
decimalPlaces - the number of decimal placesIllegalArgumentException - if the decimal places are less than 0 or greater than or equal to 19public static int digits(long num)
num - the number to count the digits ofpublic static long fives(int decimalPlaces)
decimalPlaces - the number of decimal placespublic static boolean same(double a,
double b)
a - the first double valueb - the second double valuepublic static boolean same(float a,
float b)
a - the first float valueb - the second float valuepublic static int hash(Object o)
public static double asDouble(long value,
int exponent,
boolean negative,
int decimalPlaces)
(-1)^negative * value * 2^exponent * 10^-decimalPlaces to a double.
Accuracy. Within the Clinger fast-path domain this returns the correctly-rounded
result — bit-identical to Double.parseDouble(String) of the same decimal. The
domain is:
exponent == 0 (the mantissa was accumulated without binary shedding), andvalue <= 2^53 (the mantissa is exactly representable — i.e. up to 15
significant decimal digits), and-22 <= decimalPlaces <= 22 (10^22 is the largest exactly-representable
power of ten).[1e-8, 1e15)
carrying up to 15 significant digits, and modest-precision values up to 1e22. For such
inputs the result is guaranteed exact (0 ULP).
Deviation (extreme cases only). Outside that domain — a full-precision mantissa of
16–17 significant digits (value > 2^53), or |decimalPlaces| > 22
(magnitudes below ~1e-8 or very large values written to full precision) — the
result is produced by an approximate reconstruction and may differ from the correctly-rounded
value by up to 1 ULP (up to 2 ULP at the most extreme exponents). Round-half-to-even is
not guaranteed there, so the error rate skews with the trailing decimal digit (odd last digits,
which sit nearer a rounding boundary, miss more often). These cases do not arise on the common
realistic data path; closing the gap fully would require a correctly-rounded big-integer /
Eisel-Lemire step, traded off here against speed and code size.
value - The integer value (mantissa), >= 0exponent - The binary exponent (non-zero only when the mantissa overflowed a long)negative - Whether the result is negativedecimalPlaces - The number of decimal places (net of any explicit power-of-ten exponent)public static double add(long a,
long b,
long c)
Copyright © 2026 Chronicle Software Ltd. All rights reserved.