diff options
author | Sven Gothel <[email protected]> | 2014-03-15 16:54:34 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2014-03-15 16:54:34 +0100 |
commit | 101567f5f16d91a13c8067764d5e14eefb2b9936 (patch) | |
tree | dd53040810d4728182962a6a6dc6ae96427741cf /src/jogl/classes/com/jogamp/opengl/math/FloatUtil.java | |
parent | 06fbb390d28bc247945931699e1d59bdd76230c6 (diff) |
FloatUtil/VectorUtil: Enhance isEqual/compare w/ and w/o epsilon, add unit tests - Cleanup VectorUtil (vec2/3 naming, remove dedundant functions)
Diffstat (limited to 'src/jogl/classes/com/jogamp/opengl/math/FloatUtil.java')
-rw-r--r-- | src/jogl/classes/com/jogamp/opengl/math/FloatUtil.java | 88 |
1 files changed, 86 insertions, 2 deletions
diff --git a/src/jogl/classes/com/jogamp/opengl/math/FloatUtil.java b/src/jogl/classes/com/jogamp/opengl/math/FloatUtil.java index ee57fc7e8..bb40fed33 100644 --- a/src/jogl/classes/com/jogamp/opengl/math/FloatUtil.java +++ b/src/jogl/classes/com/jogamp/opengl/math/FloatUtil.java @@ -122,7 +122,7 @@ public class FloatUtil { final float s = sin(angrad); tmpVec3f[0]=x; tmpVec3f[1]=y; tmpVec3f[2]=z; - VectorUtil.normalize(tmpVec3f); + VectorUtil.normalizeVec3(tmpVec3f); x = tmpVec3f[0]; y = tmpVec3f[1]; z = tmpVec3f[2]; // Rotation matrix (Row Order): @@ -713,11 +713,95 @@ public class FloatUtil { public static final float EPSILON = 1.1920929E-7f; // Float.MIN_VALUE == 1.4e-45f ; double EPSILON 2.220446049250313E-16d /** + * Return true if both values are equal w/o regarding an epsilon. + * <p> + * Implementation considers following corner cases: + * <ul> + * <li>NaN == NaN</li> + * <li>+Inf == +Inf</li> + * <li>-Inf == -Inf</li> + * </ul> + * </p> + * @see #isEqual(float, float, float) + */ + public static boolean isEqual(final float a, final float b) { + // Values are equal (Inf, Nan .. ) + return Float.floatToIntBits(a) == Float.floatToIntBits(b); + } + + /** * Return true if both values are equal, i.e. their absolute delta < <code>epsilon</code>. + * <p> + * Implementation considers following corner cases: + * <ul> + * <li>NaN == NaN</li> + * <li>+Inf == +Inf</li> + * <li>-Inf == -Inf</li> + * </ul> + * </p> * @see #EPSILON */ public static boolean isEqual(final float a, final float b, final float epsilon) { - return Math.abs(a - b) < epsilon; + if ( Math.abs(a - b) < epsilon ) { + return true; + } else { + // Values are equal (Inf, Nan .. ) + return Float.floatToIntBits(a) == Float.floatToIntBits(b); + } + } + + /** + * Return true if both values are equal w/o regarding an epsilon. + * <p> + * Implementation considers following corner cases: + * <ul> + * <li>NaN == NaN</li> + * <li>+Inf == +Inf</li> + * <li>-Inf == -Inf</li> + * <li>NaN > 0</li> + * <li>+Inf > -Inf</li> + * </ul> + * </p> + * @see #compare(float, float, float) + */ + public static int compare(final float a, final float b) { + if (a < b) { + return -1; // Neither is NaN, a is smaller + } + if (a > b) { + return 1; // Neither is NaN, a is larger + } + final int aBits = Float.floatToIntBits(a); + final int bBits = Float.floatToIntBits(b); + if( aBits == bBits ) { + return 0; // Values are equal (Inf, Nan .. ) + } else if( aBits < bBits ) { + return -1; // (-0.0, 0.0) or (!NaN, NaN) + } else { + return 1; // ( 0.0, -0.0) or ( NaN, !NaN) + } + } + + /** + * Return true if both values are equal, i.e. their absolute delta < <code>epsilon</code>. + * <p> + * Implementation considers following corner cases: + * <ul> + * <li>NaN == NaN</li> + * <li>+Inf == +Inf</li> + * <li>-Inf == -Inf</li> + * <li>NaN > 0</li> + * <li>+Inf > -Inf</li> + * </ul> + * </p> + * @see #EPSILON + */ + public static int compare(final float a, final float b, final float epsilon) { + if ( Math.abs(a - b) < epsilon ) { + return 0; + } else { + return compare(a, b); + } } /** |