public final class SloppyMath
extends java.lang.Object
SloppyMath
contains methods for performing basic
numeric operations. In some cases, such as max and min, they cut a few
corners in
the implementation for the sake of efficiency. In particular, they may
not handle special notions like NaN and 0.0 correctly. This was the
origin of the class name, but many other methods are just useful
math additions, such as logAdd. This class just has static math methods.Modifier and Type  Method and Description 

static double 
acos(double cosValue)
Compute acos very quickly by directly looking up the value.

static double 
chiSquare2by2(int k,
int n,
int r,
int m)
Find a 2x2 chisquare value.

static double 
exactBinomial(int k,
int n,
double p)
Find a one tailed exact binomial test probability.

static double 
factorial(int x)
Uses floating point so that it can represent the really big numbers that come up.

static double 
gamma(double n)
Approximation to gamma function.

static double 
hypergeometric(int k,
int n,
int r,
int m)
Find a hypergeometric distribution.

static double 
intPow(double b,
int e)
Exponentiation like we learned in grade school:
multiply b by itself e times.

static float 
intPow(float b,
int e)
Exponentiation like we learned in grade school:
multiply b by itself e times.

static int 
intPow(int b,
int e)
Exponentiation like we learned in grade school:
multiply b by itself e times.

static boolean 
isCloseTo(double a,
double b) 
static boolean 
isDangerous(double d)
Returns true if the argument is a "dangerous" double to have
around, namely one that is infinite, NaN or zero.

static boolean 
isVeryDangerous(double d)
Returns true if the argument is a "very dangerous" double to have
around, namely one that is infinite or NaN.

static double 
lgamma(double x) 
static double 
log(double num,
double base)
Convenience method for log to a different base

static double 
logAdd(double lx,
double ly)
Returns the log of the sum of two numbers, which are
themselves input in log form.

static float 
logAdd(float lx,
float ly)
Returns the log of the sum of two numbers, which are
themselves input in log form.

static void 
main(java.lang.String[] args)
Tests the hypergeometric distribution code, or other functions
provided in this module.

static int 
max(java.util.Collection<java.lang.Integer> vals) 
static double 
max(double a,
double b)
Returns the greater of two
double values. 
static float 
max(float a,
float b)
Returns the greater of two
float values. 
static int 
max(int a,
int b)
Returns the greater of two
int values. 
static int 
max(int a,
int b,
int c)
max() that works on three integers.

static double 
min(double a,
double b)
Returns the smaller of two
double values. 
static float 
min(float a,
float b)
Returns the smaller of two
float values. 
static int 
min(int a,
int b,
int c)
Returns the minimum of three int values.

static int 
nChooseK(int n,
int k)
Computes n choose k in an efficient way.

static double 
oneTailedFishersExact(int k,
int n,
int r,
int m)
Find a onetailed Fisher's exact probability.

static double 
poisson(int x,
double lambda) 
static double 
pow(double a,
double b)
Returns an approximation to Math.pow(a,b) that is ~27x faster
with a margin of error possibly around ~10%.

static int 
pythonMod(int num,
int modulus)
Returns a mod where the sign of the answer is the same as the sign of the second
argument.

static double 
round(double x)
Round a double to the nearest integer, via conventional rules
(.5 rounds up, .49 rounds down), and return the result, still as a double.

static double 
round(double x,
int precision)
Round a double to the given number of decimal places,
rounding to the nearest value via conventional rules (5 rounds up, 49
rounds down).

static double 
sigmoid(double x)
Compute the sigmoid function with mean zero.

public static double round(double x)
x
 What to roundpublic static double round(double x, int precision)
public static int max(int a, int b, int c)
public static int max(java.util.Collection<java.lang.Integer> vals)
public static int max(int a, int b)
int
values. That
is, the result is the argument closer to positive infinity. If
the arguments have the same value, the result is that same
value. Does none of the special checks for NaN or 0.0f that
Math.max
does.a
 an argument.b
 another argument.a
and b
.public static float max(float a, float b)
float
values. That is,
the result is the argument closer to positive infinity. If the
arguments have the same value, the result is that same
value. Does none of the special checks for NaN or 0.0f that
Math.max
does.a
 an argument.b
 another argument.a
and b
.public static double max(double a, double b)
double
values. That
is, the result is the argument closer to positive infinity. If
the arguments have the same value, the result is that same
value. Does none of the special checks for NaN or 0.0f that
Math.max
does.a
 an argument.b
 another argument.a
and b
.public static int min(int a, int b, int c)
public static float min(float a, float b)
float
values. That is,
the result is the value closer to negative infinity. If the
arguments have the same value, the result is that same
value. Does none of the special checks for NaN or 0.0f that
Math.max
does.a
 an argument.b
 another argument.a
and b.
public static double min(double a, double b)
double
values. That
is, the result is the value closer to negative infinity. If the
arguments have the same value, the result is that same
value. Does none of the special checks for NaN or 0.0f that
Math.max
does.a
 an argument.b
 another argument.a
and b
.public static int pythonMod(int num, int modulus)
num
 The numbermodulus
 The moduluspublic static double lgamma(double x)
public static boolean isDangerous(double d)
public static boolean isVeryDangerous(double d)
public static boolean isCloseTo(double a, double b)
public static double gamma(double n)
public static double log(double num, double base)
public static float logAdd(float lx, float ly)
lx
 First number, in log formly
 Second number, in log formlog(exp(lx) + exp(ly))
public static double logAdd(double lx, double ly)
lx
 First number, in log formly
 Second number, in log formlog(exp(lx) + exp(ly))
public static int nChooseK(int n, int k)
public static double pow(double a, double b)
public static int intPow(int b, int e)
b
 basee
 exponentpublic static float intPow(float b, int e)
b
 basee
 exponentpublic static double intPow(double b, int e)
b
 basee
 exponentpublic static double hypergeometric(int k, int n, int r, int m)
k
 The number of black balls drawnn
 The total number of ballsr
 The number of black ballsm
 The number of balls drawnpublic static double exactBinomial(int k, int n, double p)
k
 number of successesn
 Number of trialsp
 Probability of a successpublic static double oneTailedFishersExact(int k, int n, int r, int m)
k
 The number of black balls drawnn
 The total number of ballsr
 The number of black ballsm
 The number of balls drawnpublic static double chiSquare2by2(int k, int n, int r, int m)
k
 The number of black balls drawnn
 The total number of ballsr
 The number of black ballsm
 The number of balls drawnpublic static double sigmoid(double x)
x
 Point to compute sigmoid at.public static double acos(double cosValue)
cosValue
 The cosine of the angle to fine.java.lang.IllegalArgumentException
 if cosValue is not between 1 and 1public static double poisson(int x, double lambda)
public static double factorial(int x)
x
 Argument to take factorial ofpublic static void main(java.lang.String[] args)
args
 Either none, and the log add routines are tested, or the
following 4 arguments: k (cell), n (total), r (row), m (col)