Here you can find the source of mod(int a, int b)
Parameter | Description |
---|---|
a | the dividend |
b | the divisor |
(((a % b) + b) % b)
public static int mod(int a, int b)
//package com.java2s; //License from project: Open Source License public class Main { /**/* ww w . j av a 2s . c om*/ * 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; } }