Author: nextgens
Date: 2006-07-20 23:07:59 +0000 (Thu, 20 Jul 2006)
New Revision: 9686

Modified:
   trunk/freenet/src/freenet/node/Location.java
   trunk/freenet/src/freenet/node/LocationManager.java
   trunk/freenet/src/freenet/node/NodeDispatcher.java
Log:
Floating point types are inherently imprecise. Using the operators == or != 
might not yield the expected result ...
With that patch we will loose in terms of precision ... but we will know how 
much :p

Modified: trunk/freenet/src/freenet/node/Location.java
===================================================================
--- trunk/freenet/src/freenet/node/Location.java        2006-07-20 22:41:59 UTC 
(rev 9685)
+++ trunk/freenet/src/freenet/node/Location.java        2006-07-20 23:07:59 UTC 
(rev 9686)
@@ -45,7 +45,7 @@

     public boolean equals(Object o) {
         if(o instanceof Location) {
-            return ((Location)o).loc == loc;
+            return Math.abs(((Location)o).loc - loc) <= Double.MIN_VALUE;
         }
         return false;
     }

Modified: trunk/freenet/src/freenet/node/LocationManager.java
===================================================================
--- trunk/freenet/src/freenet/node/LocationManager.java 2006-07-20 22:41:59 UTC 
(rev 9685)
+++ trunk/freenet/src/freenet/node/LocationManager.java 2006-07-20 23:07:59 UTC 
(rev 9686)
@@ -126,7 +126,7 @@
                                     PeerNode pn = peers[i];
                                     if(pn.isRoutable()) {
                                         double ploc = 
pn.getLocation().getValue();
-                                        if(ploc == myLoc) {
+                                        if(Math.abs(ploc - myLoc) <= 
Double.MIN_VALUE) {
                                             myFlag = true;
                                             // Log an ERROR
                                             // As this is an ERROR, it results 
from either a bug or malicious action.
@@ -595,22 +595,22 @@

         double A = 1.0;
         for(int i=0;i<friendLocs.length;i++) {
-            if(friendLocs[i] == myLoc) continue;
+            if(Math.abs(friendLocs[i] - myLoc) <= Double.MIN_VALUE) continue;
             A *= PeerManager.distance(friendLocs[i], myLoc);
         }
         for(int i=0;i<hisFriendLocs.length;i++) {
-            if(hisFriendLocs[i] == hisLoc) continue;
+            if(Math.abs(hisFriendLocs[i] - hisLoc) <= Double.MIN_VALUE) 
continue;
             A *= PeerManager.distance(hisFriendLocs[i], hisLoc);
         }

         // B = the same, with our two values swapped
         double B = 1.0;
         for(int i=0;i<friendLocs.length;i++) {
-            if(friendLocs[i] == hisLoc) continue;
+            if(Math.abs(friendLocs[i] - hisLoc) <= Double.MIN_VALUE) continue;
             B *= PeerManager.distance(friendLocs[i], hisLoc);
         }
         for(int i=0;i<hisFriendLocs.length;i++) {
-            if(hisFriendLocs[i] == myLoc) continue;
+            if(Math.abs(hisFriendLocs[i] - myLoc) <= Double.MIN_VALUE) 
continue;
             B *= PeerManager.distance(hisFriendLocs[i], myLoc);
         }


Modified: trunk/freenet/src/freenet/node/NodeDispatcher.java
===================================================================
--- trunk/freenet/src/freenet/node/NodeDispatcher.java  2006-07-20 22:41:59 UTC 
(rev 9685)
+++ trunk/freenet/src/freenet/node/NodeDispatcher.java  2006-07-20 23:07:59 UTC 
(rev 9686)
@@ -283,7 +283,7 @@
         // pn == null => originated locally, keep full htl
         double target = m.getDouble(DMT.TARGET_LOCATION);
         Logger.minor(this, "id "+id+" from "+pn+" htl "+htl+" target "+target);
-        if(node.lm.getLocation().getValue() == target) {
+        if(Math.abs(node.lm.getLocation().getValue() - target) <= 
Double.MIN_VALUE) {
             Logger.minor(this, "Dispatching "+m.getSpec()+" on 
"+node.portNumber);
             // Handle locally
             // Message type specific processing


Reply via email to