Here you can find the source of modulo(int x, int mod)
Parameter | Description |
---|---|
x | the number to divide |
mod | the divisor |
public static int modulo(int x, int mod)
//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; } }