Example usage for java.math BigDecimal add

List of usage examples for java.math BigDecimal add

Introduction

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

Prototype

public BigDecimal add(BigDecimal augend) 

Source Link

Document

Returns a BigDecimal whose value is (this + augend) , and whose scale is max(this.scale(), augend.scale()) .

Usage

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;/*from  w  ww .j  a  v  a2 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.add(term);
        } else {
            currentValue = currentValue.subtract(term);
        }
        i++;
    }

    return currentValue;
}

From source file:com.surfs.storage.web.utils.Stringutils.java

public static String sumSpace(String totalSpace, String currentSpace) {
    if (StringUtils.isBlank(totalSpace)) {
        return convertToKB(currentSpace);
    }/*from ww w  .  j  a v  a2  s.co m*/

    //String total = convertToKB(totalSpace);
    String current = convertToKB(currentSpace);
    BigDecimal sum = new BigDecimal(totalSpace);
    long sumNum = sum.add(new BigDecimal(current)).longValue();

    //BigDecimal sumTB = new BigDecimal(sumNum);
    //double divSum = sumTB.divide(new BigDecimal("1048576"), 2, BigDecimal.ROUND_HALF_UP).doubleValue();

    //return String.valueOf(divSum).concat("T");
    return String.valueOf(sumNum);
}

From source file:Main.java

public static BigDecimal sine(BigDecimal x) {
    BigDecimal lastVal = x.add(BigDecimal.ONE);
    BigDecimal currentValue = x;/* w ww .  j  av a 2 s  .  com*/
    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 ww w. ja v  a2 s . 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.add(term);
        } else {
            currentValue = currentValue.subtract(term);
        }

        i++;
    }
    return currentValue;
}

From source file:Main.java

public static Double add(Number value1, Number value2) {
    BigDecimal b1 = new BigDecimal(Double.toString(value1.doubleValue()));
    BigDecimal b2 = new BigDecimal(Double.toString(value2.doubleValue()));
    return b1.add(b2).doubleValue();
}

From source file:io.seldon.prediction.VariationPredictionStrategy.java

public static VariationPredictionStrategy build(List<Variation> variations) {
    Map<Range, SimplePredictionStrategy> strategyMap = new LinkedHashMap<>();
    BigDecimal ratioTotal = BigDecimal.ZERO;
    for (Variation var : variations) {
        ratioTotal = ratioTotal.add(var.ratio);
    }// ww  w  . j  av a 2 s .  com
    BigDecimal currentMax = BigDecimal.ZERO;
    for (Variation var : variations) {
        NumberRange range = new NumberRange(currentMax,
                currentMax.add(var.ratio.divide(ratioTotal, 5, BigDecimal.ROUND_UP)));
        strategyMap.put(range, var.variationStrategy);
        currentMax = currentMax.add(var.ratio);
    }
    return new VariationPredictionStrategy(strategyMap);
}

From source file:io.seldon.recommendation.VariationTestingClientStrategy.java

public static VariationTestingClientStrategy build(List<Variation> variations) {
    Map<Range, ClientStrategy> strategyMap = new LinkedHashMap<>();
    BigDecimal ratioTotal = BigDecimal.ZERO;
    for (Variation var : variations) {
        ratioTotal = ratioTotal.add(var.ratio);
    }/*from   ww w  . ja v  a  2 s . co m*/
    BigDecimal currentMax = BigDecimal.ZERO;
    for (Variation var : variations) {
        NumberRange range = new NumberRange(currentMax,
                currentMax.add(var.ratio.divide(ratioTotal, 5, BigDecimal.ROUND_UP)));
        strategyMap.put(range, var.variationStrategy);
        currentMax = currentMax.add(var.ratio);
    }
    return new VariationTestingClientStrategy(strategyMap);
}

From source file:Main.java

/**
 * H = -[0.5 * lg(0.5) + 0.4*lg(0.4) + 0.1*lg(0.1)]
 *//*from   w  w w . j a v a 2  s  .c o m*/
public static BigDecimal calculateEntropy(double... probabilities) {
    BigDecimal res = BigDecimal.ZERO;

    for (double singleProbability : probabilities) {

        BigDecimal singleEntropy = BigDecimal.valueOf(singleProbability)
                .multiply(BigDecimal.valueOf(lg2(singleProbability)));

        res = res.add(singleEntropy);
    }

    return res.negate().setScale(ENTROPY_SCALE, BigDecimal.ROUND_HALF_UP);
}

From source file:Main.java

/**
 * Compute e^x to a given scale by the Taylor series.
 * @param x the value of x//from w  w w  .  ja v a2 s.c o  m
 * @param scale the desired scale of the result
 * @return the result value
 */
private static BigDecimal expTaylor(BigDecimal x, int scale) {
    BigDecimal factorial = BigDecimal.valueOf(1);
    BigDecimal xPower = x;
    BigDecimal sumPrev;

    // 1 + x
    BigDecimal sum = x.add(BigDecimal.valueOf(1));

    // Loop until the sums converge
    // (two successive sums are equal after rounding).
    int i = 2;
    do {
        // x^i
        xPower = xPower.multiply(x).setScale(scale, BigDecimal.ROUND_HALF_EVEN);

        // i!
        factorial = factorial.multiply(BigDecimal.valueOf(i));

        // x^i/i!
        BigDecimal term = xPower.divide(factorial, scale, BigDecimal.ROUND_HALF_EVEN);

        // sum = sum + x^i/i!
        sumPrev = sum;
        sum = sum.add(term);

        ++i;
        Thread.yield();
    } while (sum.compareTo(sumPrev) != 0);

    return sum;
}

From source file:com.spaceprogram.simplejpa.util.AmazonSimpleDBUtil.java

public static String encodeRealNumberRange(BigDecimal number, int maxNumDigits, BigDecimal offsetValue) {
    BigDecimal offsetNumber = number.add(offsetValue);
    String longString = offsetNumber.toString();
    int numZeroes = maxNumDigits - longString.length();
    StringBuffer strBuffer = new StringBuffer(numZeroes + longString.length());
    for (int i = 0; i < numZeroes; i++) {
        strBuffer.insert(i, '0');
    }//from w ww  .  ja va  2s . c  om
    strBuffer.append(longString);
    return strBuffer.toString();
}