FastMath extension: mod(x, y)

When porting some code from GLSL to java I ran into a method that does not exist in FastMath (Or java.util.Math…) : mod(x, y). It simply caclulates the modulus for two float variables similar to ‘x % y’ for integers.

GLSL also contains similar methods for vec2, vec3 and vec4.

It might be convenient to have these methods in FastMath (Or the respective VectorXf classes).

The reason I’m porting some (unfamiliar) GLSL code to java is because I have some issues with it and need to debug it. Java provides much better facilities for this and allows me to output intermediate results as well. The closer the Java code is to GLSL the easier it would be to do this.

If anyone is interested, I’d be happy to port all missing GLSL methods to FastMath. (Or a new GlslSim class). Some of these methods are already implemented in the respective vector classes. We’d might want to have those reference FastMath as well then.



  • Modulus, calculates the remainder of the division x/y, similar to x%y for
  • integers.

  • @param x
  • @param y
  • @return The remainder of x/y.


    public static float mod(float x, float y) {

    return x - y * floor(x / y);



You do know that in java % works on floats?

8| You’ve got to be kidding me! 10 years of java experience and I’ve always believed it only worked on integers… Hmm, it suddenly makes sense why java.util.Math didn’t have it either.

I’ll go hide now… :wink:

1 Like