summaryrefslogtreecommitdiffstats
path: root/src/javax/vecmath/VecMathUtil.java
diff options
context:
space:
mode:
authorKevin Rushforth <[email protected]>2004-10-05 17:57:17 +0000
committerKevin Rushforth <[email protected]>2004-10-05 17:57:17 +0000
commitc2c794b342ee48fbaa91b6a97123884a09807e80 (patch)
tree5eb9713f863876cea9ce49168c4ea0ad97b844dc /src/javax/vecmath/VecMathUtil.java
parent42c4b785100ca085a8e39d86a45cda07a8d898f1 (diff)
Issue number: 36
Submitted by: kcr Fixed Issue 36 -- Vector3d hashCode() method distinguishes between -0.0 and 0.0 git-svn-id: https://svn.java.net/svn/vecmath~svn/trunk@20 dd45e54d-f42e-c781-df72-dca083a658b1
Diffstat (limited to 'src/javax/vecmath/VecMathUtil.java')
-rw-r--r--src/javax/vecmath/VecMathUtil.java84
1 files changed, 84 insertions, 0 deletions
diff --git a/src/javax/vecmath/VecMathUtil.java b/src/javax/vecmath/VecMathUtil.java
new file mode 100644
index 0000000..cc34d90
--- /dev/null
+++ b/src/javax/vecmath/VecMathUtil.java
@@ -0,0 +1,84 @@
+/*
+ * $RCSfile$
+ *
+ * Copyright (c) 2004 Sun Microsystems, Inc. All rights reserved.
+ *
+ * Use is subject to license terms.
+ *
+ * $Revision$
+ * $Date$
+ * $State$
+ */
+
+package javax.vecmath;
+
+/**
+ * Utility vecmath class used when computing the hash code for vecmath
+ * objects containing float or double values. This fixes Issue 36.
+ */
+class VecMathUtil {
+ /**
+ * Returns the representation of the specified floating-point
+ * value according to the IEEE 754 floating-point "single format"
+ * bit layout, after first mapping -0.0 to 0.0. This method is
+ * identical to Float.floatToIntBits(float) except that an integer
+ * value of 0 is returned for a floating-point value of
+ * -0.0f. This is done for the purpose of computing a hash code
+ * that satisfies the contract of hashCode() and equals(). The
+ * equals() method in each vecmath class does a pair-wise "=="
+ * test on each floating-point field in the class (e.g., x, y, and
+ * z for a Tuple3f). Since 0.0f&nbsp;==&nbsp;-0.0f returns true,
+ * we must also return the same hash code for two objects, one of
+ * which has a field with a value of -0.0f and the other of which
+ * has a cooresponding field with a value of 0.0f.
+ *
+ * @param f an input floating-point number
+ * @return the integer bits representing that floating-point
+ * number, after first mapping -0.0f to 0.0f
+ */
+ static int floatToIntBits(float f) {
+ // Check for +0 or -0
+ if (f == 0.0f) {
+ return 0;
+ }
+ else {
+ return Float.floatToIntBits(f);
+ }
+ }
+
+ /**
+ * Returns the representation of the specified floating-point
+ * value according to the IEEE 754 floating-point "double format"
+ * bit layout, after first mapping -0.0 to 0.0. This method is
+ * identical to Double.doubleToLongBits(double) except that an
+ * integer value of 0L is returned for a floating-point value of
+ * -0.0. This is done for the purpose of computing a hash code
+ * that satisfies the contract of hashCode() and equals(). The
+ * equals() method in each vecmath class does a pair-wise "=="
+ * test on each floating-point field in the class (e.g., x, y, and
+ * z for a Tuple3d). Since 0.0&nbsp;==&nbsp;-0.0 returns true, we
+ * must also return the same hash code for two objects, one of
+ * which has a field with a value of -0.0 and the other of which
+ * has a cooresponding field with a value of 0.0.
+ *
+ * @param d an input double precision floating-point number
+ * @return the integer bits representing that floating-point
+ * number, after first mapping -0.0f to 0.0f
+ */
+ static long doubleToLongBits(double d) {
+ // Check for +0 or -0
+ if (d == 0.0) {
+ return 0L;
+ }
+ else {
+ return Double.doubleToLongBits(d);
+ }
+ }
+
+
+ /**
+ * Do not construct an instance of this class.
+ */
+ private VecMathUtil() {
+ }
+}