Java floor floorModulo(int value, int divisor)

Here you can find the source of floorModulo(int value, int divisor)

Description

Calculates the remainder based on #floorDivide(int,int) .

License

Open Source License

Parameter

Parameter Description
value numerator
divisor divisor

Return

remainder of division (never negative if divisor is positive)

Declaration


public static int floorModulo(int value, int divisor) 

Method Source Code

//package com.java2s;
/*//from ww  w .  j a va2 s .c  o m
 * -----------------------------------------------------------------------
 * Copyright ? 2013-2014 Meno Hochschild, <http://www.menodata.de/>
 * -----------------------------------------------------------------------
 * This file (MathUtils.java) is part of project Time4J.
 *
 * Time4J is free software: You can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * Time4J 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Time4J. If not, see <http://www.gnu.org/licenses/>.
 * -----------------------------------------------------------------------
 */

public class Main {
    /**
     * <p>Calculates the remainder based on {@link #floorDivide(int, int)}. </p>
     *
     * <p>Examples: </p>
     *
     * <ul>
     *  <li>{@code floorModulo(2, 2) == 0}</li>
     *  <li>{@code floorModulo(1, 2) == 1}</li>
     *  <li>{@code floorModulo(0, 2) == 0}</li>
     *  <li>{@code floorModulo(-1, 2) == 1}</li>
     *  <li>{@code floorModulo(-2, 2) == 0}</li>
     *  <li>{@code floorModulo(-3, 2) == 1}</li>
     * </ul>
     *
     * @param   value       numerator
     * @param   divisor     divisor
     * @return  remainder of division (never negative if divisor is positive)
     */
    /*[deutsch]
     * <p>Modulo-Operator, der den Divisionsrest auf Basis von
     * {@link #floorDivide(int, int)} berechnet. </p>
     *
     * <p>Beispiele: </p>
     *
     * <ul>
     *  <li>{@code floorModulo(2, 2) == 0}</li>
     *  <li>{@code floorModulo(1, 2) == 1}</li>
     *  <li>{@code floorModulo(0, 2) == 0}</li>
     *  <li>{@code floorModulo(-1, 2) == 1}</li>
     *  <li>{@code floorModulo(-2, 2) == 0}</li>
     *  <li>{@code floorModulo(-3, 2) == 1}</li>
     * </ul>
     *
     * @param   value       numerator
     * @param   divisor     divisor
     * @return  remainder of division (never negative if divisor is positive)
     */
    public static int floorModulo(int value, int divisor) {

        return (value - divisor * (floorDivide(value, divisor)));

    }

    /**
     * <p>See {@link #floorModulo(int, int)}. </p>
     *
     * @param   value       numerator
     * @param   divisor     divisor
     * @return  remainder of division (never negative if divisor is positive)
     */
    /*[deutsch]
     * <p>Siehe {@link #floorModulo(int, int)}. </p>
     *
     * @param   value       numerator
     * @param   divisor     divisor
     * @return  remainder of division (never negative if divisor is positive)
     */
    public static int floorModulo(long value, int divisor) {

        long ret = (value - divisor * (floorDivide(value, divisor)));
        return (int) ret; // Type-Cast hier wegen modulo-Semantik sicher

    }

    /**
     * <p>Returns the largest lower limit of quotient. </p>
     *
     * <p>Examples: </p>
     *
     * <ul>
     *  <li>{@code floorDivide(2, 2) == 1}</li>
     *  <li>{@code floorDivide(1, 2) == 0}</li>
     *  <li>{@code floorDivide(0, 2) == 0}</li>
     *  <li>{@code floorDivide(-1, 2) == -1}</li>
     *  <li>{@code floorDivide(-2, 2) == -1}</li>
     *  <li>{@code floorDivide(-3, 2) == -2}</li>
     * </ul>
     *
     * @param   value       numerator
     * @param   divisor     divisor
     * @return  quotient as result of division
     */
    /*[deutsch]
     * <p>Liefert die gr&ouml;&szlig;te untere Schranke des Quotienten. </p>
     *
     * <p>Beispiele: </p>
     *
     * <ul>
     *  <li>{@code floorDivide(2, 2) == 1}</li>
     *  <li>{@code floorDivide(1, 2) == 0}</li>
     *  <li>{@code floorDivide(0, 2) == 0}</li>
     *  <li>{@code floorDivide(-1, 2) == -1}</li>
     *  <li>{@code floorDivide(-2, 2) == -1}</li>
     *  <li>{@code floorDivide(-3, 2) == -2}</li>
     * </ul>
     *
     * @param   value       numerator
     * @param   divisor     divisor
     * @return  quotient as result of division
     */
    public static int floorDivide(int value, int divisor) {

        if (value >= 0) {
            return (value / divisor);
        } else {
            return ((value + 1) / divisor) - 1;
        }

    }

    /**
     * <p>See {@link #floorDivide(int, int)}. </p>
     *
     * @param   value       numerator
     * @param   divisor     divisor
     * @return  quotient as result of division
     */
    /*[deutsch]
     * <p>Siehe {@link #floorDivide(int, int)}. </p>
     *
     * @param   value       numerator
     * @param   divisor     divisor
     * @return  quotient as result of division
     */
    public static long floorDivide(long value, int divisor) {

        if (value >= 0) {
            return (value / divisor);
        } else {
            return ((value + 1) / divisor) - 1;
        }

    }
}

Related

  1. floorLong(final double x)
  2. floormod(float a, float b)
  3. floorMod(long x, long y)
  4. floorMod(long x, long y)
  5. floorModExt(int dividend, int divisor)
  6. floorPlaneAndHorizToPlanView(final int[][] frameCells, final short frame[], final int h)
  7. floorPlaneToPlanView(int[][] frameCells, final int h)
  8. floorPositive(float value)
  9. floorPot(float value)