-1/2

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Serve Lau

    -1/2

    What is the expected result of -1/2 in python?

  • Christian Heimes

    #2
    Re: -1/2

    Serve Lau wrote:
    What is the expected result of -1/2 in python?
    0

    Christian

    Comment

    • Gary Herron

      #3
      Re: -1/2

      Serve Lau wrote:
      What is the expected result of -1/2 in python?
      >
      --
      http://mail.python.org/mailman/listinfo/python-list
      From the manual:


      The result is always rounded towards minus infinity: 1/2 is 0, (-1)/2 is
      -1, 1/(-2) is -1, and (-1)/(-2) is 0.


      Gary Herron

      Comment

      • Gary Herron

        #4
        Re: -1/2

        Christian Heimes wrote:
        Serve Lau wrote:
        >
        >What is the expected result of -1/2 in python?
        >>
        >
        0
        >
        >
        No. That's not right. (It would be in C/C++ and many other languages.)

        See my other response for the correct answer.

        Gary Herron


        Comment

        • drobinow@gmail.com

          #5
          Re: -1/2

          On Jun 22, 2:32 pm, "Serve Lau" <ni...@qinqin.c omwrote:
          What is the expected result of -1/2 in python?
          I would say -1, but it depends on whether the "-" is a unary minus.
          >>-1/2
          -1
          >>3 -1/2
          3

          Comment

          • Terry Reedy

            #6
            Re: -1/2



            Serve Lau wrote:
            What is the expected result of -1/2 in python?
            Python 3.0b1 (r30b1:64403M, Jun 19 2008, 14:56:09) [MSC v.1500 32 bit
            (Intel)]
            n win32
            Type "help", "copyright" , "credits" or "license" for more information.
            >>-1/2
            -0.5
            as expected ;-)

            -1//2 is as Gary Herron specified from the manual, for reasons posted
            here several times and which should be in the FAQ

            Comment

            • Lie

              #7
              Re: -1/2

              On Jun 23, 1:32 am, "Serve Lau" <ni...@qinqin.c omwrote:
              What is the expected result of -1/2 in python?

              Operator precedence:
              Both python 2 and 3 follows the same rule for operator precedence,
              see: http://www.ibiblio.org/g2swap/byteof...recedence.html
              . In -1/2, unary negative takes precedence, then division, i.e. it's
              interpreted as ((-1) / 2).

              Py3k and Python 2 differences:
              Before Python 3, integer division is always rounds to minus infinity.
              In Python 3, integer division yields floats. To use integer division
              in Python 3, you use // operator.

              Simple answer:
              Python 2: (-1)/2 = round(-0.5) = -1
              Py3k: (-1)/2 = float(-1) / float(2) = -0.5

              Comment

              • Duncan Booth

                #8
                Re: -1/2

                Lie <Lie.1296@gmail .comwrote:
                On Jun 23, 1:32 am, "Serve Lau" <ni...@qinqin.c omwrote:
                >What is the expected result of -1/2 in python?
                >
                >
                Operator precedence:
                Both python 2 and 3 follows the same rule for operator precedence,
                see:

                l . In -1/2, unary negative takes precedence, then division, i.e. it's
                interpreted as ((-1) / 2).
                >
                Py3k and Python 2 differences:
                Before Python 3, integer division is always rounds to minus infinity.
                In Python 3, integer division yields floats. To use integer division
                in Python 3, you use // operator.
                >
                Simple answer:
                Python 2: (-1)/2 = round(-0.5) = -1
                Py3k: (-1)/2 = float(-1) / float(2) = -0.5
                >
                Python 2.x gives -1 or -0.5 depending on the division mode in operation and
                for -1 may also generate a deprecation warning:


                C:\>python25\py thon -Qnew -c "print -1/2"
                -0.5

                C:\>python25\py thon -Qwarn -c "print -1/2"
                -c:1: DeprecationWarn ing: classic int division
                -1


                --
                Duncan Booth http://kupuguy.blogspot.com

                Comment

                Working...