Java Long Number Mod modulo(final long x, final long y)

Here you can find the source of modulo(final long x, final long y)

Description

Modulo, with correction for negative dividends.

License

Open Source License

Parameter

Parameter Description
x dividend
y divisor

Return

x % y, non-negative

Declaration

public static long modulo(final long x, final long y) 

Method Source Code

//package com.java2s;
/* Util.java//  w w w  .ja v a  2s  .  c  o  m
 *
 * Copyright (C) 2015, Tom? Pecina <tomas@pecina.cz>
 *
 * This file is part of cz.pecina.retro, retro 8-bit computer emulators.
 *
 * This application is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This application 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 General Public License for more details.         
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Modulo, with correction for negative dividends.
     * The result is always non-negative.
     *
     * @param  x dividend
     * @param  y divisor
     * @return   x % y, non-negative
     */
    public static int modulo(final int x, final int y) {
        return ((x % y) + y) % y;
    }

    /**
     * Modulo, with correction for negative dividends.
     * The result is always non-negative.
     *
     * @param  x dividend
     * @param  y divisor
     * @return   x % y, non-negative
     */
    public static long modulo(final long x, final long y) {
        return ((x % y) + y) % y;
    }

    /**
     * Modulo, with correction for negative dividends.
     * The result is always non-negative.
     *
     * @param  x dividend
     * @param  y divisor
     * @return   x % y, non-negative
     */
    public static float modulo(final float x, final float y) {
        return ((x % y) + y) % y;
    }

    /**
     * Modulo, with correction for negative dividends.
     * The result is always non-negative.
     *
     * @param  x dividend
     * @param  y divisor
     * @return   x % y, non-negative
     */
    public static double modulo(final double x, final double y) {
        return ((x % y) + y) % y;
    }
}

Related

  1. modDivide(long a, long b, long m)
  2. modifiedCalcElementOffset(long index, long mask)
  3. modifyValue(long value, char op, long modifier, long max, long min)
  4. mods(long v, long m)
  5. modularReciprocal(long a, long mod)
  6. modulo(long a, long n)