valarray.sum() gives unclean results

O

Oliver Block

Hi everybody,

using the following code leads to unclean result of sum() function:

/* va is a nxn array (valarray<int> (0,n*n) ) filled with integers 1 ..
5 */
int sum = 0;

for(int i = 0; i < n; i++) {
std::valarray<int> col = va[std::slice(i*n, n, n)];
cout << "sum: " << col.sum();
}

This leads to results like 14993885 even if the maximum should be 15

I have no clue what the problem might be.

I using a SuSE Linux Box with gcc 3.3 (as far as I remember the
version).

bye,

oliver
 
J

John Harrison

Oliver said:
Hi everybody,

using the following code leads to unclean result of sum() function:

/* va is a nxn array (valarray<int> (0,n*n) ) filled with integers 1 ..
5 */
int sum = 0;

for(int i = 0; i < n; i++) {
std::valarray<int> col = va[std::slice(i*n, n, n)];
cout << "sum: " << col.sum();
}

This leads to results like 14993885 even if the maximum should be 15

I have no clue what the problem might be.

I using a SuSE Linux Box with gcc 3.3 (as far as I remember the
version).

bye,

oliver

Because it accesses outside of the bounds of the valarray. Either you
meant this

std::valarray<int> col = va[std::slice(i*n, n, 1)];

or this

std::valarray<int> col = va[std::slice(i, n, 1)];

John
 
O

Oliver Block

n is the dimension of the nxn array.

i*n is 1*n, 2*n ... which is supposed to be the first element of each
column of my (logical) 2dim array:

for 3x3:

each column has 3 elements and the distance (stride) is also 3.

11 12 13
21 22 23
31 32 33

is saved in va like this

11 21 31 12 22 32 13 23 33

start:

0*3 = 0; 1*3 = 3; 2*3 = 6

You are right: stride should be 1.

Thanks,

Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top