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

public class Generic_BigDecimal
extends Generic_Number

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...


Field Summary
 java.math.BigDecimal _E
          For storing the Euler–Mascheroni constant correct to a fixed decimal place precision
 Generic_BigInteger _Factorial_Generic_BigInteger
          Factorial of numbers are quite often wanted (especially in Taylor Series calculations), and when they are, this is for them.
 java.math.BigDecimal _PI
          For storing the PI constant correct to a fixed decimal place precision
 java.math.RoundingMode _RoundingMode
          RoundingMode used in calculations
static java.math.BigDecimal Two
          Two is quite often wanted as a BigDecimal so it is made available as a constant.
 
Constructor Summary
Generic_BigDecimal()
          Creates a new instance of Generic_BigDecimal
Generic_BigDecimal(int n)
           
 
Method Summary
static java.math.BigDecimal ceilingSignificantDigit(java.math.BigDecimal x)
           
static java.math.BigDecimal divide(java.math.BigDecimal x, java.math.BigDecimal y, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
           
static java.math.BigDecimal divide(java.math.BigDecimal x, java.math.BigInteger y, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
           
static java.math.BigDecimal divide(java.math.BigInteger x, java.math.BigDecimal y, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
           
static java.math.BigDecimal exp(java.math.BigDecimal x, java.math.BigDecimal e, Generic_BigDecimal a_Generic_BigDecimal, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
          e^x = 1 + x/1! +x^2/2! + x^3/3! +...
static java.math.BigDecimal floorSignificantDigit(java.math.BigDecimal x)
           
 java.math.BigDecimal getE(int decimalPlaces)
           
 java.math.BigDecimal getEMinDecimalPlaces(int decimalPlaces)
           
 void init_E(int decimalPlaces)
           
 void init_Factorial_Generic_BigInteger(int n)
           
 void init_PI(int decimalPlaces)
           
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 void main(java.lang.String[] args)
           
static java.math.BigDecimal max(java.math.BigDecimal x, java.math.BigDecimal y)
           
static java.math.BigDecimal min(java.math.BigDecimal x, java.math.BigDecimal y)
           
static java.math.BigDecimal multiply(java.math.BigDecimal x, java.math.BigDecimal y, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
           
static java.math.BigDecimal multiply(java.math.BigDecimal x, java.math.BigInteger y, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
           
static java.math.BigDecimal multiply(java.math.BigInteger x, java.math.BigDecimal y, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
           
static java.math.BigDecimal newtonRaphson(java.math.BigDecimal x, java.math.BigDecimal result0, java.math.BigInteger root, java.math.BigDecimal epsilon, int maxite, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
          Uses MathContext division
static java.math.BigDecimal newtonRaphson0(java.math.BigDecimal x, java.math.BigDecimal result0, java.math.BigInteger root_BigInteger, java.math.BigDecimal epsilon_BigDecimal, int maxite, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
          Uses decimalPlace division
static java.math.BigDecimal power(java.math.BigDecimal x, java.math.BigDecimal y, java.math.BigDecimal e, Generic_BigDecimal a_Generic_BigDecimal, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
          x^y = e^(y * ln(x))
static java.math.BigDecimal power(java.math.BigDecimal x, java.math.BigInteger y0, int div, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
           
static java.math.BigDecimal power(java.math.BigDecimal x, int y, int div, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
           
static boolean powerTestAbove(java.math.BigDecimal compare, java.math.BigDecimal x, java.math.BigInteger y0, 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 powerTestBelow(java.math.BigDecimal compare, java.math.BigDecimal x, java.math.BigInteger y0, 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 java.math.BigDecimal reciprocal(java.math.BigDecimal x, int decimalPlace, java.math.RoundingMode a_RoundingMode)
           
static java.math.BigInteger reciprocalWillBeInteger(java.math.BigDecimal x)
           
static java.math.BigDecimal root(java.math.BigDecimal x, java.math.BigInteger root, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
           
static java.math.BigDecimal root(java.math.BigDecimal x, int root, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
           
static java.math.BigDecimal round(java.math.BigDecimal x, int decimalPlaces, java.math.RoundingMode a_RoundingMode)
          If x is rounded to a BigDecimal equivalent to 0, the trailing Zeros are not stripped!
static java.math.BigDecimal sqrt0(java.math.BigDecimal input, int decimalPlaces)
          Returns the square root of the input BigDecimal as a BigDecimal.
 
Methods inherited from class uk.ac.leeds.ccg.andyt.generic.math.Generic_Number
timeReport
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_Factorial_Generic_BigInteger

public Generic_BigInteger _Factorial_Generic_BigInteger
Factorial of numbers are quite often wanted (especially in Taylor Series calculations), and when they are, this is for them.


_E

public java.math.BigDecimal _E
For storing the Euler–Mascheroni constant correct to a fixed decimal place precision


_PI

public java.math.BigDecimal _PI
For storing the PI constant correct to a fixed decimal place precision


_RoundingMode

public java.math.RoundingMode _RoundingMode
RoundingMode used in calculations


Two

public static final java.math.BigDecimal Two
Two is quite often wanted as a BigDecimal so it is made available as a constant.

Constructor Detail

Generic_BigDecimal

public Generic_BigDecimal()
Creates a new instance of Generic_BigDecimal


Generic_BigDecimal

public Generic_BigDecimal(int n)
Method Detail

init_Factorial_Generic_BigInteger

public final void init_Factorial_Generic_BigInteger(int n)

init_E

public final void init_E(int decimalPlaces)

init_PI

public final void init_PI(int decimalPlaces)

main

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

multiply

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

multiply

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

multiply

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

divide

public static java.math.BigDecimal divide(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;

divide

public static java.math.BigDecimal divide(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;

divide

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

powerTestAbove

public static boolean powerTestAbove(java.math.BigDecimal compare,
                                     java.math.BigDecimal x,
                                     java.math.BigInteger y0,
                                     int div,
                                     int decimalPlaces,
                                     java.math.RoundingMode a_RoundingMode)
Parameters:
compare -
x - x > 1
y0 - y0 > 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 y0,
                                     int div,
                                     int decimalPlaces,
                                     java.math.RoundingMode a_RoundingMode)
Parameters:
compare -
x - x > 0
y0 - y0 > 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
y - x > 1
div - If < 3 this is set to 2. If > 256 set to 256
decimalPlaces -
a_RoundingMode -
Returns:
true iff x^y > 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
y - x > 1
div - If < 3 this is set to 2. If > 256 set to 256
decimalPlaces -
a_RoundingMode -
Returns:
true iff x^y > compare

power

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

Parameters:
x -
y -
decimalPlaces - The number of decimal places the toCompare must be accurate and rounded to if necessary.
Returns:
x^y 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)
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

power

public static java.math.BigDecimal power(java.math.BigDecimal x,
                                         java.math.BigInteger y0,
                                         int div,
                                         int decimalPlaces,
                                         java.math.RoundingMode a_RoundingMode)
Parameters:
x -
y0 -
div - If div < 2 it is set to 2. If div > 256 it is set to 256.
decimalPlaces -
a_RoundingMode -
Returns:
x^y0 accurate to decimalPlaces

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. If x = 0 then an IllegalArgumentException is thrown

reciprocalWillBeInteger

public static java.math.BigInteger reciprocalWillBeInteger(java.math.BigDecimal x)
Parameters:
x -
Returns:
1/x if toCompare is an integer or throws a ArithmeticException. If x = 0 then an IllegalArgumentException 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 root 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

round

public static java.math.BigDecimal round(java.math.BigDecimal x,
                                         int decimalPlaces,
                                         java.math.RoundingMode a_RoundingMode)
If x is rounded to a BigDecimal equivalent to 0, the trailing Zeros are not stripped!

Parameters:
x -
decimalPlaces -
a_RoundingMode -
Returns:
x rounded to decimalPlaces

getEMinDecimalPlaces

public java.math.BigDecimal getEMinDecimalPlaces(int decimalPlaces)

getE

public java.math.BigDecimal getE(int decimalPlaces)

exp

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

Parameters:
x -
decimalPlaces -
Returns:
e^x where e is like _E

max

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

min

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

floorSignificantDigit

public static java.math.BigDecimal floorSignificantDigit(java.math.BigDecimal x)

ceilingSignificantDigit

public static java.math.BigDecimal ceilingSignificantDigit(java.math.BigDecimal x)

root

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

newtonRaphson

public static java.math.BigDecimal newtonRaphson(java.math.BigDecimal x,
                                                 java.math.BigDecimal result0,
                                                 java.math.BigInteger root,
                                                 java.math.BigDecimal epsilon,
                                                 int maxite,
                                                 int decimalPlaces,
                                                 java.math.RoundingMode a_RoundingMode)
Uses MathContext division

Parameters:
x -
result0 -
root -
epsilon -
maxite -
decimalPlaces -
a_RoundingMode -
Returns:
Re-approximation of result0 using Newton Raphson method

newtonRaphson0

public static java.math.BigDecimal newtonRaphson0(java.math.BigDecimal x,
                                                  java.math.BigDecimal result0,
                                                  java.math.BigInteger root_BigInteger,
                                                  java.math.BigDecimal epsilon_BigDecimal,
                                                  int maxite,
                                                  int decimalPlaces,
                                                  java.math.RoundingMode a_RoundingMode)
Uses decimalPlace division

Parameters:
x -
result0 -
root_BigInteger -
epsilon_BigDecimal -
maxite -
decimalPlaces -
a_RoundingMode -
Returns:
Re-approximation of result0 using Newton Raphson method

root

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

sqrt0

public static java.math.BigDecimal sqrt0(java.math.BigDecimal input,
                                         int decimalPlaces)
Returns the square root of the input BigDecimal as a BigDecimal. This can only handle input with a precision of less than Integer.MAX_VALUE. No checking for this is done.

Parameters:
input -
decimalPlaces -
Returns:
Square root of input