[Avida-SVN] r2053 - development/source/tools

brysonda at myxo.css.msu.edu brysonda at myxo.css.msu.edu
Thu Sep 6 08:01:24 PDT 2007


Author: brysonda
Date: 2007-09-06 11:01:24 -0400 (Thu, 06 Sep 2007)
New Revision: 2053

Modified:
   development/source/tools/cMerit.cc
   development/source/tools/cMerit.h
Log:
Fix to cMerit::UpdateValue so that it does not crash under certain circumstances with low exponent values.   Credit goes to Gregory Propf (via SourceForge) for discovering this issue.

Modified: development/source/tools/cMerit.cc
===================================================================
--- development/source/tools/cMerit.cc	2007-09-05 20:27:44 UTC (rev 2052)
+++ development/source/tools/cMerit.cc	2007-09-06 15:01:24 UTC (rev 2053)
@@ -16,21 +16,19 @@
 
 void cMerit::UpdateValue(double in_value)
 {
-  const int max_bits = sizeof(unsigned int)*8;
-  static double mult[max_bits];
-  static bool mult_initilalized = false;
+  static const int max_bits = sizeof(unsigned int) * 8;
+  struct sExponentMultiplier
+  {
+    double mult[max_bits + 1];
+    sExponentMultiplier() { for (int i = 0; i <= max_bits; i++) mult[i] = pow(2.0, i - 1); }
+  };
+  static sExponentMultiplier exp;
 
+  
   // Do not allow negative merits. If less than 1, set to 0.
   if (in_value < 1.0) in_value = 0.0;
 
-  // Initilize multipliers only once
-  if (mult_initilalized == false) {
-    mult_initilalized = true;
-    for (int i = 0; i < max_bits; ++i) mult[i] = pow(2.0, i);
-  }
-
   value = in_value;
-
   double mant = frexp(value, &bits);
 
   if (bits > max_bits)
@@ -38,7 +36,7 @@
   else
     offset = 0;
 
-  base = static_cast<unsigned int>(mant * mult[bits-offset-1] * 2);
+  base = static_cast<unsigned int>(mant * exp.mult[bits - offset] * 2.0);
 }
 
 

Modified: development/source/tools/cMerit.h
===================================================================
--- development/source/tools/cMerit.h	2007-09-05 20:27:44 UTC (rev 2052)
+++ development/source/tools/cMerit.h	2007-09-06 15:01:24 UTC (rev 2053)
@@ -98,7 +98,7 @@
 
   static double EnergyToMerit(const double orgEnergy, cWorld* m_world);
 
-  std::ostream& BinaryPrint(std::ostream& os = std::cout) const ;
+  std::ostream& BinaryPrint(std::ostream& os = std::cout) const;
 };
 
 




More information about the Avida-cvs mailing list