Here you can find the source of modulo(int a, int b)
Parameter | Description |
---|---|
a | a parameter |
b | a parameter |
public static int modulo(int a, int b)
//package com.java2s; /* --------------------------------------------------------------------- * Numenta Platform for Intelligent Computing (NuPIC) * Copyright (C) 2014, Numenta, Inc. Unless you have an agreement * with Numenta, Inc., for a separate license for this software code, the * following terms and conditions apply: * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero Public License version 3 as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Affero Public License for more details. * * You should have received a copy of the GNU Affero Public License * along with this program. If not, see http://www.gnu.org/licenses. * * http://numenta.org/licenses//* w w w .j a v a 2s . co m*/ * --------------------------------------------------------------------- */ public class Main { /** * Performs a modulus operation in Python style. * * @param a * @param b * @return */ public static int modulo(int a, int b) { if (b == 0) throw new IllegalArgumentException("Division by Zero!"); if (a > 0 && b > 0 && b > a) return a; boolean isMinus = Math.abs(b - (a - b)) < Math.abs(b - (a + b)); if (isMinus) { while (a >= b) { a -= b; } } else { if (a % b == 0) return 0; while (a + b < b) { a += b; } } return a; } /** * Performs a modulus on every index of the first argument using * the second argument and places the result in the same index of * the first argument. * * @param a * @param b * @return */ public static int[] modulo(int[] a, int b) { for (int i = 0; i < a.length; i++) { a[i] = modulo(a[i], b); } return a; } }