Example usage for java.math BigDecimal ONE

List of usage examples for java.math BigDecimal ONE

Introduction

In this page you can find the example usage for java.math BigDecimal ONE.

Prototype

BigDecimal ONE

To view the source code for java.math BigDecimal ONE.

Click Source Link

Document

The value 1, with a scale of 0.

Usage

From source file:Main.java

public static void main(String[] args) {

    System.out.println(BigDecimal.ONE);
}

From source file:Main.java

public static BigDecimal subtractVatAmount(BigDecimal percentage, BigDecimal amount) {
    BigDecimal f = BigDecimal.ONE
            .subtract(BigDecimal.ONE.divide(percentage.add(HUNDRED).divide(HUNDRED), 14, RoundingMode.HALF_UP));
    return amount.multiply(f).setScale(2, RoundingMode.HALF_UP);
}

From source file:Main.java

public static BigDecimal cosine(BigDecimal x) {

    BigDecimal currentValue = BigDecimal.ONE;
    BigDecimal lastVal = currentValue.add(BigDecimal.ONE);
    BigDecimal xSquared = x.multiply(x);
    BigDecimal numerator = BigDecimal.ONE;
    BigDecimal denominator = BigDecimal.ONE;
    int i = 0;/*w  w w  .j a  va 2 s .  c  om*/

    while (lastVal.compareTo(currentValue) != 0) {
        lastVal = currentValue;

        int z = 2 * i + 2;

        denominator = denominator.multiply(BigDecimal.valueOf(z));
        denominator = denominator.multiply(BigDecimal.valueOf(z - 1));
        numerator = numerator.multiply(xSquared);

        BigDecimal term = numerator.divide(denominator, SCALE + 5, ROUNDING_MODE);

        if (i % 2 == 0) {
            currentValue = currentValue.subtract(term);
        } else {
            currentValue = currentValue.add(term);
        }
        i++;
    }

    return currentValue;
}

From source file:Main.java

public static BigDecimal cosine(BigDecimal x) {

    BigDecimal currentValue = BigDecimal.ONE;
    BigDecimal lastVal = currentValue.add(BigDecimal.ONE);
    BigDecimal xSquared = x.multiply(x);
    BigDecimal numerator = BigDecimal.ONE;
    BigDecimal denominator = BigDecimal.ONE;
    int i = 0;//  w w  w  .  j  av  a2 s  .  c o m

    while (lastVal.compareTo(currentValue) != 0) {
        lastVal = currentValue;

        int z = 2 * i + 2;

        denominator = denominator.multiply(BigDecimal.valueOf(z));
        denominator = denominator.multiply(BigDecimal.valueOf(z - 1));
        numerator = numerator.multiply(xSquared);

        BigDecimal term = numerator.divide(denominator, SCALE + 5, ROUNDING_MODE);

        if (i % 2 != 0) {
            currentValue = currentValue.add(term);
        } else {
            currentValue = currentValue.subtract(term);
        }
        i++;
    }

    return currentValue;
}

From source file:Main.java

public static BigDecimal sine(BigDecimal x) {
    BigDecimal lastVal = x.add(BigDecimal.ONE);
    BigDecimal currentValue = x;//w w w  .  j a v  a2s .co m
    BigDecimal xSquared = x.multiply(x);
    BigDecimal numerator = x;
    BigDecimal denominator = BigDecimal.ONE;
    int i = 0;

    while (lastVal.compareTo(currentValue) != 0) {
        lastVal = currentValue;

        int z = 2 * i + 3;

        denominator = denominator.multiply(BigDecimal.valueOf(z));
        denominator = denominator.multiply(BigDecimal.valueOf(z - 1));
        numerator = numerator.multiply(xSquared);

        BigDecimal term = numerator.divide(denominator, SCALE + 5, ROUNDING_MODE);

        if (i % 2 == 0) {
            currentValue = currentValue.subtract(term);
        } else {
            currentValue = currentValue.add(term);
        }

        i++;
    }
    return currentValue;
}

From source file:Main.java

public static BigDecimal sine(BigDecimal x) {
    BigDecimal lastVal = x.add(BigDecimal.ONE);
    BigDecimal currentValue = x;/*from  w  ww . j ava2  s .  c om*/
    BigDecimal xSquared = x.multiply(x);
    BigDecimal numerator = x;
    BigDecimal denominator = BigDecimal.ONE;
    int i = 0;

    while (lastVal.compareTo(currentValue) != 0) {
        lastVal = currentValue;

        int z = 2 * i + 3;

        denominator = denominator.multiply(BigDecimal.valueOf(z));
        denominator = denominator.multiply(BigDecimal.valueOf(z - 1));
        numerator = numerator.multiply(xSquared);

        BigDecimal term = numerator.divide(denominator, SCALE + 5, ROUNDING_MODE);

        if (i % 2 != 0) {
            currentValue = currentValue.add(term);
        } else {
            currentValue = currentValue.subtract(term);
        }

        i++;
    }
    return currentValue;
}

From source file:org.apache.fineract.portfolio.servicecharge.util.ServiceChargeOperationUtils.java

public static BigDecimal divideAndMultiplyNonZeroValues(BigDecimal operand, BigDecimal divisor,
        BigDecimal multiplicand) {
    if (operand == null) {
        return BigDecimal.ONE;
    }/*  w w w  .j a v a  2 s . c  o m*/
    if (divisor != null && !divisor.equals(BigDecimal.ZERO)) {
        operand = operand.divide(divisor, RoundingMode.HALF_UP);
    }
    if (multiplicand != null) {
        operand = operand.multiply(multiplicand);
    }
    return operand;
}

From source file:org.cirdles.calamari.algorithms.TukeyBiweight.java

public static ValueModel calculateTukeyBiweightMean(String name, double tuningConstant, double[] values) {
    // guarantee termination
    BigDecimal epsilon = BigDecimal.ONE.movePointLeft(10);
    int iterationMax = 100;
    int iterationCounter = 0;

    int n = values.length;
    // initial mean is median
    BigDecimal mean = new BigDecimal(calculateMedian(values));

    // initial sigma is median absolute deviation from mean = median (MAD)
    double deviations[] = new double[n];
    for (int i = 0; i < values.length; i++) {
        deviations[i] = StrictMath.abs(values[i] - mean.doubleValue());
    }/*from www  .  j  a  v a2s  .  c  o m*/
    BigDecimal sigma = new BigDecimal(calculateMedian(deviations)).max(BigDecimal.valueOf(SQUID_TINY_VALUE));

    BigDecimal previousMean;
    BigDecimal previousSigma;

    do {
        iterationCounter++;
        previousMean = mean;
        previousSigma = sigma;

        // init to zeroes
        BigDecimal[] deltas = new BigDecimal[n];
        BigDecimal[] u = new BigDecimal[n];
        BigDecimal sa = BigDecimal.ZERO;
        BigDecimal sb = BigDecimal.ZERO;
        BigDecimal sc = BigDecimal.ZERO;

        BigDecimal tee = new BigDecimal(tuningConstant).multiply(sigma);

        for (int i = 0; i < n; i++) {
            deltas[i] = new BigDecimal(values[i]).subtract(mean);
            if (tee.compareTo(deltas[i].abs()) > 0) {
                deltas[i] = new BigDecimal(values[i]).subtract(mean);
                u[i] = deltas[i].divide(tee, MathContext.DECIMAL128);
                BigDecimal uSquared = u[i].multiply(u[i]);
                sa = sa.add(deltas[i].multiply(BigDecimal.ONE.subtract(uSquared).pow(2)).pow(2));
                sb = sb.add(BigDecimal.ONE.subtract(uSquared)
                        .multiply(BigDecimal.ONE.subtract(new BigDecimal(5.0).multiply(uSquared))));
                sc = sc.add(u[i].multiply(BigDecimal.ONE.subtract(uSquared).pow(2)));
            }
        }

        sigma = bigDecimalSqrtBabylonian(sa.multiply(new BigDecimal(n))).divide(sb.abs(),
                MathContext.DECIMAL128);
        sigma = sigma.max(BigDecimal.valueOf(SQUID_TINY_VALUE));
        mean = previousMean.add(tee.multiply(sc).divide(sb, MathContext.DECIMAL128));

    } // both tests against epsilon must pass OR iterations top out
      // april 2016 Simon B discovered we need 101 iterations possible, hence the "<=" below
    while (((sigma.subtract(previousSigma).abs().divide(sigma, MathContext.DECIMAL128).compareTo(epsilon) > 0)//
            || mean.subtract(previousMean).abs().divide(mean, MathContext.DECIMAL128).compareTo(epsilon) > 0)//
            && (iterationCounter <= iterationMax));

    return new ValueModel(name, mean, "ABS", sigma);
}

From source file:Main.java

public static BigDecimal log10(BigDecimal b) {
    final int NUM_OF_DIGITS = SCALE + 2;
    // need to add one to get the right number of dp
    //  and then add one again to get the next number
    //  so I can round it correctly.

    MathContext mc = new MathContext(NUM_OF_DIGITS, RoundingMode.HALF_EVEN);
    //special conditions:
    // log(-x) -> exception
    // log(1) == 0 exactly;
    // log of a number lessthan one = -log(1/x)
    if (b.signum() <= 0) {
        throw new ArithmeticException("log of a negative number! (or zero)");
    } else if (b.compareTo(BigDecimal.ONE) == 0) {
        return BigDecimal.ZERO;
    } else if (b.compareTo(BigDecimal.ONE) < 0) {
        return (log10((BigDecimal.ONE).divide(b, mc))).negate();
    }//  w  w w.  java 2 s . co m

    StringBuilder sb = new StringBuilder();
    //number of digits on the left of the decimal point
    int leftDigits = b.precision() - b.scale();

    //so, the first digits of the log10 are:
    sb.append(leftDigits - 1).append(".");

    //this is the algorithm outlined in the webpage
    int n = 0;
    while (n < NUM_OF_DIGITS) {
        b = (b.movePointLeft(leftDigits - 1)).pow(10, mc);
        leftDigits = b.precision() - b.scale();
        sb.append(leftDigits - 1);
        n++;
    }

    BigDecimal ans = new BigDecimal(sb.toString());

    //Round the number to the correct number of decimal places.
    ans = ans.round(new MathContext(ans.precision() - ans.scale() + SCALE, RoundingMode.HALF_EVEN));
    return ans;
}

From source file:Main.java

public static BigDecimal log10(BigDecimal b) {
    final int NUM_OF_DIGITS = SCALE + 2;
    // need to add one to get the right number of dp
    // and then add one again to get the next number
    // so I can round it correctly.

    MathContext mc = new MathContext(NUM_OF_DIGITS, RoundingMode.HALF_EVEN);
    // special conditions:
    // log(-x) -> exception
    // log(1) == 0 exactly;
    // log of a number lessthan one = -log(1/x)
    if (b.signum() <= 0) {
        throw new ArithmeticException("log of a negative number! (or zero)");
    } else if (b.compareTo(BigDecimal.ONE) == 0) {
        return BigDecimal.ZERO;
    } else if (b.compareTo(BigDecimal.ONE) < 0) {
        return (log10((BigDecimal.ONE).divide(b, mc))).negate();
    }//ww w.j  a  v  a  2  s . co  m

    StringBuilder sb = new StringBuilder();
    // number of digits on the left of the decimal point
    int leftDigits = b.precision() - b.scale();

    // so, the first digits of the log10 are:
    sb.append(leftDigits - 1).append(".");

    // this is the algorithm outlined in the webpage
    int n = 0;
    while (n < NUM_OF_DIGITS) {
        b = (b.movePointLeft(leftDigits - 1)).pow(10, mc);
        leftDigits = b.precision() - b.scale();
        sb.append(leftDigits - 1);
        n++;
    }

    BigDecimal ans = new BigDecimal(sb.toString());

    // Round the number to the correct number of decimal places.
    ans = ans.round(new MathContext(ans.precision() - ans.scale() + SCALE, RoundingMode.HALF_EVEN));
    return ans;
}