[PATCH] small bug fix for ConstantPoolGen and NaN

Tom Rodriguez <[email protected]>
Newsgroups gmane.comp.jakarta.bcel.devel
Message-ID <[email protected]>
ConstantPoolGen.lookupFloat and ConstantPoolGen.lookupDouble don't work 
correctly for NaN, since NaN != NaN.  Here's small fix for it which 
compares the canonical bit pattern of the double or float instead.

tom

Index: ConstantPoolGen.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-bcel/src/java/org/apache/bcel/generic/ConstantPoolGen.java,v
retrieving revision 1.3
diff -u -r1.3 ConstantPoolGen.java
--- ConstantPoolGen.java        11 Jul 2002 19:39:04 -0000      1.3
+++ ConstantPoolGen.java        7 Dec 2002 01:42:49 -0000
@@ -323,11 +323,12 @@
     * @return index on success, -1 otherwise
     */
    public int lookupFloat(float n) {
+    int bits = Float.floatToIntBits(n);
      for(int i=1; i < index; i++) {
        if(constants[i] instanceof ConstantFloat) {
         ConstantFloat c = (ConstantFloat)constants[i];

-       if(c.getBytes() == n)
+       if(Float.floatToIntBits(c.getBytes()) == bits)
           return i;
        }
      }
@@ -438,11 +439,12 @@
     * @return index on success, -1 otherwise
     */
    public int lookupDouble(double n) {
+    long bits = Double.doubleToLongBits(n);
      for(int i=1; i < index; i++) {
        if(constants[i] instanceof ConstantDouble) {
         ConstantDouble c = (ConstantDouble)constants[i];

-       if(c.getBytes() == n)
+       if(Double.doubleToLongBits(c.getBytes()) == bits)
           return i;
        }
      }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.