math.pow vs pow

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Clueless Moron

    math.pow vs pow

    Why do they act differently with respect to complex numbers?

    Python 2.2.2 (#1, Feb 24 2003, 19:13:11)
    [GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-4)] on linux2
    Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
    >>> pow(2, 0+1j)[/color][/color][/color]
    (0.769238901363 97211+0.6389612 7631363475j)[color=blue][color=green][color=darkred]
    >>>
    >>> import math
    >>> math.pow(2, 0+1j)[/color][/color][/color]
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: can't convert complex to float; use e.g. abs(z)[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    Apart from handling complex numbers, the built-in pow also has a
    modulo efficiency hack.

    When would I want to use math.pow?

  • John J. Lee

    #2
    Re: math.pow vs pow

    Clueless Moron <Bill.Gates@Mic rosoft.com> writes:
    [color=blue]
    > Why do they act differently with respect to complex numbers?[/color]
    [...][color=blue]
    > Apart from handling complex numbers, the built-in pow also has a
    > modulo efficiency hack.
    >
    > When would I want to use math.pow?[/color]

    For some applications, getting a complex result means you've made a
    mistake. And some people don't know what a complex number is, and
    would rather have their teeth drilled than find out.


    John

    You wouldn't know a subtle plan if it painted itself purple and danced
    naked on top of a harpsichord singing 'Subtle Plans Are Here Again'

    Blackadder

    Comment

    • Clueless Moron

      #3
      Re: math.pow vs pow

      John J. Lee wrote:[color=blue]
      > For some applications, getting a complex result means you've made a
      > mistake. And some people don't know what a complex number is, and
      > would rather have their teeth drilled than find out.[/color]

      Perhaps, but why should pow() be different from math.pow() in python?

      Comment

      • Georgy Pruss

        #4
        Re: math.pow vs pow


        "John J. Lee" <jjl@pobox.co m> wrote in message news:87k75lmlru .fsf@pobox.com. ..
        | For some applications, getting a complex result means you've made a
        | mistake. And some people don't know what a complex number is, and
        | would rather have their teeth drilled than find out.

        Can you provide an example when pow with real arguments gives a complex result,
        please?

        Georgy

        |
        |
        | John
        |
        | You wouldn't know a subtle plan if it painted itself purple and danced
        | naked on top of a harpsichord singing 'Subtle Plans Are Here Again'
        |
        | Blackadder


        Comment

        • Peter Otten

          #5
          Re: math.pow vs pow

          Georgy Pruss wrote:
          [color=blue]
          >
          > "John J. Lee" <jjl@pobox.co m> wrote in message
          > news:87k75lmlru .fsf@pobox.com. ..
          > | For some applications, getting a complex result means you've made a
          > | mistake. And some people don't know what a complex number is, and
          > | would rather have their teeth drilled than find out.
          >
          > Can you provide an example when pow with real arguments gives a complex
          > result, please?[/color]

          He could, if Python were not so reluctant:
          [color=blue][color=green][color=darkred]
          >>> pow(-5,.5)[/color][/color][/color]
          Traceback (most recent call last):
          File "<stdin>", line 1, in ?
          ValueError: negative number cannot be raised to a fractional power

          Sad day for the dentist:-)
          But:
          [color=blue][color=green][color=darkred]
          >>> pow(complex(-5), .5)[/color][/color][/color]
          (1.369151526412 4976e-16+2.2360679774 997898j)
          [color=blue][color=green][color=darkred]
          >>> pow(-5, complex(.5))[/color][/color][/color]
          (1.369151526412 4976e-16+2.2360679774 997898j)

          as you might expect.

          Peter

          Comment

          • John J. Lee

            #6
            Re: math.pow vs pow

            "Georgy Pruss" <see_signature_ _@hotmail.com> writes:
            [color=blue]
            > "John J. Lee" <jjl@pobox.co m> wrote in message news:87k75lmlru .fsf@pobox.com. ..
            > | For some applications, getting a complex result means you've made a
            > | mistake. And some people don't know what a complex number is, and
            > | would rather have their teeth drilled than find out.
            >
            > Can you provide an example when pow with real arguments gives a complex result,
            > please?[/color]
            [...]

            I was thinking about results of earlier calculations providing the
            argument for pow.


            John

            Comment

            Working...