chart_rounded_tick: Avoid arithmetic problems with inputs close to zero
authorJohn Darrington <john@darrington.wattle.id.au>
Fri, 23 Mar 2012 10:15:11 +0000 (11:15 +0100)
committerJohn Darrington <john@darrington.wattle.id.au>
Fri, 23 Mar 2012 10:15:11 +0000 (11:15 +0100)
src/math/chart-geometry.c

index 736f89934b986648cc6ca1c423473a520d7676a5..573459892e109781e4eaa079ed0d34d5549cbfc7 100644 (file)
@@ -25,7 +25,6 @@
 double
 chart_rounded_tick(double tick)
 {
-
   int i;
 
   double diff = DBL_MAX;
@@ -33,7 +32,13 @@ chart_rounded_tick(double tick)
 
   static const double standard_ticks[] = {1, 2, 5, 10};
 
-  const double factor = pow(10,ceil(log10(standard_ticks[0] / tick))) ;
+  double factor;
+
+  /* Avoid arithmetic problems with very small values */
+  if (abs (tick) < DBL_EPSILON)
+     return 0;
+
+  factor = pow(10,ceil(log10(standard_ticks[0] / tick))) ;
 
   for (i = 3  ; i >= 0 ; --i)
     {