ch.bind.philib.math
Class Calc

java.lang.Object
  extended by ch.bind.philib.math.Calc

public abstract class Calc
extends Object


Constructor Summary
protected Calc()
           
 
Method Summary
static int ceilDiv(int num, int divisor)
          Divides num by divisor, rounding up if num is not evenly divisible by divisor.
static long ceilDiv(long num, long divisor)
          Divides num by divisor, rounding up if num is not evenly divisible by divisor.
static double clip(double value, double min, double max)
           
static int clip(int value, int min, int max)
           
static long clip(long value, long min, long max)
           
static boolean isAddUnderOrOverflow(long a, long b, long r)
          Detects under or overflow after calculating result = a + b.
static long sumOfRange(long end)
          Calculate the sum of all values from 1 to end, including.
static long sumOfRange(long start, long end)
          Calculates the sum of all values from start to end, including.
static long unsignedAdd(long a, long b)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Calc

protected Calc()
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 > 0
divisor - 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 > 0
divisor - 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. Derivation
 sign 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:
true if an over-, or overflow occurred, false otherwise.

sumOfRange

public static long sumOfRange(long end)
Calculate the sum of all values from 1 to end, 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 from start to end, 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 start to end , 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)


Copyright © 2013. All Rights Reserved.