Is std::accumulate strictly bound to return int type values?
I have a vector with quite large double values, and when I use accumulate on it, it always returns -2147483646.
Is it possible to get accumulate to return a double value?
Is std::accumulate strictly bound to return int type values?
I have a vector with quite large double values, and when I use accumulate on it, it always returns -2147483646.
Is it possible to get accumulate to return a double value?
std::accumulate works fine with doubles, and will return a double if you pass it a container of doubles. Show your code if you are having troubles with it.
The following code is what I've been having trouble with.
I've checked the copied "result" vector and it is a vector with double values. I was wondering why this particular prediction function was always returning 0 and I discovered that the value of accumulate was always -2147483646. How do I fix this?Code:template <class T1, class T2> int GetPrediction(vector<T1>& v1, vector<T2>& v2) //v1 = weight vector, v2 = feature vector { vector<T1> result(v1); double sum; transform(v2.begin(), v2.end(), result.begin(), result.begin(), multiplies<double>()); // v2 is a vector of integers return accumulate(result.begin(), result.end(), 0) > THRESHOLD ? 1 : 0; // THRESHOLD = 0 }
>> accumulate(result.begin(), result.end(), 0)
The literal 0 is an int so accumulate assumes you are accumulating ints. I'm a little surprised it doesn't warn or error, but what you should be using is 0.0 which is a double.
Apparently I was wrong above when I said it would return a double if you pass it a container of doubles. I guess it uses the type of the initial value.
Thanks. Actually it did throw a warning message. But I'm using g++ on a linux machine with a tiny screen and sometimes it gives me a headache to just look at the stuff.
While we're at it, could you tell me if there's a way to add syntax highlighting to g++ warning and error messages in the console? I hope I don't have to do any bash programming. I know almost nothing about it.
>> could you tell me if there's a way to add syntax highlighting to g++ warning and error messages in the console?
I couldn't, it's been years since I've spent much time with gcc or bash. Maybe somebody else will know, or try the Tech Board.
g++ doesn't have an option for it... but you can still sort of hack it... provided your terminal is forgiving.
Attachment 6919Code:g++ $* 2>&1 | sed \ -e s/'warning'/'^[[1;33;40m'\&'^[[0;37;40m'/ \ -e s/'error'/'^[[1;31;40m'\&'^[[0;37;40m'
Note that ^[ is an escape sequence. You'll do yourself a favour to not copy and paste.
Callou collei we'll code the way
Of prime numbers and pings!
Thanks for the script. But it doesn't work. I changed the mode to executable, added the path and everything (and I tweaked it a bit: g++ $* -Wall 2>&1 ...).
When I run it, I get the following (and no highlighting whatsoever):
Could the fact that I'm doing it over an ssh connection matter?Code:perceptron.cpp: In function `void GetWeightVector(std::vector<T1, std::allocator<_CharT> >&, std::vector<std::vector<T2, std::allocator<_T2> >, std::allocator<std::vector<T2, std::allocator<_T2> > > >&, int) [with T1 = double, T2 = int]': perceptron.cpp:139: instantiated from here perceptron.cpp:218: [1;33;40mwarning[0;37;40m: comparison between signed and unsigned integer expressions perceptron.cpp:139: instantiated from here perceptron.cpp:238: [1;33;40mwarning[0;37;40m: comparison between signed and unsigned integer expressions perceptron.cpp: In function `int ArgMax(std::vector<T1, std::allocator<_CharT> >&, std::vector<T2, std::allocator<_T2> >&, int) [with T1 = double, T2 = int]': perceptron.cpp:161: instantiated from here perceptron.cpp:269: [1;33;40mwarning[0;37;40m: comparison between signed and unsigned integer expressions
I guess it shouldn't since ls --color works fine.
My best guess:
Your editor may have borked with the escape sequences.
If not that, then sorry. It was a bit of a kludge in the first place.
Callou collei we'll code the way
Of prime numbers and pings!