SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.number.big
Class BigDecimalUtils

java.lang.Object
  extended by com.numericalmethod.suanshu.number.big.BigDecimalUtils

public class BigDecimalUtils
extends java.lang.Object

This class collects a set of utility functions for the java class BigDecimal.

See Also:
Class BigDecimal

Field Summary
static java.math.BigDecimal PI
          the value of PI
 
Method Summary
static int compare(java.math.BigDecimal n1, java.math.BigDecimal n2, int precision)
          Compare two BigDecimals up to a precision.
static boolean equals(java.math.BigDecimal n1, java.math.BigDecimal n2, int precision)
          Compare two BigDecimals up to a precision.
static java.math.BigDecimal exp(java.math.BigDecimal x)
          Compute ex.
static java.math.BigDecimal exp(java.math.BigDecimal x, int scale)
          Compute ex.
static java.math.BigDecimal exp(double x)
          Compute ex.
static java.math.BigDecimal exp(double x, int scale)
          Compute ex.
static java.math.BigDecimal getFractional(java.math.BigDecimal num)
          Get the fractional part of this number.
static java.math.BigDecimal getWhole(java.math.BigDecimal num)
          Get the integral part of this number and discard the fractional part.
static java.math.BigDecimal log(java.math.BigDecimal x)
          Compute log(x).
static java.math.BigDecimal log(java.math.BigDecimal x, int scale)
          Compute log(x) up to a scale.
static java.math.BigDecimal pow(java.math.BigDecimal a, java.math.BigDecimal b)
          Compute a to the power of b.
static java.math.BigDecimal pow(java.math.BigDecimal a, java.math.BigDecimal b, int scale)
          Compute a to the power of b.
static java.math.BigDecimal pow(java.math.BigDecimal a, int b)
          Compute a to the power of b where n is an integer.
static java.math.BigDecimal pow(java.math.BigDecimal a, int b, int scale)
          Compute a to the power of b where b is an integer.
static java.math.BigDecimal sum(java.math.BigDecimal... big)
          Compute the sum of an array of BigDecimals.
static java.math.BigDecimal sum(double... big)
          Compute the sum of an array of doubles.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PI

public static final java.math.BigDecimal PI
the value of PI

See Also:
Joy of PI
Method Detail

compare

public static int compare(java.math.BigDecimal n1,
                          java.math.BigDecimal n2,
                          int precision)
Compare two BigDecimals up to a precision. In other words, if the absolute difference between the two numbers falls below a threshold, they are considered equal.

Parameters:
n1 - a BigDecimal
n2 - a BigDecimal
precision - p; the threshold is 1e-p
Returns:
-1, 0, or 1 as n1 is numerically less than, equal to, or greater than n2

equals

public static boolean equals(java.math.BigDecimal n1,
                             java.math.BigDecimal n2,
                             int precision)
Compare two BigDecimals up to a precision. In other words, if the absolute difference between the two numbers falls below a threshold, they are considered equal.

Parameters:
n1 - a BigDecimal
n2 - a BigDecimal
precision - p; the threshold is 1e-p
Returns:
true if the numbers are equal up to a precision

sum

public static java.math.BigDecimal sum(java.math.BigDecimal... big)
Compute the sum of an array of BigDecimals.

Parameters:
big - an array of BigDecimals
Returns:
the sum

sum

public static java.math.BigDecimal sum(double... big)
Compute the sum of an array of doubles.

This is done by first converting the doubles into BigDecimals, and then apply BigDecimal.add(java.math.BigDecimal).

The accuracy is better than first adding them up as doubles and then convert the sum into BigDecimal.

Parameters:
big - an array of BigDecimals
Returns:
the sum

getWhole

public static java.math.BigDecimal getWhole(java.math.BigDecimal num)
Get the integral part of this number and discard the fractional part.

Parameters:
num - a BigDecimal
Returns:
the integral part of this number

getFractional

public static java.math.BigDecimal getFractional(java.math.BigDecimal num)
Get the fractional part of this number. This equals to the number subtracting the whole part.

For -ve. number, the fractional part is also -ve. For example, for -3.1415, the whole is -3 and the fractional part is -0.1415.

Parameters:
num - a BigDecimal
Returns:
the fractional part of this number

pow

public static java.math.BigDecimal pow(java.math.BigDecimal a,
                                       java.math.BigDecimal b)
Compute a to the power of b.

Parameters:
a - the base
b - the exponent
Returns:
ab

pow

public static java.math.BigDecimal pow(java.math.BigDecimal a,
                                       java.math.BigDecimal b,
                                       int scale)
Compute a to the power of b.

Parameters:
a - the base
b - the exponent
scale - the scale for the BigDecimal result; a precision parameter
Returns:
ab

pow

public static java.math.BigDecimal pow(java.math.BigDecimal a,
                                       int b)
Compute a to the power of b where n is an integer.

Parameters:
a - the base
b - the exponent
Returns:
ab

pow

public static java.math.BigDecimal pow(java.math.BigDecimal a,
                                       int b,
                                       int scale)
Compute a to the power of b where b is an integer. This is simply a wrapper around BigDecimal.pow(int) but handles also negative exponents. Use BigDecimal.pow(int) for arbitrary precision if the exponent is positive.

Parameters:
a - the base
b - the exponent
scale - the scale for the BigDecimal result; a precision parameter
Returns:
xn

log

public static java.math.BigDecimal log(java.math.BigDecimal x)
Compute log(x). The base is e, hence the natural log.

Parameters:
x - x
Returns:
log(x)
See Also:
Wikipedia: Natural logarithm

log

public static java.math.BigDecimal log(java.math.BigDecimal x,
                                       int scale)
Compute log(x) up to a scale. The base is e, hence the natural log.

Parameters:
x - x
scale - the scale for the BigDecimal result; a precision parameter
Returns:
log(x)
See Also:
Wikipedia: Natural logarithm

exp

public static java.math.BigDecimal exp(double x)
Compute ex.

Parameters:
x - the exponent
Returns:
ex
See Also:
Wikipedia: Exponential function

exp

public static java.math.BigDecimal exp(double x,
                                       int scale)
Compute ex.

Parameters:
x - the exponent
scale - the scale for the BigDecimal result; a precision parameter
Returns:
ex
See Also:
Wikipedia: Exponential function

exp

public static java.math.BigDecimal exp(java.math.BigDecimal x)
Compute ex.

Parameters:
x - the exponent
Returns:
ex
See Also:
Wikipedia: Exponential function

exp

public static java.math.BigDecimal exp(java.math.BigDecimal x,
                                       int scale)
Compute ex.

Parameters:
x - the exponent
scale - the scale for the BigDecimal result; a precision parameter
Returns:
ex
See Also:
Wikipedia: Exponential function

SuanShu, a Java numerical and statistical library

Copyright © 2011 Numerical Method Inc. Ltd. All Rights Reserved.