strange math?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • joe.hrbek@gmail.com

    #1

    strange math?

    Hello everyone, I'm experimenting with python and i'm following this
    tutorial:
    http://docs.python.org/tut/node6.htm...00000000000000 I'm
    in section 4.7.5 Lambda Forms. In this section I was working along and
    I noticed something strange. It happened because of a typo. Below is
    a copy/paste from my idle session:
    [color=blue][color=green][color=darkred]
    >>>def make_incremento r(n):[/color][/color][/color]
    return lambda x: x+n
    [color=blue][color=green][color=darkred]
    >>>f=make_incre mentor(42)
    >>>f(0)[/color][/color][/color]
    42[color=blue][color=green][color=darkred]
    >>>f(1)[/color][/color][/color]
    43[color=blue][color=green][color=darkred]
    >>>f(10)[/color][/color][/color]
    52[color=blue][color=green][color=darkred]
    >>>f(0)[/color][/color][/color]
    42[color=blue][color=green][color=darkred]
    >>>f(01)[/color][/color][/color]
    43[color=blue][color=green][color=darkred]
    >>>f(02)[/color][/color][/color]
    44[color=blue][color=green][color=darkred]
    >>>f(010)[/color][/color][/color]
    50[color=blue][color=green][color=darkred]
    >>>42+010[/color][/color][/color]
    50

    The first f(01) was a mistake. I accidentally forgot to delete the
    zero, but to my suprise, it yielded the result I expected. So, I tried
    it again, and viola, the right answer. So, I decided to really try and
    throw it for a loop, f(010), and it produced 50. I expected 52
    (42+10). Why doesn't python ignore the first zero and produce a result
    of 52? It ignored the first zero for f(01) and f(02). Hmm. I know, I
    know, why am I sending it a 01,02, or a 010 to begin with? Like I
    said, it was an accident, but now i'm curious. I'm not a computer
    science major so please be kind with any explanations.

  • Steven Bethard

    #2
    Re: strange math?

    joe.hrbek@gmail .com wrote:[color=blue][color=green][color=darkred]
    >>>> f(01)[/color][/color]
    > 43[color=green][color=darkred]
    >>>> f(02)[/color][/color]
    > 44[color=green][color=darkred]
    >>>> f(010)[/color][/color]
    > 50[color=green][color=darkred]
    >>>> 42+010[/color][/color]
    > 50
    >
    > The first f(01) was a mistake. I accidentally forgot to delete the
    > zero, but to my suprise, it yielded the result I expected. So, I tried
    > it again, and viola, the right answer. So, I decided to really try and
    > throw it for a loop, f(010), and it produced 50. I expected 52
    > (42+10). Why doesn't python ignore the first zero and produce a result
    > of 52? It ignored the first zero for f(01) and f(02). Hmm. I know, I
    > know, why am I sending it a 01,02, or a 010 to begin with? Like I
    > said, it was an accident, but now i'm curious. I'm not a computer
    > science major so please be kind with any explanations.[/color]

    Python isn't ignoring the initial 0. It reads the initial 0 as
    indicating that the following number is expressed in octal. You can see
    this if you try any number with a digit higher than 7 in it:
    [color=blue][color=green][color=darkred]
    >>> 08[/color][/color][/color]
    Traceback ( File "<interacti ve input>", line 1
    08
    ^
    SyntaxError: invalid token

    So you get 8 added to your number when you write 010 because eight is
    spelled as 10 in octal.

    The leading-zero notation is unfortunate, and there has been some recent
    discussion[1][2] on python-dev about trying to change the prefix to
    ``0o`` or ``0c`` in Python 3.0 but for the moment at least it looks like
    we're stuck with the confusing leading zero.

    [1]http://mail.python.org/pipermail/python-dev/2006-January/060262.html
    [2]http://mail.python.org/pipermail/python-dev/2006-February/060277.html

    STeVe

    Comment

    • Christian Stapfer

      #3
      Re: strange math?

      <joe.hrbek@gmai l.com> wrote in message
      news:1142745707 .384085.42050@i 39g2000cwa.goog legroups.com...[color=blue]
      > Hello everyone, I'm experimenting with python and i'm following this
      > tutorial:
      > http://docs.python.org/tut/node6.htm...00000000000000 I'm
      > in section 4.7.5 Lambda Forms. In this section I was working along and
      > I noticed something strange. It happened because of a typo. Below is
      > a copy/paste from my idle session:
      >[color=green][color=darkred]
      >>>>def make_incremento r(n):[/color][/color]
      > return lambda x: x+n
      >[color=green][color=darkred]
      >>>>f=make_incr ementor(42)
      >>>>f(0)[/color][/color]
      > 42[color=green][color=darkred]
      >>>>f(1)[/color][/color]
      > 43[color=green][color=darkred]
      >>>>f(10)[/color][/color]
      > 52[color=green][color=darkred]
      >>>>f(0)[/color][/color]
      > 42[color=green][color=darkred]
      >>>>f(01)[/color][/color]
      > 43[color=green][color=darkred]
      >>>>f(02)[/color][/color]
      > 44[color=green][color=darkred]
      >>>>f(010)[/color][/color]
      > 50[color=green][color=darkred]
      >>>>42+010[/color][/color]
      > 50
      >
      > The first f(01) was a mistake. I accidentally forgot to delete the
      > zero, but to my suprise, it yielded the result I expected. So, I tried
      > it again, and viola, the right answer. So, I decided to really try and
      > throw it for a loop, f(010), and it produced 50. I expected 52
      > (42+10). Why doesn't python ignore the first zero and produce a result
      > of 52?[/color]

      That's because python interprets 010 as *octal* 10
      which is *decimal* 8. Thus

      42+010 = 42+8 = 50

      which is quite as it should be...

      Regards,
      Christian


      Comment

      • Terry Reedy

        #4
        Re: strange math?

        [color=blue][color=green][color=darkred]
        >>>>f(01)[/color][/color]
        > 43[color=green][color=darkred]
        >>>>f(02)[/color][/color]
        > 44[color=green][color=darkred]
        >>>>f(010)[/color][/color]
        > 50[color=green][color=darkred]
        >>>>42+010[/color][/color]
        > 50[/color]

        Literal ints with lead 0 are interpreted as octal numbers (base 8) instead
        of decimal numbers (base 10). 01==1 either way, but 010 == 8.[color=blue][color=green][color=darkred]
        >>> 010[/color][/color][/color]
        8

        0x (zero eks) prefix indicate hexadecimal numbers:[color=blue][color=green][color=darkred]
        >>> 0xff[/color][/color][/color]
        255

        Terry Jan Reedy



        Comment

        • joe.hrbek@gmail.com

          #5
          Re: strange math?

          Thanks for the great reply, Steve, et al.

          -j

          Comment

          • Ron Adam

            #6
            Re: strange math?

            joe.hrbek@gmail .com wrote:
            [color=blue][color=green][color=darkred]
            >>>> f(01)[/color][/color]
            > 43[color=green][color=darkred]
            >>>> f(02)[/color][/color]
            > 44[color=green][color=darkred]
            >>>> f(010)[/color][/color]
            > 50[color=green][color=darkred]
            >>>> 42+010[/color][/color]
            > 50
            >
            > The first f(01) was a mistake. I accidentally forgot to delete the
            > zero, but to my suprise, it yielded the result I expected. So, I tried
            > it again, and viola, the right answer. So, I decided to really try and
            > throw it for a loop, f(010), and it produced 50. I expected 52
            > (42+10). Why doesn't python ignore the first zero and produce a result
            > of 52? It ignored the first zero for f(01) and f(02). Hmm. I know, I
            > know, why am I sending it a 01,02, or a 010 to begin with? Like I
            > said, it was an accident, but now i'm curious. I'm not a computer
            > science major so please be kind with any explanations.[/color]

            Number beginning with the digit '0' are octal (base 8), so 010 == 8.
            42 + 8 = 50.

            Numbers beginning with '0x' are base 16.

            Cheers,
            Ron

            Comment

            Working...