public class ScriptMath2
extends java.lang.Object
The underlying Math2 class is Copyright (c) 2005 Robert Simons (CoHortSoftware@gmail.com). See the MIT/X-like license in com/cohort/util/LICENSE.txt.
Modifier and Type | Field and Description |
---|---|
static int |
Binary0
-2000
|
static int |
BinaryLimit
980; //2^980 = ~1e295
|
static long |
BytesPerGB
BytesPerMB * (long)BytesPerKB
|
static int |
BytesPerKB
1024
|
static int |
BytesPerMB
BytesPerKB * BytesPerKB
|
static long |
BytesPerPB
BytesPerTB * BytesPerKB;
|
static long |
BytesPerTB
BytesPerGB * BytesPerKB
|
static double[] |
COMMON_MV9
-99, -99.9, -99.99, -999, -999.9, -9999, -99999, -999999, -9999999,
99, 99.9, 99.99, 999, 999.9, 9999, 99999, 999999, 9999999
|
static double |
dEps
epsilon suitable for doubles = 1e-13.
|
static float |
fEps
Eps values define small values that are suitable for quick tests
if the difference between two values is close to the precision of
of the data type.
|
static double[] |
InverseTen
This defines inverse powers of ten.
|
static double |
kelvinToC
-273.15
|
static double |
kmPerMile
mPerMile * 0.001
|
static double |
ln10
Math.log(10.0); //2.302585092994046;
|
static double |
ln2
Math.log(2.0)
|
static long |
loAnd
((long) Integer.MAX_VALUE * 2) + 1; //mask for low 32 bits
|
static double |
meterPerFoot
1200.0 / 3927.0.
|
static double |
mPerMile
5280 * meterPerFoot
|
static double |
OneRadian
180.0 / Math.PI
|
static double[] |
Ten
This defines powers of ten.
|
static int[] |
Two
two defines powers of two,
e.g., Two[0]=1, Two[1]=2, Two[2]=4, ...
|
static double |
TwoPi
2 * Math.PI
|
Constructor and Description |
---|
ScriptMath2() |
Modifier and Type | Method and Description |
---|---|
static boolean |
almost0(double d)
This quickly tests if d is almost 0 (Math.abs(d)<dEps).
|
static boolean |
almostEqual(int nSignificantDigits,
double d1,
double d2)
This tests if the numbers are equal to at least n significant digits.
|
static boolean |
almostEqual(int nSignificantDigits,
float f1,
float f2)
This tests if two floats are equal to at least n significant digits.
|
static double |
angle02Pi(double radians)
This converts an angle (in radians) into the range >=0 to
<2PI.
|
static double |
angle0360(double degrees)
This converts an angle (in degrees) into the range >=0 to
<360.
|
static double |
anglePM180(double degrees)
This converts an angle (in degrees) into the range >=-180 to <180
(180 becomes -180).
|
static double |
bigger(double d)
This increases the value (nicely).
|
static double |
bigger15(double d)
This increases the value (nicely) so the mantissa is 1 or 5.
|
static double |
biggerAngle(double d)
This increases the double degrees value (nicely),
and returns it as a string.
|
static double |
biggerDouble(double def,
double mult,
double max,
double d)
This increases d to the next multiple of mult.
|
static int |
binaryExponent(double d)
This returns the binary exponent of a double:
usually +-1023.
|
static int |
binaryFindClosest(double[] dar,
double x)
Find the closest element to x in an ascending sorted array.
|
static int |
binaryFindFirstGAE(double[] dar,
double x,
int precision)
Find the first element which is >x or almostEqual(precision, x) in an ascending sorted array.
|
static int |
binaryFindFirstGE(double[] dar,
double x)
Find the first element which is >= x in an ascending sorted array.
|
static int |
binaryFindLastLAE(double[] dar,
double x,
int precision)
Find the last element which is <x or almostEqual(5, x) in an ascending sorted array.
|
static int |
binaryFindLastLE(double[] dar,
double x)
Find the last element which is <= x in an ascending sorted array.
|
static char |
byteToChar(int b)
Safely converts a byte (-128..127) to char (0..255).
|
static double |
compassToMathDegrees(double compass)
This converts a compass heading (usually 0..360, where North is 0, East is 90...)
to Math-style degrees (East is 0, North is 90, ...).
|
static float |
doubleToFloatNaN(double d)
Safely converts a double to a float (including the non-standard
conversion of large values to Float.NaN, not Float.POSITIVE_INFINITY).
|
static boolean |
equalsIncludingNanOrInfinite(double a,
double b)
This returns true if a == b.
|
static boolean |
equalsIncludingNanOrInfinite(float a,
float b)
This returns true if a == b.
|
static double |
exponent(double d)
This returns the double exponent of a double (e.g., -0.0175 returns 0.01
since -0.0175=-1.75*0.01).
|
static double |
finiteMax(double a,
double b)
This returns the greater value.
|
static double |
finiteMin(double a,
double b)
This returns the lesser value.
|
static double |
floatToDouble(double f)
Safely converts a float to a double.
|
static double |
floatToDoubleNaN(double f)
Crudely (not nicely) converts a float to a double (including the non-standard
conversion of INFINITY values to NaN).
|
static int |
floorDiv(int num,
int den)
A div where the implied mod is always >=0.
|
static long |
floorDiv(long num,
long den)
A div where the implied mod is always >=0.
|
static double |
frac(double d)
This returns the fraction part of a double.
|
static int |
gcd(int n,
int d)
Finds the greatest common divisor.
|
static double |
getSmallIncrement(double range)
This returns a small increment roughly 1/100th the range
(e.g., .1, 1, 10, ....).
|
static boolean |
greaterThanAE(int nSignificantDigits,
double d1,
double d2)
This tests if d1 is greater than or almostEqual9 d2.
|
static void |
guessFrac(double r,
int[] int3)
Looks for a fraction very close to some decimal value.
|
static java.lang.String |
guessFracString(double d)
This creates a String based on the results of guessFrac()
|
static int |
hiDiv(int num,
int den)
A div that rounds up if den>0.
|
static long |
hiDiv(long num,
long den)
A div that rounds up if den>0.
|
static int |
intExponent(double d)
This returns the integer exponent of a double (-0.0175 returns -2
since -0.0175=-1.75*10^-2).
|
static boolean |
lessThanAE(int nSignificantDigits,
double d1,
double d2)
This tests if d1 is less than or almostEqual d2.
|
static double |
longToDoubleNaN(long tl)
This converts a long to a double (Long.MAX_VALUE becomes NaN).
|
static double |
looserAngle0360(double degrees)
This converts an angle (in degrees) into the range >= 0 to <=360
(note 360 is valid).
|
static double |
looserAnglePM180(double degrees)
This converts an angle (in degrees) into the range >=-180 to <=180
(note 180 is valid).
|
static double |
mantissa(double d)
This returns the mantissa of a double (-0.0175 returns -1.75
since -0.0175=-1.75*10^-2).
|
static double |
mathToCompassDegrees(double math)
This converts a Math-style degrees (East is 0, North is 90, ...) to a
compass heading (where North is 0, East is 90, ...).
|
static double |
minMax(double min,
double max,
double current)
This forces a double into a range defined by min..max.
|
static int |
minMax(int min,
int max,
int current)
This forces a int into a range defined by min..max.
|
static double |
minMaxDef(double min,
double max,
double def,
double current)
This forces a double into a range defined by min..max.
|
static int |
minMaxDef(int min,
int max,
int def,
int current)
This forces an int into a range defined by min..max.
|
static double |
NaNCheck(double d)
Checks if the value is NaN or infinite:
returns Double.NaN if so; otherwise returns the original value.
|
static byte |
narrowToByte(int i)
Safely narrows an int to a byte.
|
static byte |
narrowToByte(long i)
Safely narrows a long to a byte.
|
static char |
narrowToChar(int i)
Safely narrows an int to a char.
|
static char |
narrowToChar(long i)
Safely narrows a long to a char.
|
static int |
narrowToInt(long i)
Safely narrows a long to an int.
|
static short |
narrowToShort(int i)
Safely narrows an int to a short.
|
static short |
narrowToShort(long i)
Safely narrows a long to a short.
|
static double |
niceDouble(double d,
int nDigits)
Safely tries to un-bruise a double (8.999999999 -> 9.0,
or 1.000000001 -> 1.0).
|
static boolean |
odd(int i)
Indicates if i is odd.
|
static double |
oneDigitBigger(double max,
double def,
double d)
This increases the first digit of d
(for example, .8, .9, 1, 2, 3, ..., 9, 10, 20, 30, ...).
|
static double |
oneDigitSmaller(double min,
double def,
double d)
This decreases the first digit of d
(for example, 30, 20, 10, 9, ..., 3, 2, 1, .9, .8, ...).
|
static int |
random(int max)
This returns a random integer between 0 and max-1.
|
static java.lang.String |
reduceHashCode(int hashCode)
This reduces a hash code (currently to a 10 digit unsigned number --
no loss of information).
|
static int |
roundDiv(int num,
int den)
A div that rounds.
|
static double |
roundTo(double d,
int nPlaces)
Rounds the value to the specified number of decimal places.
|
static byte |
roundToByte(double d)
Safely rounds a double to a byte.
|
static char |
roundToChar(double d)
Safely rounds a double to a char (treated as unsigned short).
|
static double |
roundToDouble(double d)
Safely rounds a double to the nearest integer (stored as a double).
|
static int |
roundToInt(double d)
Safely rounds a double to an int.
|
static long |
roundToLong(double d)
Safely rounds a double to a long.
|
static short |
roundToShort(double d)
Safely rounds a double to a short.
|
static void |
setSeed(long seed)
This sets the seed for the next call to random().
|
static int |
sign1(int i)
This returns 1 for positive i's, -1 for negative i's,
and 1 if i is 0 (i.e., 0 is treated as a positive number).
|
static double |
smaller(double d)
This decreases the value (nicely).
|
static double |
smaller15(double d)
This gets the double value from the string, decreases it (nicely),
so the mantissa is 1 or 5.
|
static double |
smallerAngle(double d)
This decreases the double degree value (nicely).
|
static double |
smallerDouble(double def,
double mult,
double min,
double d)
This decreases d to the previous multiple of mult.
|
static double[] |
suggestDivisions(double range)
This suggests the division distance along an axis so that there
will be about 5-7 primary divisions and 10-25 secondary.
|
static double[] |
suggestLowHigh(double low,
double high)
This returns a nice bounding range (e.g., for an axis) which
includes low and high.
|
static double |
suggestMaxDivisions(double range,
int maxDivisions)
This suggests the division distance along an axis so that there
will be between maxDivisions/2 and maxDivisions.
|
static double |
ten(int toThe)
This returns an integer power of ten.
|
static double |
trunc(double d)
This returns the truncated part of a double.
|
static int |
truncToInt(double d)
This returns the truncated part of a double, stored as an int.
|
static int |
unsignedByte(int b)
Safely converts a signed byte (-128..127) to an unsigned byte (0..255).
|
public static final float fEps
public static final double dEps
public static final double OneRadian
public static final double TwoPi
public static final double ln10
public static final double ln2
public static final double kelvinToC
public static final int Binary0
public static final int BinaryLimit
public static final int BytesPerKB
public static final int BytesPerMB
public static final long BytesPerGB
public static final long BytesPerTB
public static final long BytesPerPB
public static final long loAnd
public static final double meterPerFoot
public static final double mPerMile
public static final double kmPerMile
public static final int[] Two
public static final double[] Ten
public static final double[] InverseTen
public static final double[] COMMON_MV9
public static double trunc(double d)
d
- a double valuepublic static int truncToInt(double d)
d
- any doubleMath2.trunc(double)
public static double frac(double d)
d
- a doublepublic static int sign1(int i)
i
- an intpublic static int intExponent(double d)
See the similar String2.toRational()
d
- a double valuepublic static double exponent(double d)
d
- a double valuepublic static double mantissa(double d)
See the similar String2.toRational()
d
- any doublepublic static double NaNCheck(double d)
d
- and double valuepublic static boolean odd(int i)
i
- any intpublic static double ten(int toThe)
toThe
- the number that ten is to be raised to the power ofpublic static int binaryExponent(double d)
d
- any doublepublic static final boolean almost0(double d)
d
- any doublepublic static boolean lessThanAE(int nSignificantDigits, double d1, double d2)
nSignificantDigits
- 0 to 18 are allowed; 5, 9, and 14 are commond1
- the first numberd2
- the second numberpublic static boolean greaterThanAE(int nSignificantDigits, double d1, double d2)
nSignificantDigits
- 0 to 18 are allowed; 5, 9, and 14 are commond1
- the first numberd2
- the second numberpublic static boolean almostEqual(int nSignificantDigits, double d1, double d2)
nSignificantDigits
- 0 to 18 are allowed; 5, 9, and 14 are commond1
- any doubled2
- any doublepublic static boolean almostEqual(int nSignificantDigits, float f1, float f2)
nSignificantDigits
- 0 to 18 are allowed; 5, 9, and 14 are commonf1
- any floatf2
- any floatpublic static int roundDiv(int num, int den)
num
- the numerator (a positive number)den
- the denominator (a positive number)public static int hiDiv(int num, int den)
num
- the numerator (a positive number)den
- the denominator (a positive number)public static long hiDiv(long num, long den)
num
- the numerator (a positive number)den
- the denominator (a positive number)public static int floorDiv(int num, int den)
num
- the numerator (a positive or negative number)den
- the denominator (a positive number)public static long floorDiv(long num, long den)
num
- the numerator (a positive or negative number)den
- the denominator (a positive number)public static final int minMax(int min, int max, int current)
min
- the minimum allowed valuemax
- the maximum allowed valuecurrent
- the current valuepublic static final double minMax(double min, double max, double current)
min
- the minimum allowed valuemax
- the maximum allowed valuecurrent
- the current valuepublic static final int minMaxDef(int min, int max, int def, int current)
min
- the minimum allowed valuemax
- the maximum allowed valuedef
- the default valuecurrent
- the current valuepublic static final double minMaxDef(double min, double max, double def, double current)
min
- the minimum allowed valuemax
- the maximum allowed valuedef
- the default valuecurrent
- the current valuepublic static final byte roundToByte(double d)
d
- any doublepublic static final char roundToChar(double d)
d
- any doublepublic static final short roundToShort(double d)
d
- any doublepublic static final int roundToInt(double d)
d
- any doublepublic static final long roundToLong(double d)
d
- any doublepublic static final double roundToDouble(double d)
d
- any doublepublic static final double roundTo(double d, int nPlaces)
d
- any doublenPlaces
- the desired number of digits to the right of the
decimal pointpublic static final byte narrowToByte(int i)
i
- any intpublic static final byte narrowToByte(long i)
i
- any longpublic static final char narrowToChar(int i)
i
- any intpublic static final char narrowToChar(long i)
i
- any longpublic static final short narrowToShort(int i)
i
- any intpublic static final short narrowToShort(long i)
i
- any longpublic static final int narrowToInt(long i)
i
- any longpublic static final char byteToChar(int b)
b
- a byte (-128 .. 127) (or char, short, or int where you just
want the lower 8 bits stored as 0..255)public static final int unsignedByte(int b)
b
- a byte (-128 .. 127) (or char, short, or int where you just
want the lower 8 bits stored as 0..255)public static final double angle0360(double degrees)
public static final double looserAngle0360(double degrees)
degrees
- an angle (in degrees)public static final double compassToMathDegrees(double compass)
compass
- public static final double mathToCompassDegrees(double math)
math
- public static final double anglePM180(double degrees)
degrees
- an angle (in degrees)public static final double looserAnglePM180(double degrees)
degrees
- an angle (in degrees)public static final double angle02Pi(double radians)
public static int gcd(int n, int d)
public static void guessFrac(double r, int[] int3)
int ar[]=new int[3]; double d=-1.75; int whole=guessFrac(d,ar); //results: ar[0]=-1, ar[1]=-3, ar[2]=4
public static java.lang.String guessFracString(double d)
public static final double longToDoubleNaN(long tl)
tl
- public static final double floatToDouble(double f)
f
- a double or floatpublic static final double floatToDoubleNaN(double f)
f
- a float or doublepublic static final float doubleToFloatNaN(double d)
d
- a doublepublic static final double niceDouble(double d, int nDigits)
d
- a double (often bruised)nDigits
- the desired number of significant digitspublic static void setSeed(long seed)
seed
- the new seed -- be sure that it is randomly chosen
from the full range of longspublic static int random(int max)
max
- a int greater than 0public static double bigger(double d)
d
- a doublepublic static double smaller(double d)
d
- a doublepublic static double oneDigitBigger(double max, double def, double d)
max
- the maximum value which may be returneddef
- the default value, to be used if !isFinited
- the initial valuepublic static double oneDigitSmaller(double min, double def, double d)
min
- the minimum value which may be returneddef
- the default value, to be used if !isFinited
- the initial valuepublic static double getSmallIncrement(double range)
range
- public static double biggerDouble(double def, double mult, double max, double d)
def
- the default valuemult
- the multiplemax
- the maximum valued
- the initial valuepublic static double smallerDouble(double def, double mult, double min, double d)
def
- the default valuemult
- the multiplemin
- the minimum valued
- the initial valuepublic static double biggerAngle(double d)
d
- the initial valuepublic static double smallerAngle(double d)
d
- the initial valuepublic static double bigger15(double d)
d
- the initial valuepublic static double smaller15(double d)
d
- the initial valuepublic static double[] suggestLowHigh(double low, double high)
low
- the low end of the rangehigh
- the high end of the rangepublic static double[] suggestDivisions(double range)
range
- the range of the axis, e.g., an axis spanning 30 - 50
would have a range of 20public static double suggestMaxDivisions(double range, int maxDivisions)
range
- the range of the axis, e.g., an axis spanning 30 - 50
would have a range of 20maxDivisions
- the maximum number of divisions (segments).
If you have maxNValues, use maxNValues-1 to call this method.public static int binaryFindLastLE(double[] dar, double x)
If firstGE > lastLE, there are no matching elements (because the requested range is less than or greater than all the values, or between two adjacent values).
dar
- an ascending sorted double[] which may have duplicate valuesx
- public static int binaryFindLastLAE(double[] dar, double x, int precision)
If firstGE > lastLE, there are no matching elements (because the requested range is less than or greater than all the values, or between two adjacent values).
dar
- an ascending sorted double[] which may have duplicate valuesx
- precision
- e.g., 5 for floats and 9 for doublespublic static int binaryFindFirstGE(double[] dar, double x)
If firstGE > lastLE, there are no matching elements (because the requested range is less than or greater than all the values, or between two adjacent values).
dar
- an ascending sorted double[] which currently may not have duplicate valuesx
- public static int binaryFindFirstGAE(double[] dar, double x, int precision)
If firstGE > lastLE, there are no matching elements (because the requested range is less than or greater than all the values, or between two adjacent values).
dar
- an ascending sorted double[] which currently may not have duplicate valuesx
- precision
- e.g., 5 for floats and 9 for doublespublic static int binaryFindClosest(double[] dar, double x)
dar
- an ascending sorted double[].
It the array has duplicates and x equals one of them,
it isn't specified which duplicate's index will be returned.x
- public static java.lang.String reduceHashCode(int hashCode)
hashCode
- public static double finiteMin(double a, double b)
public static double finiteMax(double a, double b)
public static boolean equalsIncludingNanOrInfinite(double a, double b)
a
- b
- public static boolean equalsIncludingNanOrInfinite(float a, float b)
a
- b
-