Boost logo

Ublas :

From: Hongyu Miao (jackymiao_at_[hidden])
Date: 2007-09-17 09:30:08

Thanks, Paul. I put a "double" before i*A4.size2() and it did work. However, I declared i and j to be
int, not size_type or unsigned int; so the arihmetic conversions should not treat the result as unsigned number, am I right? Thanks again.

BTW: I'm using BOOST v1.34.1 and VS 2005 on Windows.

Hongyu Miao

From:Paul C. Leopardi
Date:2007-09-16 22:32:31
To:ublas mailing list
Subject:Re: [ublas] Bug Report: ublas::matrix

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),
> 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 ) 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);
ublas mailing list