Thread: std::accumulate with double type

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    std::accumulate with double type

    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?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    The following code is what I've been having trouble with.

    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
    }
    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?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    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.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    g++ doesn't have an option for it... but you can still sort of hack it... provided your terminal is forgiving.

    Code:
    g++ $* 2>&1 | sed \
    		-e s/'warning'/'^[[1;33;40m'\&'^[[0;37;40m'/ \
    		-e s/'error'/'^[[1;31;40m'\&'^[[0;37;40m'
    Attachment 6919
    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!

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    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):
    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
    Could the fact that I'm doing it over an ssh connection matter?

    I guess it shouldn't since ls --color works fine.

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM