Complete.Org: Mailing Lists: Archives: freeciv-dev: December 2004:
[Freeciv-Dev] (PR#11354) log(0.0) causes a server crash
Home

[Freeciv-Dev] (PR#11354) log(0.0) causes a server crash

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: ph.bayon@xxxxxxxxxxxxxxx
Subject: [Freeciv-Dev] (PR#11354) log(0.0) causes a server crash
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 9 Dec 2004 08:55:33 -0800
Reply-to: bugs@xxxxxxxxxxx

<URL: http://bugs.freeciv.org/Ticket/Display.html?id=11354 >

OK, that fix was quite wrong!  But this one I think is better.

When the value is 0, the log is assumed to be -FC_INFINITY.  Which means
that the max_dist becomes -FC_INFINITY/log(DIST_FACTOR) which is
approximately FC_INFINITY (because DIST_FACTOR is less than 1 its log is
negative).

I also changed the values to doubles to lessen the likelihood of this
problem.

-jason

Index: ai/aiexplorer.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aiexplorer.c,v
retrieving revision 1.5
diff -u -r1.5 aiexplorer.c
--- ai/aiexplorer.c     29 Sep 2004 02:24:18 -0000      1.5
+++ ai/aiexplorer.c     9 Dec 2004 16:54:58 -0000
@@ -249,7 +249,7 @@
   struct tile *ptile = punit->tile;
 
   /* The want of the most desirable tile, given nearby water, cities, etc. */
-  float most_desirable = 0;
+  double most_desirable = 0;
 
   /* The maximum distance we are willing to search. It decreases depending
    * on the want of already discovered tagets.  It is defined as the distance
@@ -274,7 +274,7 @@
 
   map = pf_create_map(&parameter);
   while (pf_next(map)) {
-    float desirable;
+    double desirable;
     struct pf_position pos;
 
     pf_next_get_position(map, &pos);
@@ -299,7 +299,13 @@
        *   log(most_desirable/BEST_POSSIBLE_SCORE) > dist * log(DIST_FACTOR)
        *   log(most_desirable/BEST_POSSIBLE_SCORE)/log(DIST_FACTOR) > dist
        */
-      max_dist = log(most_desirable / BEST_POSSIBLE_SCORE) / log(DIST_FACTOR);
+      if (most_desirable / BEST_POSSIBLE_SCORE == 0.0) {
+       /* If the desirability is 0 we'd better keep going. */
+       max_dist = -FC_INFINITY / log(DIST_FACTOR);
+      } else {
+       max_dist = log(most_desirable / BEST_POSSIBLE_SCORE)
+         / log(DIST_FACTOR);
+      }
     }
 
     if (pos.total_MC > max_dist) {

[Prev in Thread] Current Thread [Next in Thread]