what is this?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Herbert Fritsch

    what is this?

    Hi,
    I'm a real beginner with python and I don't really know if I will get it.
    But here is one of my first exercises:

    Python 2.3+ (#1, Sep 23 2003, 23:07:16)
    [GCC 3.3.1 (SuSE Linux)] on linux2
    Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
    >>> a = 3
    >>> b = 4
    >>> b / 2 + a[/color][/color][/color]
    5[color=blue][color=green][color=darkred]
    >>> b / (2.0 + a)[/color][/color][/color]
    0.8000000000000 0004

    There must be something wrong. I' running the new Suse 9.0.

    Herbert
  • Peter Otten

    #2
    Re: what is this?

    Herbert Fritsch wrote:
    [color=blue][color=green][color=darkred]
    >>>> b / (2.0 + a)[/color][/color]
    > 0.8000000000000 0004
    >
    > There must be something wrong. I' running the new Suse 9.0.[/color]

    No :-)

    That's the way *all* floating point operations work, nothing python-specific
    here. See



    Peter

    Comment

    • Jay Dorsey

      #3
      Re: what is this?

      Herbert Fritsch wrote:[color=blue][color=green][color=darkred]
      >>>>a = 3
      >>>>b = 4
      >>>>b / 2 + a[/color][/color]
      >
      > 5
      >[color=green][color=darkred]
      >>>>b / (2.0 + a)[/color][/color]
      >
      > 0.8000000000000 0004
      >
      > There must be something wrong. I' running the new Suse 9.0.[/color]

      Not really. Think of the order of operations in mathematics.
      Multiplication and division are ranked higher than addition. Using
      parentheses for grouping is higher (before) mulitplication and division.

      First example:

      4 / 2 + 3 yields 2 + 3 yields 5

      Second:

      4 / (2.0 + 3)

      Python coerces the 3 into a floating point number, so lets write all
      these as floating points:

      4.0 / (2.0 + 3.0)

      Order of operations says do the parentheses first:

      4.0 / 5.0 yields 0.8000000000000 004

      Theres a listing of Pythons order of operations somewhere in the
      documentation on python.org (couldn't find it with a quick search).

      :)




      Comment

      Working...