Java BigInteger Calculate min(BigInteger a, BigInteger b)

Here you can find the source of min(BigInteger a, BigInteger b)

Description

Since Math.max() doesn't handle BigInteger

License

Open Source License

Parameter

Parameter Description
a Can be null
b Can be null

Return

the max of a,b, or null if both are null

Declaration

public static BigInteger min(BigInteger a, BigInteger b) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.math.BigInteger;

import java.util.Collection;

public class Main {
    /**//from  w  ww. j a v  a 2s .  co  m
     * 
     * @param xs
     *            Can contain nulls -- which are ignored
     * @return this will be a member of the xs
     */
    public static <N extends Number> N min(Collection<N> xs) {
        assert !xs.isEmpty();
        double min = Double.POSITIVE_INFINITY;
        N minn = null;
        for (N number : xs) {
            // skip null!
            if (number == null) {
                continue;
            }
            double x = number.doubleValue();
            if (x < min) {
                min = x;
                minn = number;
            }
        }
        return minn;
    }

    /**
     * @param values
     *            Must not be zero-length or null
     * @return min of values (remember that -100 beats 1 - use min + abs if you
     *         want the smallest number)
     */
    public static double min(double... values) {
        double min = values[0];
        for (double i : values) {
            if (i < min) {
                min = i;
            }
        }
        return min;
    }

    /**
     * Since Math.max() doesn't handle BigInteger
     * 
     * @param a Can be null
     * @param b Can be null
     * @return the max of a,b, or null if both are null
     */
    public static BigInteger min(BigInteger a, BigInteger b) {
        if (a == null)
            return b;
        if (b == null)
            return a;
        int c = a.compareTo(b);
        return c > 0 ? b : a;
    }
}

Related

  1. jsonBigInteger(JsonValue value)
  2. length(BigInteger bi)
  3. listToBigInteger(List list)
  4. log2(BigInteger x)
  5. maskBits(BigInteger value, int bits)
  6. modPow(BigInteger base, BigInteger e, BigInteger m)
  7. modPowByte(byte[] arg, BigInteger e, BigInteger n)
  8. modPowLong(BigInteger n, long exponent, BigInteger modulo)
  9. mods(BigInteger v, BigInteger m)