uk.ac.leeds.ccg.andyt.generic.math
Class Generic_BigDecimal

java.lang.Object
  extended by uk.ac.leeds.ccg.andyt.generic.math.Generic_Number
      extended by uk.ac.leeds.ccg.andyt.generic.math.Generic_BigDecimal
All Implemented Interfaces:
java.io.Serializable

public class Generic_BigDecimal
extends Generic_Number
implements java.io.Serializable

For BigDecimal arithmetic. For the time being it is expected that BigDecimal numbers will not have a precision or scale greater than Integer.MAX_VALUE. Some of the functionality provided in this class may exist in third party libraries with appropriate licenses, but at the time of writing, I have not found them. The aim is for accuracy within a fixed number of decimal places. All methods need to be fully tested to ensure compliance...

See Also:
Serialized Form

Field Summary
 Generic_BigInteger _Generic_BigInteger
          A Generic_BigInteger is often wanted (such as in Taylor Series calculations).
static java.math.BigDecimal ELEVEN
          The number 11 is often used so is made available as a constant.
static java.math.BigDecimal HALF
          The number 0.5 is often used so is made available as a constant.
static java.math.BigDecimal TWO
          The number 2 is often used so is made available as a constant.
 
Fields inherited from class uk.ac.leeds.ccg.andyt.generic.math.Generic_Number
_RandomArray
 
Constructor Summary
Generic_BigDecimal()
          Creates a new instance of Generic_BigDecimal initialising _Generic_BigInteger with 1000 entries and initialising _E to 1000 decimal places.
Generic_BigDecimal(Generic_BigDecimal _Generic_BigDecimal)
          Creates a new instance of Generic_BigDecimal based on _Generic_BigDecimal
Generic_BigDecimal(int n)
          Creates a new instance of Generic_BigDecimal initialising _Generic_BigInteger with n entries and initialising _E to n decimal places.
 
Method Summary
static java.math.BigDecimal ceilingSignificantDigit(java.math.BigDecimal x)
           
static java.math.BigDecimal divideNoRounding(java.math.BigDecimal x, java.math.BigDecimal y)
           
static java.math.BigDecimal divideNoRounding(java.math.BigDecimal x, java.math.BigInteger y)
           
static java.math.BigDecimal divideNoRounding(java.math.BigInteger x, java.math.BigDecimal y)
           
static java.math.BigDecimal divideNoRounding(java.math.BigInteger x, java.math.BigInteger y)
           
static java.util.ArrayList<java.math.BigDecimal> divideRoundIfNecessary(java.util.ArrayList<java.math.BigDecimal> list, java.math.BigDecimal divisor, int decimalPlaces, java.math.RoundingMode roundingMode)
          Creates and returns a new list containing all the numbers in list divided by divisor using decimalPlaces and roundingMode and rounding if necessary.
static java.math.BigDecimal divideRoundIfNecessary(java.math.BigDecimal x, java.math.BigDecimal y, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
           
static java.math.BigDecimal divideRoundIfNecessary(java.math.BigDecimal x, java.math.BigDecimal y, java.math.MathContext a_MathContext)
           
static java.math.BigDecimal divideRoundIfNecessary(java.math.BigDecimal x, java.math.BigInteger y, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
           
static java.math.BigDecimal divideRoundIfNecessary(java.math.BigInteger x, java.math.BigDecimal y, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
           
static java.math.BigDecimal divideRoundIfNecessary(java.math.BigInteger x, java.math.BigInteger y, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
           
static java.math.BigDecimal divideRoundToFixedDecimalPlaces(java.math.BigDecimal x, java.math.BigDecimal y, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
           
static java.math.BigDecimal divideRoundToFixedDecimalPlaces(java.math.BigDecimal x, java.math.BigInteger y, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
           
static java.math.BigDecimal divideRoundToFixedDecimalPlaces(java.math.BigInteger x, java.math.BigDecimal y, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
           
static java.math.BigDecimal divideRoundToFixedDecimalPlaces(java.math.BigInteger x, java.math.BigInteger y, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
           
static java.math.BigDecimal exp(java.math.BigDecimal y, Generic_BigDecimal a_Generic_BigDecimal, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
          e^y = 1 + y/1! + y^2/2! + y^3/3! +...
static java.math.BigDecimal floorSignificantDigit(java.math.BigDecimal x)
           
 java.math.BigDecimal get_PI()
           
 java.math.RoundingMode get_RoundingMode()
           
static int getDecimalPlacePrecision(java.math.BigDecimal value, int significantDigits)
           
 java.math.BigDecimal getEulerConstantToAFixedDecimalPlacePrecision(int decimalPlaces, java.math.RoundingMode a_RoundingMode)
          If _E has enough precision, this is rounded and returned otherwise _E is calculated to the required precision, stored and a copy returned.
 java.math.BigDecimal getEulerConstantToAMinimumDecimalPlacePrecision(int decimalPlaces)
          If _E has at least decimalPlaces decimal place accuracy, then a copy created using new BigDecimal(_E.toString()) is returned otherwise the Euler constant is recalculated using get calculated to the required precision, stored and a copy returned.
static java.math.BigDecimal getRandom(Generic_BigInteger a_Generic_BigInteger, int decimalPlaces, java.math.BigDecimal lowerLimit, java.math.BigDecimal upperLimit)
           
static java.math.BigDecimal getRandom(Generic_BigInteger a_Generic_BigInteger, java.util.Random random, int decimalPlaces, java.math.BigDecimal lowerLimit, java.math.BigDecimal upperLimit)
           
static java.math.BigDecimal getRandom(Generic_Number a_Generic_Number, int decimalPlaces)
          Provided for convenience.
static boolean isEven(java.math.BigDecimal x)
           
static java.math.BigDecimal ln(java.math.BigDecimal x, Generic_BigDecimal a_Generic_BigDecimal, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
          Compute and return the natural logarithm of x accurate to decimalPlaces decimal place precision using Newton's algorithm.
static java.math.BigDecimal log(java.math.BigDecimal base, java.math.BigDecimal x, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
          Only works for x > 0.
static java.math.BigDecimal log(int base_int, java.math.BigDecimal x, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
          Adapted from http://stackoverflow.com/questions/739532/logarithm-of-a-bigdecimal "Pseudocode algorithm for doing a logarithm.
static int magnitude(java.math.BigDecimal x)
          Returns the number of digits to the left of the decimal point
static void main(java.lang.String[] args)
           
static java.math.BigDecimal max(java.util.ArrayList<java.math.BigDecimal> list)
          Returns a copy of the maximum BigDecimal in the list.
static java.math.BigDecimal max(java.math.BigDecimal x, java.math.BigDecimal y)
           
static java.math.BigDecimal max(java.math.BigDecimal x, java.math.BigInteger y)
           
static java.math.BigDecimal min(java.math.BigDecimal x, java.math.BigDecimal y)
           
static java.math.BigDecimal min(java.math.BigDecimal x, java.math.BigInteger y)
           
static java.math.BigDecimal multiplyRoundIfNecessary(java.math.BigDecimal x, java.math.BigDecimal y, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
          This is immutable.
static java.math.BigDecimal multiplyRoundIfNecessary(java.math.BigDecimal x, java.math.BigDecimal y, java.math.MathContext a_MathContext, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
          This is immutable.
static java.math.BigDecimal multiplyRoundIfNecessary(java.math.BigDecimal x, java.math.BigInteger y, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
          x*y If rounding is not wanted, use x.multiply(y1)
static java.math.BigDecimal multiplyRoundIfNecessary(java.math.BigDecimal x, java.math.BigInteger y, java.math.MathContext a_MathContext, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
          x*y If sure that a_MathContext is sufficgfhgshsdfgfdrounding is not wanted, use x.multiply(y1)
protected static java.math.BigDecimal multiplyRoundIfNecessaryNoSpecialCaseCheck(java.math.BigDecimal x, java.math.BigDecimal y, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
           
protected static java.math.BigDecimal multiplyRoundIfNecessaryNoSpecialCaseCheck(java.math.BigDecimal x, java.math.BigDecimal y, java.math.MathContext a_MathContext, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
          If a_MathContext has the right precision for rounding, it is used, otherwise rounding is done if necessary using decimalPlaces and a_RoundingMode.
static java.math.BigDecimal multiplyRoundToFixedDecimalPlaces(java.math.BigDecimal x, java.math.BigDecimal y, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
          The calculation could be divided into parts using the following algebra: (a + b) * (c + d) = (a * c) + (a * d) + (b * c) + (b * d) Consider that a is the integer part of x and b is the remainder and similarly that c is the integer part of y and d is the remainder.
static int positionSignificantDigit(java.math.BigDecimal x)
          If most (biggest) significant digit is right of decimal point then this is positive.
static java.math.BigDecimal power(java.math.BigDecimal x, java.math.BigDecimal y, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
          x^y = e^(y * ln(x)) x^(a+b) = (x^a)*(x^b)
static java.math.BigDecimal power(java.math.BigDecimal x, java.math.BigInteger y, int div, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
          Calculates and returns x raised to the power of y accurate to decimalPlaces number of decimal places.
static java.math.BigDecimal power(java.math.BigDecimal x, int y, int div, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
          Calculates and returns x raised to the power of y accurate to decimalPlaces number of decimal places.
static java.math.BigDecimal power(java.math.BigDecimal x, long y, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
           
static java.math.BigDecimal powerNoRounding(java.math.BigDecimal x, java.math.BigDecimal y)
          Immutable
static java.math.BigDecimal powerNoRounding(java.math.BigDecimal x, java.math.BigInteger y, int div)
          In the case of y being positive.
static java.math.BigDecimal powerNoRounding(java.math.BigDecimal x, int y)
          Calculates and returns an accurate representation of x^y.
static java.math.BigDecimal powerNoRounding(java.math.BigDecimal x, int y, int div)
          Calculates and returns x raised to the power of y accurate to decimalPlaces number of decimal places.
static java.math.BigDecimal powerNoSpecialCaseCheck(java.math.BigDecimal x, java.math.BigInteger y, int div, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
          Calculates and returns x raised to the power of y accurate to decimalPlaces number of decimal places.
static java.math.BigDecimal powerNoSpecialCaseCheckNoRounding(java.math.BigDecimal x, java.math.BigInteger y, int div)
           
static boolean powerTestAbove(java.math.BigDecimal compare, java.math.BigDecimal x, java.math.BigInteger y, int div, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
           
static boolean powerTestAbove(java.math.BigDecimal compare, java.math.BigDecimal x, int y, int div, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
          Deprecated. 
static boolean powerTestAboveNoRounding(java.math.BigDecimal compare, java.math.BigDecimal x, java.math.BigInteger y, int div)
           
static boolean powerTestBelow(java.math.BigDecimal compare, java.math.BigDecimal x, java.math.BigInteger y, int div, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
           
static boolean powerTestBelow(java.math.BigDecimal compare, java.math.BigDecimal x, int y, int div, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
          Deprecated. 
static boolean powerTestBelowNoRounding(java.math.BigDecimal compare, java.math.BigDecimal x, java.math.BigInteger y, int div)
           
static java.math.BigDecimal powerUnscaled1Precision1(java.math.BigDecimal x, int y)
          x.unscaledValue() == 1 x.precision == 1
static boolean randomUniformTest(java.util.Random a_Random, java.math.BigDecimal probability, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
          Effectively this is the same as generating a random number between 0 and 1 and comparing it with probability and if it were higher then return false and otherwise return true
static boolean randomUniformTest(java.util.Random a_Random, java.math.BigDecimal probability, java.math.MathContext a_MathContext)
          Effectively this is the same as generating a random number between 0 and 1 and comparing it with probability and if it were higher then return false and otherwise return true
static boolean randomUniformTest(java.util.Random a_Random, java.math.BigDecimal probability, java.math.RoundingMode a_RoundingMode)
          Effectively this is the same as generating a random number between 0 and 1 and comparing it with probability and if it were higher then return false and otherwise return true
static java.math.BigDecimal reciprocal(java.math.BigDecimal x, int decimalPlace, java.math.RoundingMode a_RoundingMode)
           
static java.math.BigDecimal reciprocalWillBeIntegerReturnBigDecimal(java.math.BigDecimal x)
           
static java.math.BigInteger reciprocalWillBeIntegerReturnBigInteger(java.math.BigDecimal x)
           
static java.math.BigDecimal rootNoRounding(java.math.BigDecimal x, java.math.BigInteger root)
           
static java.math.BigDecimal rootNoRounding(java.math.BigDecimal x, int root)
           
static java.math.BigDecimal rootRoundIfNecessary(java.math.BigDecimal x, java.math.BigInteger root, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
           
static java.math.BigDecimal rootRoundIfNecessary(java.math.BigDecimal x, int root, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
           
static java.math.BigDecimal roundIfNecessary(java.math.BigDecimal x, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
          This is not necessarily an immutable operation as x may be returned!
static java.math.BigDecimal roundStrippingTrailingZeros(java.math.BigDecimal x, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
           
static java.math.BigDecimal roundToAndSetDecimalPlaces(java.math.BigDecimal x, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
           
 void set_RoundingMode(java.math.RoundingMode a_RoundingMode)
           
static java.math.BigDecimal sqrt(java.math.BigDecimal x, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
          Returns the square rootRoundIfNecessary of x as a BigDecimal rounded to decimalPlaces decimal places.
 
Methods inherited from class uk.ac.leeds.ccg.andyt.generic.math.Generic_Number
get_RandomArray, get_RandomArray, get_RandomArrayMinLength, init_RandomArrayMinLength, timeReport
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_Generic_BigInteger

public Generic_BigInteger _Generic_BigInteger
A Generic_BigInteger is often wanted (such as in Taylor Series calculations).


TWO

public static final java.math.BigDecimal TWO
The number 2 is often used so is made available as a constant.


HALF

public static final java.math.BigDecimal HALF
The number 0.5 is often used so is made available as a constant.


ELEVEN

public static final java.math.BigDecimal ELEVEN
The number 11 is often used so is made available as a constant.

Constructor Detail

Generic_BigDecimal

public Generic_BigDecimal()
Creates a new instance of Generic_BigDecimal initialising _Generic_BigInteger with 1000 entries and initialising _E to 1000 decimal places.


Generic_BigDecimal

public Generic_BigDecimal(Generic_BigDecimal _Generic_BigDecimal)
Creates a new instance of Generic_BigDecimal based on _Generic_BigDecimal


Generic_BigDecimal

public Generic_BigDecimal(int n)
Creates a new instance of Generic_BigDecimal initialising _Generic_BigInteger with n entries and initialising _E to n decimal places.

Method Detail

get_RoundingMode

public java.math.RoundingMode get_RoundingMode()
Returns:
the _RoundingMode

set_RoundingMode

public void set_RoundingMode(java.math.RoundingMode a_RoundingMode)
Parameters:
RoundingMode - the _RoundingMode to set

main

public static void main(java.lang.String[] args)
Parameters:
args - the command line arguments

magnitude

public static int magnitude(java.math.BigDecimal x)
Returns the number of digits to the left of the decimal point

Parameters:
x -
Returns:

multiplyRoundIfNecessary

public static java.math.BigDecimal multiplyRoundIfNecessary(java.math.BigDecimal x,
                                                            java.math.BigDecimal y,
                                                            int decimalPlaces,
                                                            java.math.RoundingMode a_RoundingMode)
This is immutable.

Parameters:
x -
y -
decimalPlaces -
a_RoundingMode -
Returns:
x multiplied by y rounded if necessary

multiplyRoundIfNecessary

public static java.math.BigDecimal multiplyRoundIfNecessary(java.math.BigDecimal x,
                                                            java.math.BigDecimal y,
                                                            java.math.MathContext a_MathContext,
                                                            int decimalPlaces,
                                                            java.math.RoundingMode a_RoundingMode)
This is immutable.

Parameters:
x -
y -
decimalPlaces -
a_RoundingMode -
Returns:
x multiplied by y rounded if necessary

multiplyRoundIfNecessaryNoSpecialCaseCheck

protected static java.math.BigDecimal multiplyRoundIfNecessaryNoSpecialCaseCheck(java.math.BigDecimal x,
                                                                                 java.math.BigDecimal y,
                                                                                 java.math.MathContext a_MathContext,
                                                                                 int decimalPlaces,
                                                                                 java.math.RoundingMode a_RoundingMode)
If a_MathContext has the right precision for rounding, it is used, otherwise rounding is done if necessary using decimalPlaces and a_RoundingMode. It is expected that the RoundingMode of the MathContext is the same as a_RoundingMode.

Parameters:
x -
y -
a_MathContext - MathContext with some precision and a_RoundingMode RoundingMode
decimalPlaces - decimalPlaces > -1
a_RoundingMode -
Returns:

multiplyRoundIfNecessaryNoSpecialCaseCheck

protected static java.math.BigDecimal multiplyRoundIfNecessaryNoSpecialCaseCheck(java.math.BigDecimal x,
                                                                                 java.math.BigDecimal y,
                                                                                 int decimalPlaces,
                                                                                 java.math.RoundingMode a_RoundingMode)
Parameters:
x -
y -
decimalPlaces -
a_RoundingMode -
Returns:

multiplyRoundToFixedDecimalPlaces

public static java.math.BigDecimal multiplyRoundToFixedDecimalPlaces(java.math.BigDecimal x,
                                                                     java.math.BigDecimal y,
                                                                     int decimalPlaces,
                                                                     java.math.RoundingMode a_RoundingMode)
The calculation could be divided into parts using the following algebra: (a + b) * (c + d) = (a * c) + (a * d) + (b * c) + (b * d) Consider that a is the integer part of x and b is the remainder and similarly that c is the integer part of y and d is the remainder. Then: (a * c) is an integer; (a * d) is a number with a scale no greater than the scale of d; similarly, (b * c) is a number with a scale no greater than the scale of b. These aforementioned parts give the main magnitude of the result. With the exception of multiplications by zero, the remaining part, (b * d) is the only part that is shrinking. It is also the only part that may require more decimal places to store the result accurately. So, in calculating all but (b * d) we can assess if the MathContext is sufficient...

Parameters:
x -
y -
decimalPlaces -
a_RoundingMode -
Returns:
x multiplied by y then rounded;

multiplyRoundIfNecessary

public static java.math.BigDecimal multiplyRoundIfNecessary(java.math.BigDecimal x,
                                                            java.math.BigInteger y,
                                                            int decimalPlaces,
                                                            java.math.RoundingMode a_RoundingMode)
x*y If rounding is not wanted, use x.multiply(y1)

Parameters:
x -
y -
decimalPlaces -
a_RoundingMode -
Returns:
x multiplied by y then rounded if necessary

multiplyRoundIfNecessary

public static java.math.BigDecimal multiplyRoundIfNecessary(java.math.BigDecimal x,
                                                            java.math.BigInteger y,
                                                            java.math.MathContext a_MathContext,
                                                            int decimalPlaces,
                                                            java.math.RoundingMode a_RoundingMode)
x*y If sure that a_MathContext is sufficgfhgshsdfgfdrounding is not wanted, use x.multiply(y1)

Parameters:
x -
y -
decimalPlaces -
a_RoundingMode -
Returns:
x multiplied by y then rounded if necessary

divideRoundIfNecessary

public static java.math.BigDecimal divideRoundIfNecessary(java.math.BigDecimal x,
                                                          java.math.BigDecimal y,
                                                          int decimalPlaces,
                                                          java.math.RoundingMode a_RoundingMode)
Parameters:
x -
y -
decimalPlaces -
a_RoundingMode -
Returns:
x/y rounded to decimalPlaces decimal places if necessary using a_RoundingMode RoundingMode

divideRoundIfNecessary

public static java.math.BigDecimal divideRoundIfNecessary(java.math.BigDecimal x,
                                                          java.math.BigDecimal y,
                                                          java.math.MathContext a_MathContext)
Parameters:
x -
y -
decimalPlaces -
a_RoundingMode -
Returns:
x/y rounded to decimalPlaces decimal places if necessary using a_RoundingMode RoundingMode

divideRoundToFixedDecimalPlaces

public static java.math.BigDecimal divideRoundToFixedDecimalPlaces(java.math.BigDecimal x,
                                                                   java.math.BigDecimal y,
                                                                   int decimalPlaces,
                                                                   java.math.RoundingMode a_RoundingMode)
Parameters:
x -
y -
decimalPlaces -
a_RoundingMode -
Returns:
x/y then rounded;

divideNoRounding

public static java.math.BigDecimal divideNoRounding(java.math.BigDecimal x,
                                                    java.math.BigDecimal y)
Parameters:
x -
y -
decimalPlaces -
a_RoundingMode -
Returns:
x/y

divideRoundIfNecessary

public static java.util.ArrayList<java.math.BigDecimal> divideRoundIfNecessary(java.util.ArrayList<java.math.BigDecimal> list,
                                                                               java.math.BigDecimal divisor,
                                                                               int decimalPlaces,
                                                                               java.math.RoundingMode roundingMode)
Creates and returns a new list containing all the numbers in list divided by divisor using decimalPlaces and roundingMode and rounding if necessary.


divideRoundIfNecessary

public static java.math.BigDecimal divideRoundIfNecessary(java.math.BigDecimal x,
                                                          java.math.BigInteger y,
                                                          int decimalPlaces,
                                                          java.math.RoundingMode a_RoundingMode)
Parameters:
x -
y -
decimalPlaces -
a_RoundingMode -
Returns:
x/y then rounded;

divideRoundToFixedDecimalPlaces

public static java.math.BigDecimal divideRoundToFixedDecimalPlaces(java.math.BigDecimal x,
                                                                   java.math.BigInteger y,
                                                                   int decimalPlaces,
                                                                   java.math.RoundingMode a_RoundingMode)
Parameters:
x -
y -
decimalPlaces -
a_RoundingMode -
Returns:
x/y then rounded;

divideNoRounding

public static java.math.BigDecimal divideNoRounding(java.math.BigDecimal x,
                                                    java.math.BigInteger y)
Parameters:
x -
y -
decimalPlaces -
a_RoundingMode -
Returns:
x/y

divideRoundIfNecessary

public static java.math.BigDecimal divideRoundIfNecessary(java.math.BigInteger x,
                                                          java.math.BigDecimal y,
                                                          int decimalPlaces,
                                                          java.math.RoundingMode a_RoundingMode)

divideRoundToFixedDecimalPlaces

public static java.math.BigDecimal divideRoundToFixedDecimalPlaces(java.math.BigInteger x,
                                                                   java.math.BigDecimal y,
                                                                   int decimalPlaces,
                                                                   java.math.RoundingMode a_RoundingMode)

divideNoRounding

public static java.math.BigDecimal divideNoRounding(java.math.BigInteger x,
                                                    java.math.BigDecimal y)

divideRoundIfNecessary

public static java.math.BigDecimal divideRoundIfNecessary(java.math.BigInteger x,
                                                          java.math.BigInteger y,
                                                          int decimalPlaces,
                                                          java.math.RoundingMode a_RoundingMode)

divideRoundToFixedDecimalPlaces

public static java.math.BigDecimal divideRoundToFixedDecimalPlaces(java.math.BigInteger x,
                                                                   java.math.BigInteger y,
                                                                   int decimalPlaces,
                                                                   java.math.RoundingMode a_RoundingMode)

divideNoRounding

public static java.math.BigDecimal divideNoRounding(java.math.BigInteger x,
                                                    java.math.BigInteger y)

powerTestAbove

public static boolean powerTestAbove(java.math.BigDecimal compare,
                                     java.math.BigDecimal x,
                                     java.math.BigInteger y,
                                     int div,
                                     int decimalPlaces,
                                     java.math.RoundingMode a_RoundingMode)
Parameters:
compare -
x - x > 1
y - y > 1
div - Optimisation parameter...
decimalPlaces - The number of decimal places rounded to if rounding is necessary.
a_RoundingMode - The RoundingMode used in the case rounding is necessary.
Returns:
true iff x^y > compare

powerTestAboveNoRounding

public static boolean powerTestAboveNoRounding(java.math.BigDecimal compare,
                                               java.math.BigDecimal x,
                                               java.math.BigInteger y,
                                               int div)
Parameters:
compare -
x - x > 1
y - y > 1
div - Optimisation parameter...
decimalPlaces - The number of decimal places rounded to if rounding is necessary.
a_RoundingMode - The RoundingMode used in the case rounding is necessary.
Returns:
true iff x^y > compare

powerTestBelow

public static boolean powerTestBelow(java.math.BigDecimal compare,
                                     java.math.BigDecimal x,
                                     java.math.BigInteger y,
                                     int div,
                                     int decimalPlaces,
                                     java.math.RoundingMode a_RoundingMode)
Parameters:
compare -
x - x > 0
y - y > 1
div - Optimisation parameter...
decimalPlaces - The number of decimal places rounded to if rounding is necessary.
a_RoundingMode - The RoundingMode used in the case rounding is necessary.
Returns:
true iff x^y < compare

powerTestBelowNoRounding

public static boolean powerTestBelowNoRounding(java.math.BigDecimal compare,
                                               java.math.BigDecimal x,
                                               java.math.BigInteger y,
                                               int div)
Parameters:
compare -
x - x > 0
y - y > 1
div - Optimisation parameter...
decimalPlaces - The number of decimal places rounded to if rounding is necessary.
a_RoundingMode - The RoundingMode used in the case rounding is necessary.
Returns:
true iff x^y < compare

powerTestAbove

@Deprecated
public static boolean powerTestAbove(java.math.BigDecimal compare,
                                                java.math.BigDecimal x,
                                                int y,
                                                int div,
                                                int decimalPlaces,
                                                java.math.RoundingMode a_RoundingMode)
Deprecated. 

Parameters:
x - x > 1
y1 - x > 1
div - If < 3 this is set to 2. If > 256 set to 256
decimalPlaces -
a_RoundingMode -
Returns:
true iff x^y1 > compare

powerTestBelow

@Deprecated
public static boolean powerTestBelow(java.math.BigDecimal compare,
                                                java.math.BigDecimal x,
                                                int y,
                                                int div,
                                                int decimalPlaces,
                                                java.math.RoundingMode a_RoundingMode)
Deprecated. 

Parameters:
x - x > 0
y1 - x > 1
div - If < 3 this is set to 2. If > 256 set to 256
decimalPlaces -
a_RoundingMode -
Returns:
true iff x^y1 > compare

power

public static java.math.BigDecimal power(java.math.BigDecimal x,
                                         java.math.BigDecimal y,
                                         int decimalPlaces,
                                         java.math.RoundingMode a_RoundingMode)
x^y = e^(y * ln(x)) x^(a+b) = (x^a)*(x^b)

Parameters:
x -
y -
a_Generic_BigDecimal - this may be null. If a Generic_BigDecimal is required for the calculation though one will be created if null is passed in. So for efficiency it is recommended to pass one in if it exists or is otherwise needed.
decimalPlaces - The number of decimal places the toCompare must be accurate and rounded to if necessary.
Returns:
x raised to the power of y accurate to decimalPlaces number of decimal places

powerNoRounding

public static java.math.BigDecimal powerNoRounding(java.math.BigDecimal x,
                                                   java.math.BigDecimal y)
Immutable

Parameters:
x -
y -
decimalPlaces -
a_RoundingMode -
Returns:

power

public static java.math.BigDecimal power(java.math.BigDecimal x,
                                         long y,
                                         int decimalPlaces,
                                         java.math.RoundingMode a_RoundingMode)
Parameters:
x -
y1 -
decimalPlaces -
a_RoundingMode -
Returns:
x^y1 accurate to decimalPlaces

power

public static java.math.BigDecimal power(java.math.BigDecimal x,
                                         int y,
                                         int div,
                                         int decimalPlaces,
                                         java.math.RoundingMode a_RoundingMode)
Calculates and returns x raised to the power of y accurate to decimalPlaces number of decimal places.

Parameters:
x -
y -
div - If div < 2 it is set to 2. If div > 256 it is set to 256. div is used to divide the calculation up if y is less than div, it is all done in one step. Otherwise it is broken into parts.
decimalPlaces -
a_RoundingMode -
Returns:
x^y accurate to decimalPlaces

powerNoRounding

public static java.math.BigDecimal powerNoRounding(java.math.BigDecimal x,
                                                   int y,
                                                   int div)
Calculates and returns x raised to the power of y accurate to decimalPlaces number of decimal places. If y is negative and a precise answer cannot be given an exception will be thrown.

Parameters:
x -
y -
div - If div < 2 it is set to 2. If div > 256 it is set to 256. div is used to divide the calculation up if y is less than div, it is all done in one step. Otherwise it is broken into parts.
decimalPlaces -
a_RoundingMode -
Returns:
x^y accurate to decimalPlaces

powerUnscaled1Precision1

public static java.math.BigDecimal powerUnscaled1Precision1(java.math.BigDecimal x,
                                                            int y)
x.unscaledValue() == 1 x.precision == 1

Parameters:
x -
y - y >=0
Returns:

power

public static java.math.BigDecimal power(java.math.BigDecimal x,
                                         java.math.BigInteger y,
                                         int div,
                                         int decimalPlaces,
                                         java.math.RoundingMode a_RoundingMode)
Calculates and returns x raised to the power of y accurate to decimalPlaces number of decimal places. The calculation is divided into parts

Parameters:
x -
y -
div - If div < 2 it is set to 2. If div > 256 it is set to 256.
decimalPlaces -
a_RoundingMode -
Returns:
x^y accurate to decimalPlaces

powerNoRounding

public static java.math.BigDecimal powerNoRounding(java.math.BigDecimal x,
                                                   java.math.BigInteger y,
                                                   int div)
In the case of y being positive. For a negative y, decimalPlace and a_RoundingMode are needed due to a division in the computation. In these cases, the result

Parameters:
x -
y -
div -
decimalPlaces -
a_RoundingMode -
Returns:

powerNoSpecialCaseCheck

public static java.math.BigDecimal powerNoSpecialCaseCheck(java.math.BigDecimal x,
                                                           java.math.BigInteger y,
                                                           int div,
                                                           int decimalPlaces,
                                                           java.math.RoundingMode a_RoundingMode)
Calculates and returns x raised to the power of y accurate to decimalPlaces number of decimal places. The calculation is divided into parts

Parameters:
x -
y -
div - If div < 2 it is set to 2. If div > 256 it is set to 256.
decimalPlaces -
a_RoundingMode -
Returns:
x^y accurate to decimalPlaces

powerNoSpecialCaseCheckNoRounding

public static java.math.BigDecimal powerNoSpecialCaseCheckNoRounding(java.math.BigDecimal x,
                                                                     java.math.BigInteger y,
                                                                     int div)

powerNoRounding

public static java.math.BigDecimal powerNoRounding(java.math.BigDecimal x,
                                                   int y)
Calculates and returns an accurate representation of x^y. The method as implemented duplicates x and multiplies this duplicate by x y times. For large values of y this can be slow. A method which divides the multiplication into more parts is provided by powerNoRounding(BigDecimal,int,int). Values of x that are greater than one and have a scale greater than will have a both increased scale and magnitude and the result gets effectively increments the scale and magnitude with each multiplication. Consequently, this may not be the most appropriate multiplication method to use. If the precision of the result required in terms of significant digits or decimal places can be specified then these parameters can be passed to other methods will return the result faster albeit it rounded to the specified precision using either a default or input RoundingMode. The scale of the result is y times the scale of x.

Parameters:
x -
y -
Returns:

reciprocal

public static java.math.BigDecimal reciprocal(java.math.BigDecimal x,
                                              int decimalPlace,
                                              java.math.RoundingMode a_RoundingMode)
Parameters:
x -
decimalPlace -
a_RoundingMode -
Returns:
1/x Accurate to decimalPlace number of decimal places. Throws an ArithmeticException - if divisor is zero, roundingMode==RoundingMode.UNNECESSARY and the specified scale is insufficient to represent the result of the division exactly.

reciprocalWillBeIntegerReturnBigInteger

public static java.math.BigInteger reciprocalWillBeIntegerReturnBigInteger(java.math.BigDecimal x)
Parameters:
x -
Returns:
1/x if toCompare is an integer or throws a ArithmeticException. If x = 0 or the result cannot be returned exactly, an ArithmeticException is thrown

reciprocalWillBeIntegerReturnBigDecimal

public static java.math.BigDecimal reciprocalWillBeIntegerReturnBigDecimal(java.math.BigDecimal x)
Parameters:
x -
Returns:
1/x if toCompare is an integer or throws a ArithmeticException. If x = 0 or the result cannot be returned exactly, an ArithmeticException is thrown

log

public static java.math.BigDecimal log(int base_int,
                                       java.math.BigDecimal x,
                                       int decimalPlaces,
                                       java.math.RoundingMode a_RoundingMode)
Adapted from http://stackoverflow.com/questions/739532/logarithm-of-a-bigdecimal "Pseudocode algorithm for doing a logarithm. Assuming we want log_n of toCompare toCompare = 0; base = x_BigDecimal; input = toCompare; while (input > base) toCompare++; input /= base; fraction = 1/2; input *= input; while (((toCompare + fraction) > toCompare) && (input > 1)) if (input > base) input /= base; toCompare += fraction; input *= input; fraction /= 2.0; The big while loop may seem a bit confusing. On each pass, you can either square your input or you can take the square rootRoundIfNecessary of your base; either way, you must divide your fraction by 2. I find squaring the input, and leaving the base alone, to be more accurate. If the input goes to 1, we're through. The log of 1, for any base, is 0, which means we don't need to add any more. if (toCompare + fraction) is not greater than toCompare, then we've hit the limits of precision for our numbering system. We can stop. Obviously, if you're working with a system which has arbitrarily many digits of precision, you will want to put something else in there to limit the loop."


log

public static java.math.BigDecimal log(java.math.BigDecimal base,
                                       java.math.BigDecimal x,
                                       int decimalPlaces,
                                       java.math.RoundingMode a_RoundingMode)
Only works for x > 0.

Parameters:
base -
x -
decimalPlaces -
a_RoundingMode -
Returns:
The log of x to the base base to decimalPlace precision

roundToAndSetDecimalPlaces

public static java.math.BigDecimal roundToAndSetDecimalPlaces(java.math.BigDecimal x,
                                                              int decimalPlaces,
                                                              java.math.RoundingMode a_RoundingMode)
Parameters:
x -
decimalPlaces -
a_RoundingMode -
Returns:
x rounded if necessary and with a scale set to decimalPlaces

roundStrippingTrailingZeros

public static java.math.BigDecimal roundStrippingTrailingZeros(java.math.BigDecimal x,
                                                               int decimalPlaces,
                                                               java.math.RoundingMode a_RoundingMode)
Parameters:
x -
decimalPlaces -
a_RoundingMode -
Returns:
x rounded to decimalPlaces with any trailing zeros stripped

roundIfNecessary

public static java.math.BigDecimal roundIfNecessary(java.math.BigDecimal x,
                                                    int decimalPlaces,
                                                    java.math.RoundingMode a_RoundingMode)
This is not necessarily an immutable operation as x may be returned!

Parameters:
x -
decimalPlaces -
a_RoundingMode -
Returns:
x rounded to decimalPlaces if the scale of x is greater than decimal places. Otherwise returns x.

getDecimalPlacePrecision

public static int getDecimalPlacePrecision(java.math.BigDecimal value,
                                           int significantDigits)
Parameters:
value -
significantDigits -
Returns:

get_PI

public java.math.BigDecimal get_PI()
Returns:
_PI which is initialised with 10000 decimal places.

getEulerConstantToAMinimumDecimalPlacePrecision

public java.math.BigDecimal getEulerConstantToAMinimumDecimalPlacePrecision(int decimalPlaces)
If _E has at least decimalPlaces decimal place accuracy, then a copy created using new BigDecimal(_E.toString()) is returned otherwise the Euler constant is recalculated using get calculated to the required precision, stored and a copy returned.

Parameters:
decimalPlaces -
Returns:

getEulerConstantToAFixedDecimalPlacePrecision

public java.math.BigDecimal getEulerConstantToAFixedDecimalPlacePrecision(int decimalPlaces,
                                                                          java.math.RoundingMode a_RoundingMode)
If _E has enough precision, this is rounded and returned otherwise _E is calculated to the required precision, stored and a copy returned.

Parameters:
decimalPlaces -
Returns:

exp

public static java.math.BigDecimal exp(java.math.BigDecimal y,
                                       Generic_BigDecimal a_Generic_BigDecimal,
                                       int decimalPlaces,
                                       java.math.RoundingMode a_RoundingMode)
e^y = 1 + y/1! + y^2/2! + y^3/3! +...

Parameters:
y -
a_Generic_BigDecimal - may be null. Passing a Generic_BigDecimal in can save computation. The Euler constant is used in most invocations.
decimalPlaces -
a_RoundingMode -
Returns:
e^y where e is the Euler constant. The result is returned correct to decimalPlaces decimal place precision.

ln

public static java.math.BigDecimal ln(java.math.BigDecimal x,
                                      Generic_BigDecimal a_Generic_BigDecimal,
                                      int decimalPlaces,
                                      java.math.RoundingMode a_RoundingMode)
Compute and return the natural logarithm of x accurate to decimalPlaces decimal place precision using Newton's algorithm. Method adapted from http://stackoverflow.com/questions/739532/logarithm-of-a-bigdecimal

Parameters:
x -
a_Generic_BigDecimal -
decimalPlaces -
a_RoundingMode -
Returns:
the natural logarithm of x accurate to decimalPlaces number of decimal places and with result rounded using a_RoundingMode if necessary.

max

public static java.math.BigDecimal max(java.math.BigDecimal x,
                                       java.math.BigDecimal y)
Parameters:
x -
y1 -
Returns:
the larger (closer to positive infinity) of either x or y1

max

public static java.math.BigDecimal max(java.util.ArrayList<java.math.BigDecimal> list)
Returns a copy of the maximum BigDecimal in the list.


max

public static java.math.BigDecimal max(java.math.BigDecimal x,
                                       java.math.BigInteger y)
Parameters:
x -
y1 -
Returns:
the larger (closer to positive infinity) of either x or y1

min

public static java.math.BigDecimal min(java.math.BigDecimal x,
                                       java.math.BigDecimal y)
Parameters:
x -
y1 -
Returns:
the larger (closer to negative infinity) of either x or y1

min

public static java.math.BigDecimal min(java.math.BigDecimal x,
                                       java.math.BigInteger y)
Parameters:
x -
y1 -
Returns:
the larger (closer to positive infinity) of either x or y1

positionSignificantDigit

public static int positionSignificantDigit(java.math.BigDecimal x)
If most (biggest) significant digit is right of decimal point then this is positive. If left of decimal point this is negative

Parameters:
x -
Returns:
The exponent of 10 to which the unscaled value is multiplied to return the most (biggest) significant digit

floorSignificantDigit

public static java.math.BigDecimal floorSignificantDigit(java.math.BigDecimal x)
Parameters:
x -
Returns:
The next closer to 0 BigDecimal with same decimal place precision as x.

ceilingSignificantDigit

public static java.math.BigDecimal ceilingSignificantDigit(java.math.BigDecimal x)
Parameters:
x -
Returns:
The next further away from 0 BigDecimal with same decimal place precision as x.

rootRoundIfNecessary

public static java.math.BigDecimal rootRoundIfNecessary(java.math.BigDecimal x,
                                                        java.math.BigInteger root,
                                                        int decimalPlaces,
                                                        java.math.RoundingMode a_RoundingMode)
Parameters:
x -
rootRoundIfNecessary -
decimalPlaces -
a_RoundingMode -
Returns:
The rootRoundIfNecessary of x to decimalPlaces precision

rootNoRounding

public static java.math.BigDecimal rootNoRounding(java.math.BigDecimal x,
                                                  java.math.BigInteger root)
Parameters:
x -
rootRoundIfNecessary -
decimalPlaces -
a_RoundingMode -
Returns:
The rootRoundIfNecessary of x to decimalPlaces precision

rootRoundIfNecessary

public static java.math.BigDecimal rootRoundIfNecessary(java.math.BigDecimal x,
                                                        int root,
                                                        int decimalPlaces,
                                                        java.math.RoundingMode a_RoundingMode)
Parameters:
x - x >= 0
rootRoundIfNecessary -
decimalPlaces -
a_RoundingMode -
Returns:
The rootRoundIfNecessary rootRoundIfNecessary of x to decimalPlaces precision.

rootNoRounding

public static java.math.BigDecimal rootNoRounding(java.math.BigDecimal x,
                                                  int root)

sqrt

public static java.math.BigDecimal sqrt(java.math.BigDecimal x,
                                        int decimalPlaces,
                                        java.math.RoundingMode a_RoundingMode)
Returns the square rootRoundIfNecessary of x as a BigDecimal rounded to decimalPlaces decimal places.

Parameters:
x -
decimalPlaces -
Returns:
Square rootRoundIfNecessary of input

randomUniformTest

public static boolean randomUniformTest(java.util.Random a_Random,
                                        java.math.BigDecimal probability,
                                        java.math.RoundingMode a_RoundingMode)
Effectively this is the same as generating a random number between 0 and 1 and comparing it with probability and if it were higher then return false and otherwise return true

Parameters:
a_Random -
probability - 0 <= probability <=1
Returns:
true or false based on a random uniform test of probability

randomUniformTest

public static boolean randomUniformTest(java.util.Random a_Random,
                                        java.math.BigDecimal probability,
                                        int decimalPlaces,
                                        java.math.RoundingMode a_RoundingMode)
Effectively this is the same as generating a random number between 0 and 1 and comparing it with probability and if it were higher then return false and otherwise return true

Parameters:
a_Random -
probability - 0 <= probability <=1
Returns:
true or false based on a random uniform test of probability

randomUniformTest

public static boolean randomUniformTest(java.util.Random a_Random,
                                        java.math.BigDecimal probability,
                                        java.math.MathContext a_MathContext)
Effectively this is the same as generating a random number between 0 and 1 and comparing it with probability and if it were higher then return false and otherwise return true

Parameters:
a_Random -
probability - 0 <= probability <=1
Returns:
true or false based on a random uniform test of probability

getRandom

public static java.math.BigDecimal getRandom(Generic_BigInteger a_Generic_BigInteger,
                                             int decimalPlaces,
                                             java.math.BigDecimal lowerLimit,
                                             java.math.BigDecimal upperLimit)
Parameters:
a_Generic_BigInteger - this contains the random and the powers of two and is passed in for efficiency.
decimalPlaces -
lowerLimit -
upperLimit -
Returns:
a pseudo randomly constructed BigDecimal in the range from lowerLimit to upperLimit inclusive and that has up to decimalPlaces number of decimal places

getRandom

public static java.math.BigDecimal getRandom(Generic_BigInteger a_Generic_BigInteger,
                                             java.util.Random random,
                                             int decimalPlaces,
                                             java.math.BigDecimal lowerLimit,
                                             java.math.BigDecimal upperLimit)

getRandom

public static java.math.BigDecimal getRandom(Generic_Number a_Generic_Number,
                                             int decimalPlaces)
Provided for convenience.

Parameters:
a_Generic_BigDecimal -
decimalPlaces -
Returns:
a random BigDecimal between 0 and 1 inclusive which can have up to decimalPlaces number of decimal places

isEven

public static boolean isEven(java.math.BigDecimal x)
Parameters:
x -
Returns:
true iff last digit of x is even