Java Float Number Mod mod(float a, float b)

Here you can find the source of mod(float a, float b)

Description

Calculates x modulo y.

License

Open Source License

Parameter

Parameter Description
a The operand.
b The modulus.

Return

x modulo y

Declaration

public static float mod(float a, float b) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//  ww  w  . ja v  a 2s. com
     * Calculates x modulo y. This is different than the Java remainder operator
     * (%) in that it always returns a positive value.
     *
     * @param a The operand.
     * @param b The modulus.
     * @return x modulo y
     */
    public static float mod(float a, float b) {
        if (a < 0) {
            return a + (float) Math.ceil(-a / b) * b;
        } else if (a >= b) {
            return a - (float) Math.floor(a / b) * b;
        } else {
            return a;
        }
    }

    /**
     * Calculates x modulo y. This is different than the Java remainder operator
     * (%) in that it always returns a positive value.
     *
     * @param a The operand.
     * @param b The modulus.
     * @return x modulo y
     */
    public static double mod(double a, double b) {
        if (a < 0) {
            return a + Math.ceil(-a / b) * b;
        } else if (a >= b) {
            return a - Math.floor(a / b) * b;
        } else {
            return a;
        }
    }

    /**
     * Calculates x modulo y. This is different than the Java remainder operator
     * (%) in that it always returns a positive value.
     *
     * @param a The operand.
     * @param b The modulus.
     * @return x modulo y
     */
    public static int mod(int a, int b) {
        if (a < 0) {
            return (a % b + b) % b;
        } else {
            return a % b;
        }
    }
}

Related

  1. fmodulo(double v1, double v2)
  2. mod(final float a, final float b)
  3. mod(final float a, final float mod)
  4. mod(float a, float b)
  5. mod(float a, float b)
  6. mod(float dividend, float quotient)
  7. mod2(float value)
  8. module(float[] vector)
  9. modulusSq(float[][] a)