Java Integer Mod modulo(int x, int mod)

Here you can find the source of modulo(int x, int mod)

Description

The so-called 'euclidean' modulo, a modulo which won't yield negative results

License

Open Source License

Parameter

Parameter Description
x the number to divide
mod the divisor

Return

the euclidean modulo

Declaration

public static int modulo(int x, int mod) 

Method Source Code

//package com.java2s;
/*/*from   w  ww .j ava 2 s .c  o  m*/
 * Copyright (c) 2016 Fraunhofer IGD
 * 
 * All rights reserved. This program and the accompanying materials are made
 * available under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution. If not, see <http://www.gnu.org/licenses/>.
 * 
 * Contributors:
 *     Fraunhofer IGD <http://www.igd.fraunhofer.de/>
 */

public class Main {
    /**
     * The so-called 'euclidean' modulo, a modulo which won't yield negative
     * results
     * 
     * @param x the number to divide
     * @param mod the divisor
     * @return the euclidean modulo
     */
    public static int modulo(int x, int mod) {
        if (x >= 0) {
            return x % mod;
        }
        int n = 1 + (-x / mod);
        x += n * mod;
        return x % mod;
    }
}

Related

  1. modulateCircularIndex(int index, int seqLength)
  2. modulo(int a, int b)
  3. modulo(int a, int b)
  4. modulo(int dividend, int divisor)
  5. modulo(int x, int m)
  6. modulo(int x, int y)
  7. moduloPositive(final int value, final int size)
  8. moduloPowerOfTwo(final int x, final int powerOfTwoY)
  9. modulus(int a, int b)