Boost logo

Ublas :

From: Paul C. Leopardi (paul.leopardi_at_[hidden])
Date: 2007-09-16 23:32:28


Hello Hongyu Miao,
I reply below.
Best, Paul
On Mon, 17 Sep 2007, Hongyu Miao wrote:
> Problem Description:
> The following codes will generate some strange results:
>
> boost::numeric::ublas::matrix<double> A4(4,6);
> for (i=0; i<A4.size1(); i++)
> {
> for (j=0; j<A4.size2(); j++)
> {
> A4(i,j) = i*A4.size2()-j;
> }
> }
> std::cout << A4 << std::endl;
>
> The results shoulde be
> [4,6]((0,-1,-2,-3,-4,-5),(6,5,4,3,2,1),(12,11,10,9,8,7),(18,17,16,15,14,13
>))
>
> However, I actually got:
>
>
> [4,6]((0,4.29497e+009,4.29497e+009,4.29497e+009,4.29497e+009,4.29497e+009),
>(6,5,4,3,2,1),(12,11,10,9,8,7),(18,17,16,15,14,13))
>
> If I change the code as follows, the problem is gone:
> ...
> int nTemp = A4.size2();
> A4(i,j) = i*nTemp-j;
> ...
>
> So the problem must be caused by
>
> A4(i,j) = i*A4.size2()-j;
>
>
> Any idea about this bug? Thanks.

You did not include the declarations for i and j. Did you declare them to be
size_type, int or unsigned int? I suspect that on your system size_type is
unsigned long. The right hand side of the assignment above is therefore an
expression involving three unsigned variables. According to "the usual
arihmetic conversions" (eg. 5.9 of
http://www.kuzbass.ru:8086/docs/isocpp/expr.html ) the result of the
subtraction is unsigned and is therefore not negative. You expect a double
result. You should therefore cast the left hand side of the "-" to a double
or the right hand side or both, eg.

A4(i,j) = double(i*A4.size2())-double(j);