Example usage for java.math BigInteger abs

List of usage examples for java.math BigInteger abs

Introduction

In this page you can find the example usage for java.math BigInteger abs.

Prototype

public BigInteger abs() 

Source Link

Document

Returns a BigInteger whose value is the absolute value of this BigInteger.

Usage

From source file:Main.java

public static void main(String[] args) {

    BigInteger bi1 = new BigInteger("123");
    BigInteger bi2 = new BigInteger("-123");

    // assign absolute values of bi1, bi2 to bi3, bi4
    BigInteger bi3 = bi1.abs();
    BigInteger bi4 = bi2.abs();//  w w  w . j  av a2  s . c  om

    System.out.println(bi3);
    System.out.println(bi4);
}

From source file:com.facebook.infrastructure.utils.FBUtilities.java

public static BigInteger hash(String data) {
    byte[] result = hash(HashingSchemes.SHA1, data.getBytes());
    BigInteger hash = new BigInteger(result);
    return hash.abs();
}

From source file:com.bigdata.dastor.utils.FBUtilities.java

public static BigInteger hash(String data) {
    byte[] result = hash("MD5", data.getBytes());
    BigInteger hash = new BigInteger(result);
    return hash.abs();
}

From source file:com.stratio.cassandra.lucene.schema.mapping.BigIntegerMapper.java

/** {@inheritDoc} */
@Override//from   w w w .  j  a v a2 s  .  co  m
protected String doBase(String name, Object value) {

    // Parse big decimal
    String svalue = value.toString();
    BigInteger bi;
    try {
        bi = new BigInteger(svalue);
    } catch (NumberFormatException e) {
        throw new IndexException("Field '%s' requires a base 10 integer, but found '%s'", name, svalue);
    }

    // Check size
    if (bi.abs().toString().length() > digits) {
        throw new IndexException("Field '%s' with value '%s' has more than %d digits", name, value, digits);
    }

    // Map
    bi = bi.add(complement);
    String bis = encode(bi);
    return StringUtils.leftPad(bis, hexDigits + 1, '0');
}

From source file:com.stratio.cassandra.index.schema.ColumnMapperBigInteger.java

/** {@inheritDoc} */
@Override//  ww  w  .  j  a v  a 2s. c  o  m
public String indexValue(String name, Object value) {

    // Check not null
    if (value == null) {
        return null;
    }

    // Parse big decimal
    String svalue = value.toString();
    BigInteger bi;
    try {
        bi = new BigInteger(svalue);
    } catch (NumberFormatException e) {
        String message = String.format("Field %s requires a base 10 integer, but found \"%s\"", name, svalue);
        throw new IllegalArgumentException(message);
    }

    // Check size
    if (bi.abs().toString().length() > digits) {
        throw new IllegalArgumentException("Value has more than " + digits + " digits");
    }

    // Map
    bi = bi.add(complement);
    String bis = encode(bi);
    return StringUtils.leftPad(bis, hexDigits + 1, '0');
}

From source file:burstcoin.observer.service.AssetService.java

private String convertPrice(String priceString, int decimals) {
    BigInteger price = new BigInteger(priceString);
    BigInteger amount = price.multiply(new BigInteger("" + (long) Math.pow(10, decimals)));
    String negative = "";
    String afterComma = "";
    String fractionalPart = amount.mod(new BigInteger("100000000")).toString();
    amount = amount.divide(new BigInteger("100000000"));
    if (amount.compareTo(BigInteger.ZERO) < 0) {
        amount = amount.abs();
        negative = "-";
    }//  w w  w. j  ava  2  s  . c  o m
    if (!fractionalPart.equals("0")) {
        afterComma = ".";
        for (int i = fractionalPart.length(); i < 8; i++) {
            afterComma += "0";
        }
        afterComma += fractionalPart.replace("0+$", "");
    }
    String result = negative + amount + afterComma;
    while (result.lastIndexOf("0") == result.length() - 1 && result.contains(".")) {
        result = result.substring(0, result.length() - 1);
    }
    if (result.lastIndexOf(".") == result.length() - 1) {
        result = result.substring(0, result.length() - 1);
    }
    return result;
}

From source file:net.pms.util.Rational.java

/**
 * Calculates the greatest common divisor for two {@link BigInteger}s using
 * {@link BigInteger#gcd}.//  w ww .  ja  v a 2 s . com
 *
 * @param u the first number.
 * @param v the second number.
 * @return The GDC, always 1 or greater.
 */
@Nullable
public static BigInteger calculateGreatestCommonDivisor(@Nullable BigInteger u, @Nullable BigInteger v) {
    if (u == null || v == null) {
        return null;
    }
    if (u.abs().compareTo(BigInteger.ONE) <= 0 || v.abs().compareTo(BigInteger.ONE) <= 0) {
        return BigInteger.ONE;
    }
    return u.gcd(v);
}

From source file:net.pms.util.Rational.java

/**
 * Calculates the least common multiple for two {@link BigInteger}s using the formula
 * {@code u * v / gcd(u, v)} where {@code gcd} is the greatest common divisor for the two.
 *
 * @param u the first number.//from   ww w.jav a  2s  . co  m
 * @param v the second number.
 * @return The LCM, always 1 or greater.
 */
@Nullable
public static BigInteger calculateLeastCommonMultiple(@Nullable BigInteger u, @Nullable BigInteger v) {
    if (u == null || v == null) {
        return null;
    }
    if (u.signum() == 0 && v.signum() == 0) {
        return BigInteger.ONE;
    }
    u = u.abs();
    v = v.abs();
    if (u.signum() == 0) {
        return v;
    }
    if (v.signum() == 0) {
        return u;
    }
    return u.divide(calculateGreatestCommonDivisor(u, v)).multiply(v);
}

From source file:net.pms.util.Rational.java

/**
 * Returns a {@link Rational} whose value is {@code (this / value)}.
 *
 * @param value the value by which this {@link Rational} is to be divided.
 * @return The division result./*from w w  w  . ja  v  a 2 s .c om*/
 */
@Nullable
public Rational divide(@Nullable BigInteger value) {
    if (value == null) {
        return null;
    }
    if (isNaN()) {
        return NaN;
    }

    if (value.signum() == 0) {
        if (signum() == 0) {
            return NaN;
        }
        return signum() > 0 ? POSITIVE_INFINITY : NEGATIVE_INFINITY;
    }

    if (signum() == 0 || isInfinite() || BigInteger.ONE.equals(value.abs())) {
        return value.signum() < 0 ? negate() : this;
    }

    // Keep the sign in the numerator and the denominator positive
    if (value.signum() < 0) {
        return valueOf(reducedNumerator.negate(), reducedDenominator.multiply(value.negate()));
    }
    return valueOf(reducedNumerator, reducedDenominator.multiply(value));
}

From source file:net.pms.util.Rational.java

/**
 * Returns a {@link Rational} whose value is {@code (this * value)}.
 *
 * @param value the value to be multiplied by this {@link Rational}.
 * @return The multiplication result.// ww  w .  j  a va  2  s  .  c  om
 */
@Nullable
public Rational multiply(@Nullable BigInteger value) {
    if (value == null) {
        return null;
    }
    if (isNaN()) {
        return NaN;
    }
    if (isInfinite()) {
        if (value.signum() == 0) {
            return NaN; // Infinity by zero
        }
        return numerator.signum() == value.signum() ? POSITIVE_INFINITY : NEGATIVE_INFINITY;
    }
    if (value.signum() == 0) {
        return ZERO;
    }

    if (BigInteger.ONE.equals(value.abs())) {
        return value.signum() < 0 ? negate() : this;
    }

    return valueOf(reducedNumerator.multiply(value), reducedDenominator);
}