Here you can find the source of mod(float a, float b)
Parameter | Description |
---|---|
a | The operand. |
b | The modulus. |
public static float mod(float a, float b)
//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; } } }