Is there anything wrong with std::pow?

Discussion in 'C++' started by Peng Yu, Sep 10, 2008.

  1. Peng Yu

    Peng Yu Guest

    Hi,

    The output of the following program is
    (-27.7128,16) (-9.09495e-12,32768)

    Obviously, y*y*y is not equal to x, even the magnitude is off. I'm
    wondering what is wrong here.

    Thanks,
    Peng

    #include <complex>
    #include <iostream>

    int main () {
    std::complex<double> x(2, 2);
    std::complex<double> y = std::pow(x, 1/.3);
    std::cout << y << " " << y * y * y << std::endl;
    }
     
    Peng Yu, Sep 10, 2008
    #1
    1. Advertising

  2. Peng Yu

    Peng Yu Guest

    On Sep 9, 8:29 pm, "Alf P. Steinbach" <> wrote:
    > * Peng Yu:
    >
    >
    >
    > > Hi,

    >
    > > The output of the following program is
    > > (-27.7128,16) (-9.09495e-12,32768)

    >
    > > Obviously, y*y*y is not equal to x, even the magnitude is off. I'm
    > > wondering what is wrong here.

    >
    > > Thanks,
    > > Peng

    >
    > > #include <complex>
    > > #include <iostream>

    >
    > > int main () {
    > > std::complex<double> x(2, 2);
    > > std::complex<double> y = std::pow(x, 1/.3);
    > > std::cout << y << " " << y * y * y << std::endl;
    > > }

    >
    > The second argument to pow.


    I don't quite understand what you mean. Would you please be more
    specific?

    Thanks,
    Peng
     
    Peng Yu, Sep 10, 2008
    #2
    1. Advertising

  3. Peng Yu

    Sjouke Burry Guest

    Peng Yu wrote:
    > On Sep 9, 8:29 pm, "Alf P. Steinbach" <> wrote:
    >> * Peng Yu:
    >>
    >>
    >>
    >>> Hi,
    >>> The output of the following program is
    >>> (-27.7128,16) (-9.09495e-12,32768)
    >>> Obviously, y*y*y is not equal to x, even the magnitude is off. I'm
    >>> wondering what is wrong here.
    >>> Thanks,
    >>> Peng
    >>> #include <complex>
    >>> #include <iostream>
    >>> int main () {
    >>> std::complex<double> x(2, 2);
    >>> std::complex<double> y = std::pow(x, 1/.3);
    >>> std::cout << y << " " << y * y * y << std::endl;
    >>> }

    >> The second argument to pow.

    >
    > I don't quite understand what you mean. Would you please be more
    > specific?
    >
    > Thanks,
    > Peng

    1/.3 equals 3.333333, perhaps you meant 1./3.
     
    Sjouke Burry, Sep 10, 2008
    #3
  4. Peng Yu

    Peng Yu Guest

    On Sep 9, 9:35 pm, Sjouke Burry <>
    wrote:
    > Peng Yu wrote:
    > > On Sep 9, 8:29 pm, "Alf P. Steinbach" <> wrote:
    > >> * Peng Yu:

    >
    > >>> Hi,
    > >>> The output of the following program is
    > >>> (-27.7128,16) (-9.09495e-12,32768)
    > >>> Obviously, y*y*y is not equal to x, even the magnitude is off. I'm
    > >>> wondering what is wrong here.
    > >>> Thanks,
    > >>> Peng
    > >>> #include <complex>
    > >>> #include <iostream>
    > >>> int main () {
    > >>> std::complex<double> x(2, 2);
    > >>> std::complex<double> y = std::pow(x, 1/.3);
    > >>> std::cout << y << " " << y * y * y << std::endl;
    > >>> }
    > >> The second argument to pow.

    >
    > > I don't quite understand what you mean. Would you please be more
    > > specific?

    >
    > > Thanks,
    > > Peng

    >
    > 1/.3 equals 3.333333, perhaps you meant 1./3.


    I'm sorry that I could pinpoint it. Thank you.

    Peng
     
    Peng Yu, Sep 10, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Jansson
    Replies:
    5
    Views:
    6,351
    Ivan Vecerina
    Mar 17, 2005
  2. Clueless Moron

    math.pow vs pow

    Clueless Moron, Nov 27, 2003, in forum: Python
    Replies:
    5
    Views:
    935
    John J. Lee
    Nov 28, 2003
  3. Michel Rouzic

    pow(2, 1/2) != pow(2, 0.5) problem

    Michel Rouzic, Jun 15, 2005, in forum: C Programming
    Replies:
    52
    Views:
    1,699
    Alan Balmer
    Jun 20, 2005
  4. aaragon
    Replies:
    5
    Views:
    300
  5. Peng Yu
    Replies:
    17
    Views:
    707
    Peng Yu
    Sep 14, 2008
Loading...

Share This Page