← Back to team overview

kicad-developers team mailing list archive

Compile error on math_for_graphics.cpp

 

While checking the gencad patch I remembered a thing...

Maybe it's a compiler nuisance (gcc 4.6.1) but I get the following:

/home/mrclnz/cvswork/kicad-vnd/polygon/math_for_graphics.cpp: In function ‘double GetPointToLineDistance(double, double, int, int, double*, double*)’:
/home/mrclnz/cvswork/kicad-vnd/polygon/math_for_graphics.cpp:934:23: error: call of overloaded ‘abs(double)’ is ambiguous
/home/mrclnz/cvswork/kicad-vnd/polygon/math_for_graphics.cpp:934:23: note: candidates are:
/usr/include/stdlib.h:771:12: note: int abs(int)
/usr/stow/gcc-4.6.1/bin/../lib/gcc/i686-pc-linux-gnu/4.6.1/../../../../include/c++/4.6.1/cstdlib:173:3: note: long long int __gnu_cxx::abs(long long int)
/usr/stow/gcc-4.6.1/bin/../lib/gcc/i686-pc-linux-gnu/4.6.1/../../../../include/c++/4.6.1/cstdlib:139:3: note: long int std::abs(long int)

(and another couple similar messages on the same file)

IMHO it is correct since abs is for ints and these function take
doubles...

Trivially fixed like this:


=== modified file 'polygon/math_for_graphics.cpp'
--- polygon/math_for_graphics.cpp	2011-06-07 15:29:01 +0000
+++ polygon/math_for_graphics.cpp	2011-11-21 07:58:04 +0000
@@ -332,7 +332,7 @@
                 else
                 {
                     if( dist )
-                        *dist = min( abs(a-xi), abs(a-xf) );
+                        *dist = min( fabs(a-xi), fabs(a-xf) );
                     return 0;
                 }
             }
@@ -931,7 +931,7 @@
             *xpp = a;
             *ypp = y;
         }
-        return abs(a-x);
+        return fabs(a-x);
     }
     // find c,d such that (x,y) lies on y = c + dx where d=(-1/b)
     double d = -1.0/b;
@@ -1020,6 +1020,10 @@
 {
     double d;
     d = sqrt( (double)(x1-x2)*(x1-x2) + (double)(y1-y2)*(y1-y2) );
+    if( isnan( d ) ) // This would be a *massive* loss of precision, but can happen
+    {
+        return 0;
+    }
     if( d > INT_MAX || d < INT_MIN )
     {
         wxASSERT(0);

fabs is the useful alternative for abs using floating point,
obviously...

Actually I don't remember WHY I added the last part (it was something
like tracks very near which degenerated to e-39 numbers or something
like that)

-- 
Lorenzo Marcantonio
Logos Srl


Follow ups