Skip to content

Commit

Permalink
Fix GenericMath.mod being wrong when div == 0
Browse files Browse the repository at this point in the history
  • Loading branch information
DDoS committed Jul 29, 2016
1 parent 67f24cd commit 39ef318
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 41 deletions.
45 changes: 20 additions & 25 deletions src/main/template/float/com/flowpowered/math/GenericMath.template
Original file line number Diff line number Diff line change
Expand Up @@ -632,37 +632,18 @@ public class GenericMath {
}

/**
* Returns the modulo of x by div with corrections for negative numbers.
* Returns the modulo of 'a' by 'div' with corrections for negative numbers.
*
* @param x The number as an int
* @param a The number as an int
* @param div The div as an int
* @return The corrected modulo
*/
public static int mod(int x, int div) {
return x < 0 ? (x % div) + div : x % div;
public static int mod(int a, int div) {
final int remainder = a % div;
return remainder < 0 ? remainder + div : remainder;
}

/**
* Returns the modulo of x by div with corrections for negative numbers.
*
* @param x The number as an float
* @param div The div as an float
* @return The corrected modulo
*/
public static float mod(float x, float div) {
return x < 0 ? (x % div) + div : x % div;
}

/**
* Returns the modulo of x by div with corrections for negative numbers.
*
* @param x The number as an double
* @param div The div as an double
* @return The corrected modulo
*/
public static double mod(double x, double div) {
return x < 0 ? (x % div) + div : x % div;
}
#REPLICATED9#

/**
* Gets a thread local Random object that is seeded using SecureRandom. Only one Random is created per thread.
Expand Down Expand Up @@ -779,6 +760,20 @@ public class GenericMath {
}
=====END_REPLICATED_CONTENT #8=====

====START_REPLICATED_CONTENT #9====
/**
* Returns the modulo of 'a' by 'div' with corrections for negative numbers.
*
* @param a The dividend
* @param div The divider
* @return The corrected modulo
*/
public static #e# mod(#e# a, #e# div) {
final #e# remainder = a % div;
return remainder < 0 ? remainder + div : remainder;
}
=====END_REPLICATED_CONTENT #9=====

====START_REPLICATED_CONTENT #4====
/**
* Attempts to normalize a vector. If this fails, the method catches the exception and return a zero vector of the same dimension instead.
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/com/flowpowered/math/test/GenericMathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import static com.flowpowered.math.GenericMath.floor;
import static com.flowpowered.math.GenericMath.mean;
import static com.flowpowered.math.GenericMath.mod;
import static com.flowpowered.math.GenericMath.roundUpPow2;

public final class GenericMathTest {
Expand Down Expand Up @@ -63,4 +64,43 @@ public void testFloor() {
assertEquals(0, floor(0.9));
assertEquals(0, floor(0.9f));
}

@Test
public void testModInt() {
assertEquals(0, mod(-32, 16));
assertEquals(15, mod(-17, 16));
assertEquals(0, mod(-16, 16));
assertEquals(2, mod(-14, 16));
assertEquals(0, mod(0, 16));
assertEquals(6, mod(6, 16));
assertEquals(0, mod(16, 16));
assertEquals(8, mod(24, 16));
assertEquals(0, mod(32, 16));
}

@Test
public void testModFloat() {
assertEquals(0f, mod(-32f, 16f), 0f);
assertEquals(15f, mod(-17f, 16f), 0f);
assertEquals(0f, mod(-16f, 16f), 0f);
assertEquals(2f, mod(-14f, 16f), 0f);
assertEquals(0f, mod(0f, 16f), 0f);
assertEquals(6f, mod(6f, 16f), 0f);
assertEquals(0f, mod(16f, 16f), 0f);
assertEquals(8f, mod(24f, 16f), 0f);
assertEquals(0f, mod(32f, 16f), 0f);
}

@Test
public void testModDouble() {
assertEquals(0d, mod(-32d, 16d), 0d);
assertEquals(15d, mod(-17d, 16d), 0d);
assertEquals(0d, mod(-16d, 16d), 0d);
assertEquals(2d, mod(-14d, 16d), 0d);
assertEquals(0d, mod(0d, 16d), 0d);
assertEquals(6d, mod(6d, 16d), 0d);
assertEquals(0d, mod(16d, 16d), 0d);
assertEquals(8d, mod(24d, 16d), 0d);
assertEquals(0d, mod(32d, 16d), 0d);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -750,36 +750,39 @@ public static String decToHex(int dec, int minDigits) {
}

/**
* Returns the modulo of x by div with corrections for negative numbers.
* Returns the modulo of 'a' by 'div' with corrections for negative numbers.
*
* @param x The number as an int
* @param a The number as an int
* @param div The div as an int
* @return The corrected modulo
*/
public static int mod(int x, int div) {
return x < 0 ? (x % div) + div : x % div;
public static int mod(int a, int div) {
final int remainder = a % div;
return remainder < 0 ? remainder + div : remainder;
}

/**
* Returns the modulo of x by div with corrections for negative numbers.
* Returns the modulo of 'a' by 'div' with corrections for negative numbers.
*
* @param x The number as an float
* @param div The div as an float
* @param a The dividend
* @param div The divider
* @return The corrected modulo
*/
public static float mod(float x, float div) {
return x < 0 ? (x % div) + div : x % div;
public static float mod(float a, float div) {
final float remainder = a % div;
return remainder < 0 ? remainder + div : remainder;
}

/**
* Returns the modulo of x by div with corrections for negative numbers.
* Returns the modulo of 'a' by 'div' with corrections for negative numbers.
*
* @param x The number as an double
* @param div The div as an double
* @param a The dividend
* @param div The divider
* @return The corrected modulo
*/
public static double mod(double x, double div) {
return x < 0 ? (x % div) + div : x % div;
public static double mod(double a, double div) {
final double remainder = a % div;
return remainder < 0 ? remainder + div : remainder;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ public static Complexd fromReal(double x) {
}

/**
* Creates a new quaternion from the double imaginary components.
* Creates a new complex from the double imaginary components.
*
* <p>The {@link #ZERO} constant is re-used when {@code y} is 0.</p>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ public static Complexf fromReal(float x) {
}

/**
* Creates a new quaternion from the float imaginary components.
* Creates a new complex from the float imaginary components.
*
* <p>The {@link #ZERO} constant is re-used when {@code y} is 0.</p>
*
Expand Down

0 comments on commit 39ef318

Please sign in to comment.