我有一个包含整数值的向量,我希望总结。 然而,它总结了错误 - 如果向量在-10和10之间保持10个整数,我的总数不应该是18446744073709551602 。
我声明矢量,并用值填充它:
std::vector<int> X; for ( int i = 0; i < readings.size() ; ++i ) { X.push_back(readings[i].x);然后我总结并展示:
unsigned long temptotal = 0; for ( int i = 0; i < readings.size() ; ++i ) { std::cout << "Value in X: " << X[i] << std::endl; temptotal += X[i]; } std::cout << "Temp total: " << temptotal << std::endl;它产生以下输出: Temp total: 18446744073709551603 。
当我遍历GDB中的求和循环时(在temptotal += X[i]; ),我可以print X[i] :
(gdb) p X[i] $1 = (reference) @0x100100c80: -1在添加之前和之后打印temptotal :
(gdb) p temptotal $2 = 0 (gdb) p temptotal $3 = 18446744073709551615我相当肯定,我添加内存位置而不是内存位置的值 - 我可能是错误的 - 但我不知道为什么我不在X[i]添加值。 当我用readings[i].x的值填充X时,它们是double readings[i].x值,然后我将其输入到int 。 这是我的错误吗? 在GDB中,我对类型的readings[i].x值应该是一个整数,或者至少是某种类型的数值:
(gdb) p (int)readings[i].x $1 = -1我很难过; 如果有人能够说明为什么我不添加我认为我所添加的内容,我会很感激。
编辑 这是从int到unsigned long的投射。 我翻遍了它,按“提交问题”并发现我的错误。 虽然,我仍然不明白为什么它正在做它正在做的事情 - 任何光照都会很好 - 为什么,如果我将一个int添加到unsigned long ,它是否不从int取值?
I have a vector containing integer values, which I wish to sum. However, it's summing the total wrong - if the vector holds 10 ints between -10 and 10, my total should not be 18446744073709551602.
I declare the vector, and populate it with values:
std::vector<int> X; for ( int i = 0; i < readings.size() ; ++i ) { X.push_back(readings[i].x);I then sum and display:
unsigned long temptotal = 0; for ( int i = 0; i < readings.size() ; ++i ) { std::cout << "Value in X: " << X[i] << std::endl; temptotal += X[i]; } std::cout << "Temp total: " << temptotal << std::endl;which produces the following output: Temp total: 18446744073709551603.
When I step through the summation loop in GDB, (at temptotal += X[i];), I can print X[i]:
(gdb) p X[i] $1 = (reference) @0x100100c80: -1Printing temptotal before and after the addition is performed:
(gdb) p temptotal $2 = 0 (gdb) p temptotal $3 = 18446744073709551615I'm fairly sure that I'm adding the memory locations rather than the values in the memory locations - I could be wrong on that - but I'm not sure why I'm not adding the values at X[i]. When I populate X with the values from readings[i].x, they're doubles, which I then type-cast to ints. Is this where my error is? Checking in GDB, my type-cast of the readings[i].x value should be an int, or, at least, a number of some kind:
(gdb) p (int)readings[i].x $1 = -1I'm stumped; if somebody could shed some light on why I'm not adding what I think I'm adding, I'd be grateful.
EDIT It's the casting from int to unsigned long. I went over it and over it, pressed "submit question" and spotted my error. Although, I still don't understand why it's doing what it's doing - any light shed on that would be wonderful - why, if I'm adding an int to an unsigned long, does it not take the value from the int?
最满意答案
这是因为你的temptotal是没有签名的。 所有添加到它的数字都被解释为无符号的,因此小的负数变成二进制补码表示中的大正数。
This is because your temptotal is unsigned. All numbers added to it are interpreted as unsigned, hence small negative numbers become large positive numbers in two's complement representation.
更多推荐
发布评论