- java.lang.Object
-
- ch.bind.philib.math.Calc
-
public abstract class Calc extends Object
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedCalc()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static intceilDiv(int num, int divisor)Divides num by divisor, rounding up if num is not evenly divisible by divisor.static longceilDiv(long num, long divisor)Divides num by divisor, rounding up if num is not evenly divisible by divisor.static doubleclip(double value, double min, double max)static intclip(int value, int min, int max)static longclip(long value, long min, long max)static booleanisAddUnderOrOverflow(long a, long b, long r)Detects under or overflow after calculating result = a + b.static longsumOfRange(long end)Calculate the sum of all values from 1 toend, including.static longsumOfRange(long start, long end)Calculates the sum of all values fromstarttoend, including.static longunsignedAdd(long a, long b)
-
-
-
Method Detail
-
ceilDiv
public static long ceilDiv(long num, long divisor)Divides num by divisor, rounding up if num is not evenly divisible by divisor.- Parameters:
num- a number >= 0divisor- a number > 0- Returns:
- rounded up
num/divisor
-
ceilDiv
public static int ceilDiv(int num, int divisor)Divides num by divisor, rounding up if num is not evenly divisible by divisor.- Parameters:
num- a number >= 0divisor- a number > 0- Returns:
- rounded up
num/divisor
-
unsignedAdd
public static long unsignedAdd(long a, long b)
-
isAddUnderOrOverflow
public static boolean isAddUnderOrOverflow(long a, long b, long r)Detects under or overflow after calculating result = a + b. Derivationsign bits: A B R Outcome A^R B^R ((A^R) & (B^R)) 0 0 0 OK 0 0 0 0 0 1 Overflow 1 1 1 0 1 0 OK 0 1 0 0 1 1 OK 1 0 0 1 0 0 OK 1 0 0 1 0 1 OK 0 1 0 1 1 0 Underflow 1 1 1 1 1 1 OK 0 0 0 ((A^R) & (B^R)) produces a negative number if an under/overflow occurred.
- Parameters:
a- -b- -r- result of a + b- Returns:
trueif an over-, or overflow occurred,falseotherwise.
-
sumOfRange
public static long sumOfRange(long end)
Calculate the sum of all values from 1 toend, including. That is:sum = 1 + 2 + 3 + ... + (end-1) + end
Examples:
f(0) = 0
f(1) = 1
f(2) = 3
f(3) = 6
f(10) = 55
f(100) = 5050
- Parameters:
end- The end value of the sum-range.- Returns:
- The sum of all values from 1 to
end, including.
-
sumOfRange
public static long sumOfRange(long start, long end)Calculates the sum of all values fromstarttoend, including.- Parameters:
start- The start value of the sum-range.end- The end value of the sum-range.- Returns:
- The sum of all values from
starttoend, including.
-
clip
public static int clip(int value, int min, int max)
-
clip
public static long clip(long value, long min, long max)
-
clip
public static double clip(double value, double min, double max)
-
-