Example usage for java.math BigDecimal negate

List of usage examples for java.math BigDecimal negate

Introduction

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

Prototype

public BigDecimal negate() 

Source Link

Document

Returns a BigDecimal whose value is (-this) , and whose scale is this.scale() .

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    BigDecimal bd1 = new BigDecimal("123456789.0123456890");

    bd1 = bd1.negate();

}

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("12.1234");

    BigDecimal bg2 = bg1.negate();

    System.out.println(bg2);/*  w  w  w .jav  a  2 s  .c o  m*/
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    // Create via a string
    BigDecimal bd1 = new BigDecimal("123456789.0123456890");

    // Create via a long
    BigDecimal bd2 = BigDecimal.valueOf(123L);

    bd1 = bd1.add(bd2);/*from   w w  w  .  j  a  va2s .co m*/
    bd1 = bd1.multiply(bd2);
    bd1 = bd1.subtract(bd2);
    bd1 = bd1.divide(bd2, BigDecimal.ROUND_UP);
    bd1 = bd1.negate();
}

From source file:Main.java

public static void main(String[] args) {
    BigDecimal decimalA = new BigDecimal("123456789012345");
    BigDecimal decimalB = new BigDecimal("10");

    decimalA = decimalA.add(decimalB);/*w ww.j  a  va 2 s .c  om*/
    System.out.println("decimalA = " + decimalA);

    decimalA = decimalA.multiply(decimalB);
    System.out.println("decimalA = " + decimalA);

    decimalA = decimalA.subtract(decimalB);
    System.out.println("decimalA = " + decimalA);

    decimalA = decimalA.divide(decimalB);
    System.out.println("decimalA = " + decimalA);

    decimalA = decimalA.pow(2);
    System.out.println("decimalA = " + decimalA);

    decimalA = decimalA.negate();
    System.out.println("decimalA = " + decimalA);
}

From source file:Main.java

public static Date getDateFromRank(BigDecimal rank) {
    return new Date(rank.negate().scaleByPowerOfTen(3).longValue());
}

From source file:Util.java

public static BigDecimal[] quadratic(BigDecimal a, BigDecimal b, BigDecimal c, RoundingMode rounding) {
    // ax^2 + bx + c = 0
    BigDecimal part1 = b.negate();
    BigDecimal part2 = sqrt(b.multiply(b).subtract(BigDecimalFOUR.multiply(a).multiply(c)), rounding);
    BigDecimal part3 = BigDecimalTWO.multiply(a);
    BigDecimal[] toRet = new BigDecimal[2];
    toRet[0] = (part1.subtract(part2)).divide(part3, RoundingMode.CEILING);
    toRet[1] = (part1.add(part2)).divide(part3, RoundingMode.CEILING);
    return toRet;
}

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.//from  w ww .  j  a va  2  s.c om
 * Break x into its whole and fraction parts and
 * compute (e^(1 + fraction/whole))^whole using Taylor's formula.
 * @param x the value of x
 * @param scale the desired scale of the result
 * @return the result value
 */
public static BigDecimal exp(BigDecimal x, int scale) {
    // e^0 = 1
    if (x.signum() == 0) {
        return BigDecimal.valueOf(1);
    }

    // If x is negative, return 1/(e^-x).
    else if (x.signum() == -1) {
        return BigDecimal.valueOf(1).divide(exp(x.negate(), scale), scale, BigDecimal.ROUND_HALF_EVEN);
    }

    // Compute the whole part of x.
    BigDecimal xWhole = x.setScale(0, BigDecimal.ROUND_DOWN);

    // If there isn't a whole part, compute and return e^x.
    if (xWhole.signum() == 0)
        return expTaylor(x, scale);

    // Compute the fraction part of x.
    BigDecimal xFraction = x.subtract(xWhole);

    // z = 1 + fraction/whole
    BigDecimal z = BigDecimal.valueOf(1).add(xFraction.divide(xWhole, scale, BigDecimal.ROUND_HALF_EVEN));

    // t = e^z
    BigDecimal t = expTaylor(z, scale);

    BigDecimal maxLong = BigDecimal.valueOf(Long.MAX_VALUE);
    BigDecimal result = BigDecimal.valueOf(1);

    // Compute and return t^whole using intPower().
    // If whole > Long.MAX_VALUE, then first compute products
    // of e^Long.MAX_VALUE.
    while (xWhole.compareTo(maxLong) >= 0) {
        result = result.multiply(intPower(t, Long.MAX_VALUE, scale)).setScale(scale,
                BigDecimal.ROUND_HALF_EVEN);
        xWhole = xWhole.subtract(maxLong);

        Thread.yield();
    }
    return result.multiply(intPower(t, xWhole.longValue(), scale)).setScale(scale, BigDecimal.ROUND_HALF_EVEN);
}

From source file:Main.java

/**
 * Compute e^x to a given scale. Break x into its whole and fraction parts
 * and compute (e^(1 + fraction/whole))^whole using Taylor's formula.
 * // w ww.j a v  a2s . c  o  m
 * @param x
 *            the value of x
 * @param scale
 *            the desired scale of the result
 * @return the result value
 */
public static BigDecimal exp(BigDecimal x, int scale) {
    // e^0 = 1
    if (x.signum() == 0) {
        return BigDecimal.valueOf(1);
    }

    // If x is negative, return 1/(e^-x).
    else if (x.signum() == -1) {
        return BigDecimal.valueOf(1).divide(exp(x.negate(), scale), scale, BigDecimal.ROUND_HALF_EVEN);
    }

    // Compute the whole part of x.
    BigDecimal xWhole = x.setScale(0, BigDecimal.ROUND_DOWN);

    // If there isn't a whole part, compute and return e^x.
    if (xWhole.signum() == 0) {
        return expTaylor(x, scale);
    }

    // Compute the fraction part of x.
    BigDecimal xFraction = x.subtract(xWhole);

    // z = 1 + fraction/whole
    BigDecimal z = BigDecimal.valueOf(1).add(xFraction.divide(xWhole, scale, BigDecimal.ROUND_HALF_EVEN));

    // t = e^z
    BigDecimal t = expTaylor(z, scale);

    BigDecimal maxLong = BigDecimal.valueOf(Long.MAX_VALUE);
    BigDecimal result = BigDecimal.valueOf(1);

    // Compute and return t^whole using intPower().
    // If whole > Long.MAX_VALUE, then first compute products
    // of e^Long.MAX_VALUE.
    while (xWhole.compareTo(maxLong) >= 0) {
        result = result.multiply(intPower(t, Long.MAX_VALUE, scale)).setScale(scale,
                BigDecimal.ROUND_HALF_EVEN);
        xWhole = xWhole.subtract(maxLong);

        Thread.yield();
    }
    return result.multiply(intPower(t, xWhole.longValue(), scale)).setScale(scale, BigDecimal.ROUND_HALF_EVEN);
}

From source file:jgnash.ui.report.compiled.MonthlyAccountBalanceChartCompare.java

private static BigDecimal totalModulus(BigDecimal amount, final AccountType type) {
    if (type == AccountType.INCOME || type == AccountType.EQUITY || type == AccountType.CREDIT
            || type == AccountType.LIABILITY) {
        return amount.negate();
    }/*from w  w  w.j av a2 s .  c  o m*/

    return amount;
}