[eigen] Overflow in sum()

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


Hi,

I added a custom Scalar type to Eigen, because my sparse matrices have
many coefficients but these are rather small integers. When using the
sum() method, I get wrong results, because the function returns a
Scalar. I added an assertion (see attached diff against current trunk)
to check for possible overflows.
I'm willing to fix this problem but I'm not sure how. Any
recommendations how to proceed?

Regards,
Jens
Index: Eigen/src/Sparse/SparseRedux.h
===================================================================
--- Eigen/src/Sparse/SparseRedux.h	(revision 947812)
+++ Eigen/src/Sparse/SparseRedux.h	(working copy)
@@ -32,8 +32,10 @@
   ei_assert(rows()>0 && cols()>0 && "you are using a non initialized matrix");
   Scalar res = 0;
   for (int j=0; j<outerSize(); ++j)
-    for (typename Derived::InnerIterator iter(derived(),j); iter; ++iter)
+    for (typename Derived::InnerIterator iter(derived(),j); iter; ++iter) {
+      ei_assert((Scalar) (res + iter.value()) > res); // assert that no overflow occurred
       res += iter.value();
+    }
   return res;
 }
 


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/