This function returns the largest positive remainder of b and m. - Java java.lang

Java examples for java.lang:Math Algorithm

Description

This function returns the largest positive remainder of b and m.

Demo Code


//package com.java2s;
import java.math.BigInteger;

public class Main {
    public static void main(String[] argv) throws Exception {
        BigInteger b = new BigInteger("1234");
        BigInteger m = new BigInteger("1234");
        System.out.println(posMod(b, m));
    }/* w  w  w  . j  a  v  a  2s . c om*/

    private static BigInteger ZERO = new BigInteger("0");

    /**
     * This function returns the largest positive remainder of b and m.
     * @param b
     * @param m
     * @return BigInteger
     */
    public static BigInteger posMod(BigInteger b, BigInteger m) {
        BigInteger answer = b.mod(m);
        if (answer.compareTo(ZERO) < 0)
            return answer;
        else
            return answer.add(m);
    }
}

Related Tutorials