pow on valarray....

This is a discussion on pow on valarray.... within the C++ Programming forums, part of the General Programming Boards category; so the documentation for valarray says pow is implemented for both pow(valarray<T>&base, T&exponent) and pow(T&base, valarray<T>&exponents) so why is it ...

  1. #1
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839

    pow on valarray....

    so the documentation for valarray says pow is implemented for both pow(valarray<T>&base, T&exponent) and pow(T&base, valarray<T>&exponents)

    so why is it that the following:
    Code:
    valarray<double>history;
    pow(history-average(),2);
    is invalid? it says "could not find a match for pow<_Tp>(valarray<double>,int)"? casting 2 to double results in the same message, just replacing int with double in the error message.


    i'd like to:
    Code:
    double stdev = pow(pow(history-average(),2).sum()/history.size(),.5);
    ;

    is there some file i need to include besides math and valarray?

    the doc makes no mention of it...

    it's probably just something silly i'm overlooking, but anyone who can spot it gets my thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If I'm reading the prototype correctly, the first argument must be a reference -- that is, a lvalue (something that can be assigned to) -- and history-average() is not such a thing. Can you do it in two steps, subtract history first, and then run pow?

  3. #3
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    no, that doesn't work either.

    it seems that since the error message is saying that argument is in fact a valarray<double>, that history-average() should be a valid expression. the doc for valarray seems to corroborate this theory...

    valarray<T> operator-() const;

    Returns a new valarray object of the same size as the array in self where each element has been initialized by applying operator- to the corresponding element in self. This operation can only be applied to a valarray instantiated on a type T that supports an operator- returning T or a type convertible to T.

  4. #4
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    and anyway, i have the stdev whole calculation done in 3 steps, but it will certainly be convenient at some point in the future to run exponents on valarray.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, we can play compare and contrast: the following code works for me:
    Code:
    #include <valarray>
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main()
    {
        valarray<double> bob(6);
        for (int i = 0; i < 6; i++) {
            bob[i] = i;
        }
        bob = pow(bob-2.0, 2.0);
        for (int i = 0; i < 6; i++) {
            cout << bob[i] << " ";
        }
        cout << endl;
        return 0;
    }
    using MinGW. Does it work for you? If so, how does your other code differ?

  6. #6
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    hmm...#include <cmath> eh?

    that looks suspicous

    i was including <math>

    i will try that out at work tomorrow

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It looks, actually, like cmath is not needed (since it's re-defined, or overloaded, or whatever) in valarray itself.

    You can't have included <math> since there is no such thing. (In C, we have math.h, of course, but that still wouldn't get included with <math> either.)

  8. #8
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    math.h i mean


    tho if i don't specify an extension, BCB assumes .h.

  9. #9
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    Quote Originally Posted by tabstop View Post
    Well, we can play compare and contrast: the following code works for me:
    Code:
    #include <valarray>
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main()
    {
        valarray<double> bob(6);
        for (int i = 0; i < 6; i++) {
            bob[i] = i;
        }
        bob = pow(bob-2.0, 2.0);
        for (int i = 0; i < 6; i++) {
            cout << bob[i] << " ";
        }
        cout << endl;
        return 0;
    }
    using MinGW. Does it work for you? If so, how does your other code differ?
    negative

    debugger brings me here:
    Code:
    template <class _Tp>
    inline valarray<_Tp> pow(const valarray<_Tp>& __x, const _Tp& __c) {
      typedef typename valarray<_Tp>::_NoInit _NoInit;
      valarray<_Tp> __tmp(__x.size(), _NoInit());
      for (size_t __i = 0; __i < __x.size(); ++__i)
        __tmp[__i] = _STLP_DO_POW(_Tp)(__x[__i], __c);
      return __tmp;
    }
    and says

    "Call to undefined function '_STLP_DO_POW' "
    "Improper use to typedef _Tp"

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by m37h0d View Post
    negative

    debugger brings me here:
    Code:
    template <class _Tp>
    inline valarray<_Tp> pow(const valarray<_Tp>& __x, const _Tp& __c) {
      typedef typename valarray<_Tp>::_NoInit _NoInit;
      valarray<_Tp> __tmp(__x.size(), _NoInit());
      for (size_t __i = 0; __i < __x.size(); ++__i)
        __tmp[__i] = _STLP_DO_POW(_Tp)(__x[__i], __c);
      return __tmp;
    }
    and says

    "Call to undefined function '_STLP_DO_POW' "
    "Improper use to typedef _Tp"
    Well, I, ... well ... ... I don't know. I tested it on MinGW, and visual studio, and I even went to dinkumware's exam/proofer/syntax checker thing and tried there, and got success all the way 'round. I'm starting to think that maybe BCB's library is not up to scratch here.

  11. #11
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    looking like it.

    it's simple enough to write my own pow function; i just wanted to make sure i wan't doing it wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's the difference between pow() vs shift?
    By Overworked_PhD in forum C Programming
    Replies: 9
    Last Post: 01-06-2008, 12:04 PM
  2. Problem With Pow Function, Help?
    By verd in forum C++ Programming
    Replies: 11
    Last Post: 02-27-2006, 09:56 AM
  3. trying to use pow()
    By Mikecore in forum C Programming
    Replies: 9
    Last Post: 10-09-2005, 06:30 PM
  4. pow function returns wrong value
    By ckbales in forum C Programming
    Replies: 5
    Last Post: 02-01-2003, 08:46 PM
  5. Problem with pow()
    By RMacFly in forum C Programming
    Replies: 4
    Last Post: 09-18-2001, 11:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21