Example usage for java.math BigInteger negate

List of usage examples for java.math BigInteger negate

Introduction

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

Prototype

public BigInteger negate() 

Source Link

Document

Returns a BigInteger whose value is (-this) .

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    BigInteger bi1 = new BigInteger("1234567890123456890");

    bi1 = bi1.negate();

}

From source file:Main.java

public static void main(String[] args) {

    BigInteger bi1 = new BigInteger("20");

    // assign negate value of bi1 to bi2
    BigInteger bi2 = bi1.negate();

    System.out.println(bi2);//from ww  w .  j a  va 2  s  .  c om
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    BigInteger bi1 = new BigInteger("1234567890123456890");
    BigInteger bi2 = BigInteger.valueOf(123L);

    bi1 = bi1.multiply(bi2);/*  w  ww .ja v  a  2 s . c  o m*/

    bi1 = bi1.subtract(bi2);

    bi1 = bi1.divide(bi2);

    bi1 = bi1.negate();

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    // Create via a string
    BigInteger bi1 = new BigInteger("1234567890123456890");

    // Create via a long
    BigInteger bi2 = BigInteger.valueOf(123L);

    bi1 = bi1.add(bi2);//from ww  w.j  av  a  2 s . c o m
    bi1 = bi1.multiply(bi2);
    bi1 = bi1.subtract(bi2);
    bi1 = bi1.divide(bi2);
    bi1 = bi1.negate();
    int exponent = 2;
    bi1 = bi1.pow(exponent);

}

From source file:Main.java

public static void main(String[] args) {
    BigInteger numberA = new BigInteger("98765432123456789");
    BigInteger numberB = BigInteger.TEN;

    numberA = numberA.add(numberB);/* w w  w  .ja  v a  2  s .c om*/
    System.out.println("numberA = " + numberA);

    numberA = numberA.multiply(numberB);
    System.out.println("numberA = " + numberA);

    numberA = numberA.subtract(numberB);
    System.out.println("numberA = " + numberA);

    numberA = numberA.divide(numberB);
    System.out.println("numberA = " + numberA);

    numberA = numberA.mod(numberB);
    System.out.println("numberA = " + numberA);

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

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

From source file:com.examples.with.different.packagename.ClassNumberUtils.java

public static BigInteger createBigInteger(final String str) {
    if (str == null) {
        return null;
    }//from   w ww  .  j a v a 2s.co  m
    int pos = 0; // offset within string
    int radix = 10;
    boolean negate = false; // need to negate later?
    if (str.startsWith("-")) {
        negate = true;
        pos = 1;
    }
    if (str.startsWith("0x", pos) || str.startsWith("0X", pos)) { // hex
        radix = 16;
        pos += 2;
    } else if (str.startsWith("#", pos)) { // alternative hex (allowed by Long/Integer)
        radix = 16;
        pos++;
    } else if (str.startsWith("0", pos) && str.length() > pos + 1) { // octal; so long as there are additional digits
        radix = 8;
        pos++;
    } // default is to treat as decimal

    final BigInteger value = new BigInteger(str.substring(pos), radix);
    return negate ? value.negate() : value;
}

From source file:NumberUtils.java

/**
 * Decode a {@link java.math.BigInteger} from a {@link String} value.
 * Supports decimal, hex and octal notation.
 * @see BigInteger#BigInteger(String, int)
 *///from w w w.j a  v a2  s .  com
private static BigInteger decodeBigInteger(String value) {
    int radix = 10;
    int index = 0;
    boolean negative = false;

    // Handle minus sign, if present.
    if (value.startsWith("-")) {
        negative = true;
        index++;
    }

    // Handle radix specifier, if present.
    if (value.startsWith("0x", index) || value.startsWith("0X", index)) {
        index += 2;
        radix = 16;
    } else if (value.startsWith("#", index)) {
        index++;
        radix = 16;
    } else if (value.startsWith("0", index) && value.length() > 1 + index) {
        index++;
        radix = 8;
    }

    BigInteger result = new BigInteger(value.substring(index), radix);
    return (negative ? result.negate() : result);
}

From source file:com.livinglogic.ul4.FunctionFormat.java

public static String call(BigInteger obj, String formatString, Locale locale) {
    IntegerFormat format = new IntegerFormat(formatString);

    if (locale == null)
        locale = Locale.ENGLISH;//  w w  w .j a  v  a 2  s. c om

    String output = null;

    boolean neg = obj.signum() < 0;
    if (neg)
        obj = obj.negate();

    switch (format.type) {
    case 'b':
        output = obj.toString(2);
        break;
    case 'c':
        if (neg || obj.compareTo(new BigInteger("65535")) > 0)
            throw new RuntimeException("value out of bounds for c format");
        output = Character.toString((char) obj.intValue());
        break;
    case 'd':
        output = obj.toString();
        break;
    case 'o':
        output = obj.toString(8);
        break;
    case 'x':
        output = obj.toString(16);
        break;
    case 'X':
        output = obj.toString(16).toUpperCase();
        break;
    case 'n':
        // FIXME: locale formatting
        output = obj.toString();
        break;
    }
    return formatIntegerString(output, neg, format);
}

From source file:cc.redberry.core.number.Numeric.java

@Override
public Numeric subtract(BigInteger bg) {
    return add(bg.negate());
}

From source file:cc.redberry.core.number.Complex.java

@Override
public Complex pow(BigInteger exponent) {
    if (exponent.compareTo(BigInteger.ZERO) < 0)
        return reciprocal().pow(exponent.negate());

    Complex result = Complex.ONE;//from w ww . ja v  a2  s.c  om
    Complex k2p = this;
    while (!BigInteger.ZERO.equals(exponent)) {
        if (exponent.testBit(0))
            result = result.multiply(k2p);

        k2p = k2p.multiply(k2p);
        exponent = exponent.shiftRight(1);
    }

    return result;
}