Boost logo

Boost Users :

From: Ian McCulloch (ianmcc_at_[hidden])
Date: 2006-08-18 12:21:35


Minkoo Seo wrote:

> Hi list.
>
> I got a problem of matrix initialization. Please
> see the following code:
>
> #include <iostream>
> #include <iterator>
> #include <boost/numeric/ublas/matrix.hpp>
> #include <boost/numeric/ublas/io.hpp>
>
> using namespace std;
> using namespace boost::numeric::ublas;
>
> int main ()
> {
> matrix<double> m(3, 3);
> for (unsigned i = 0; i < 3; i++)
> m(i, i) = -i;
>
> cout << m << endl;
>
> return EXIT_SUCCESS;
> }
>
> In this code, I'm trying to initialize the diagonal with
> -1, -2, -3. But, the result is obviously different from
> what I expected:
>
> [3,3]((0,0,0),(0,4.29497e+09,0),(0,0,4.29497e+09))
>
> This must be due to type conversion from unsigned from double
> that occurs at 'm(i, i) = -i'. IMHO, the code must works as follows
>
> i // unsigned
> -i // (-1) * i = int * unsigned = int
> m(i, i) = -i // lhs is double, so int on rhs should be converted
> // into double.
>
> But the result implies that I'm wrong. Anybody knows what I'm
> missing here?

You are missing that C++ promotion rules are very weird.
int * unsigned = unsigned.

>
> How would you correct the above code?

Don't use unsigned, and especially, don't mix them with signed types.

Cheers,
Ian


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net