round off

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • W. Vanweersch

    round off

    Hello,

    The following looks strange to me in Python:
    7/-3 gives -3

    It looks like Python rounds off -2.33333 to -3! is this correct??
    Isn't it -2?




    Greets,



    Wim.

  • John Baxter

    #2
    Re: round off

    In article <mailman.441.10 67976033.702.py thon-list@python.org >,
    "W. Vanweersch" <n.vanweersch@h ccnet.nl> wrote:
    [color=blue]
    > Hello,
    >
    > The following looks strange to me in Python:
    > 7/-3 gives -3
    >
    > It looks like Python rounds off -2.33333 to -3! is this correct??
    > Isn't it -2?[/color]

    If you like, but not here. (It's really truncation, not rounding.)


    It can be unexpected. It keeps this sort of thing working:
    [color=blue][color=green][color=darkred]
    >>> (-7 / 3)* 3 + (-7 % 3)[/color][/color][/color]
    -7

    Truncate / toward - infinity. If it truncated toward zero, then you
    have to mess with the sign of the result of %, and other odd things
    happen.

    --John

    --
    Email to above address discarded by provider's server. Don't bother sending.

    Comment

    Working...