Computes the mathematical modulus of two numbers. - Java java.lang

Java examples for java.lang:Math Number

Description

Computes the mathematical modulus of two numbers.

Demo Code


//package com.java2s;

public class Main {
    /**//from   www .  j a v  a2s.co  m
     * Computes the mathematical modulus of two numbers.
     * 
     * <p>
     * The Java <code>%</code> operator returns a result that adopts the sign of the dividend; however, a true mathematical modulus adopts the sign of the divisor. This method implements a mathematical modulus.
     * </p>
     * 
     * @param a the dividend
     * @param b the divisor
     * @return a mod b; that is, <code>(((a % b) + b) % b)</code>
     */
    public static int mod(int a, int b) {
        return ((a % b) + b) % b;
    }
}

Related Tutorials