473,395 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

valarray.sum() gives unclean results

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

Nov 22 '05 #1
3 1269
Oliver Block wrote:
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
Nov 22 '05 #2
>
std::valarray<int> col = va[std::slice(i, n, 1)];


Typo

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

john
Nov 22 '05 #3
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

Nov 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Christian Brechbühler | last post by:
The template std::valarray behaves pretty much like a mathematical vector. Arithmetic operators apply elementwise. Now I'd like to extend this to a user-defined type, e.g., complex. ...
2
by: Jim West | last post by:
The GNU implementation of the valarray header (g++ version 3.3.2) includes the comment: // This complication is so to make valarray<valarray<T> > work // even though it is not required by the...
1
by: Steven T. Hatton | last post by:
Examine the following code before you compile and run it. What do you expect to happen? Is what you expected consistent with the result of running it? #include <valarray> #include <iostream> ...
2
by: Michael Hopkins | last post by:
Hi all I have a subclass of valarray<T> thus template <typename T> class uo_val : public std::valarray<T> { public: uo_val ( ) : std::valarray<T>() {} uo_val (const int sz ) :...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.