Skip to content

Commit

Permalink
Add Math.clamp(float|double|int) and test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
pollend authored and httpdigest committed May 5, 2020
1 parent 53c0245 commit 14033e6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/org/joml/Math.java
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,16 @@ public static double max(double a, double b) {
return a > b ? a : b;
}

public static float clamp(float a, float b, float val){
return max(a,min(b,val));
}
public static double clamp(double a, double b, double val) {
return max(a,min(b,val));
}
public static int clamp(int a, int b, int val) {
return max(a, min(b, val));
}

public static float toRadians(float angles) {
return (float) java.lang.Math.toRadians(angles);
}
Expand Down
17 changes: 17 additions & 0 deletions test/org/joml/test/MathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@
*/
public class MathTest extends TestCase {

public static void testClamp(){
// Integer value tests
assertEquals(Math.clamp(10,20,0),10);
assertEquals(Math.clamp(10,20,12),12);
assertEquals(Math.clamp(10,20,30),20);

// Double value tests
assertEquals(Math.clamp(10f,20f,0f),10f,.0001f);
assertEquals(Math.clamp(10f,20f,12f),12f,.0001f);
assertEquals(Math.clamp(10f,20f,30f),20f,.0001f);

// Float value tests
assertEquals(Math.clamp(10.0,20.0,0.0),10.0,.0001);
assertEquals(Math.clamp(10.0,20.0,12.0),12.0,.0001);
assertEquals(Math.clamp(10.0,20.0,30.0),20.0,.0001);
}

public static void testDoubleVecLength() {
// Integer value tests
assertEquals(5., Vector2d.length(4, 3), .0001);
Expand Down

0 comments on commit 14033e6

Please sign in to comment.