Example usage for java.math BigDecimal signum

List of usage examples for java.math BigDecimal signum

Introduction

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

Prototype

public int signum() 

Source Link

Document

Returns the signum function of this BigDecimal .

Usage

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("123");
    BigDecimal bg2 = new BigDecimal("0");
    BigDecimal bg3 = new BigDecimal("-12");

    int i1 = bg1.signum();
    int i2 = bg2.signum();
    int i3 = bg3.signum();

    String str1 = "The Result of Signum function on " + bg1 + " is " + i1;
    String str2 = "The Result of Signum function on " + bg2 + " is " + i2;
    String str3 = "The Result of Signum function on " + bg3 + " is " + i3;

    // print i1,i2,i3 values
    System.out.println(str1);//from w  ww  .j  ava  2s  . c o m
    System.out.println(str2);
    System.out.println(str3);
}

From source file:Main.java

/**
 * Compute the square root of x to a given scale, x >= 0. Use Newton's
 * algorithm.// w ww .j  ava  2  s  .  com
 * 
 * @param x
 *            the value of x
 * @return the result value
 */
public static BigDecimal sqrt(BigDecimal x) {
    // Check that x >= 0.
    if (x.signum() < 0) {
        throw new ArithmeticException("x < 0");
    }

    // n = x*(10^(2*SCALE))
    BigInteger n = x.movePointRight(SCALE << 1).toBigInteger();

    // The first approximation is the upper half of n.
    int bits = (n.bitLength() + 1) >> 1;
    BigInteger ix = n.shiftRight(bits);
    BigInteger ixPrev;

    // Loop until the approximations converge
    // (two successive approximations are equal after rounding).
    do {
        ixPrev = ix;

        // x = (x + n/x)/2
        ix = ix.add(n.divide(ix)).shiftRight(1);

        Thread.yield();
    } while (ix.compareTo(ixPrev) != 0);

    return new BigDecimal(ix, SCALE);
}

From source file:Main.java

/**
 * Compute e^x to a given scale.//from   w w w. ja v a  2  s  .co m
 * 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:org.cryptomath.CryptoMath.java

/**
 * @param value the nullable BigDecimal//ww  w . j av a 2s  .  c  o m
 * @return true if value !=null and &lt;gt; 0.
 */
public static boolean isNotZero(final BigDecimal value) {
    return value != null && value.signum() != 0;
}

From source file:org.yes.cart.service.dto.impl.ComplexSearchUtils.java

/**
 * Check if filter is a number search with prefix
 *
 * Format: [prefix]\d+/*  w  ww.  ja va2  s .  c  om*/
 *
 * E.g. -1, +1.01, #333 where -,+ and # are prefix characters
 *
 * @param filter non empty filter
 * @param binarySortedPrefixes prefixes
 * @param precision precision
 *
 * @return prefix and number
 */
public static Pair<String, BigDecimal> checkNumericSearch(String filter, char[] binarySortedPrefixes,
        int precision) {
    if (filter.length() > 1 && Arrays.binarySearch(binarySortedPrefixes, filter.charAt(0)) >= 0) {
        try {
            final BigDecimal qty = new BigDecimal(filter.substring(1)).setScale(precision,
                    BigDecimal.ROUND_CEILING);
            if (qty.signum() == -1) {
                return null;
            }
            return new Pair<>(filter.substring(0, 1), qty);
        } catch (Exception exp) {
            // do nothing
            return null;
        }
    }
    return null;
}

From source file:org.fede.calculator.service.InvestmentServiceImpl.java

private static BigDecimal pctChange(BigDecimal now, BigDecimal then) {
    if (then.signum() == 0) {
        return ONE;
    }//from  w w  w  .j a va2 s.  c o  m
    return now.subtract(then).divide(then, CONTEXT);
}

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.
 * //from  ww w .  j  a va2s. 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:Main.java

/**
 * Compute the natural logarithm of x to a given scale, x > 0.
 *//*from  ww w .ja va 2s  .  c  o  m*/
public static BigDecimal ln(BigDecimal x, int scale) {
    // Check that x > 0.
    if (x.signum() <= 0) {
        throw new IllegalArgumentException("x <= 0");
    }

    // The number of digits to the left of the decimal point.
    int magnitude = x.toString().length() - x.scale() - 1;

    if (magnitude < 3) {
        return lnNewton(x, scale);
    }

    // Compute magnitude*ln(x^(1/magnitude)).
    else {

        // x^(1/magnitude)
        BigDecimal root = intRoot(x, magnitude, scale);

        // ln(x^(1/magnitude))
        BigDecimal lnRoot = lnNewton(root, scale);

        // magnitude*ln(x^(1/magnitude))
        return BigDecimal.valueOf(magnitude).multiply(lnRoot).setScale(scale, BigDecimal.ROUND_HALF_EVEN);
    }
}

From source file:org.cirdles.geoapp.LatLongToUTM.java

private static char calcHemisphere(BigDecimal latitude) {

    char hemisphere = 'N';

    if (latitude.signum() == -1)
        hemisphere = 'S';

    return hemisphere;
}

From source file:Main.java

/**
 * Compute the integral root of x to a given scale, x >= 0.
 * Use Newton's algorithm./*from w  w  w.j  a va  2 s.  co m*/
 * @param x the value of x
 * @param index the integral root value
 * @param scale the desired scale of the result
 * @return the result value
 */
public static BigDecimal intRoot(BigDecimal x, long index, int scale) {
    // Check that x >= 0.
    if (x.signum() < 0) {
        throw new IllegalArgumentException("x < 0");
    }

    int sp1 = scale + 1;
    BigDecimal n = x;
    BigDecimal i = BigDecimal.valueOf(index);
    BigDecimal im1 = BigDecimal.valueOf(index - 1);
    BigDecimal tolerance = BigDecimal.valueOf(5).movePointLeft(sp1);
    BigDecimal xPrev;

    // The initial approximation is x/index.
    x = x.divide(i, scale, BigDecimal.ROUND_HALF_EVEN);

    // Loop until the approximations converge
    // (two successive approximations are equal after rounding).
    do {
        // x^(index-1)
        BigDecimal xToIm1 = intPower(x, index - 1, sp1);

        // x^index
        BigDecimal xToI = x.multiply(xToIm1).setScale(sp1, BigDecimal.ROUND_HALF_EVEN);

        // n + (index-1)*(x^index)
        BigDecimal numerator = n.add(im1.multiply(xToI)).setScale(sp1, BigDecimal.ROUND_HALF_EVEN);

        // (index*(x^(index-1))
        BigDecimal denominator = i.multiply(xToIm1).setScale(sp1, BigDecimal.ROUND_HALF_EVEN);

        // x = (n + (index-1)*(x^index)) / (index*(x^(index-1)))
        xPrev = x;
        x = numerator.divide(denominator, sp1, BigDecimal.ROUND_DOWN);

        Thread.yield();
    } while (x.subtract(xPrev).abs().compareTo(tolerance) > 0);

    return x;
}