Results 1 to 10 of 10

Thread: [SOLVED] [Python] Divide numbers without rounding them automatically?

  1. #1
    Join Date
    Jan 2008
    Location
    Whenever the food is.
    Beans
    1,203
    Distro
    Kubuntu

    [SOLVED] [Python] Divide numbers without rounding them automatically?

    Python automatically rounds answers with dividing.


    If I use the calculator program that comes with ubuntu:
    92 / 255 = 0,360784314

    But when I do it with python interactive console, I get:
    92 / 255 = 0


    So how to prevent that auto-rounding?
    Keyboard not found!

    Press any key to continue...

  2. #2
    Join Date
    Jun 2008
    Location
    Sin City
    Beans
    588
    Distro
    Ubuntu UNR

    Re: [Python] Divide numbers without rounding them automatically?

    Don't divide using integers.

    Code:
    >>> 92.0 / 255.0
    0.36078431372549019
    Or, more completely, learn about Python's different data types.
    Warning: Any code examples I write are probably untested and contain bugs. Do not execute directly. Look for intent, not accuracy, please!
    L.A.G. - Jobs Dissembles - 2010/4/29

  3. #3
    Join Date
    Aug 2006
    Location
    60°27'48"N 24°48'18"E
    Beans
    3,458

    Re: [Python] Divide numbers without rounding them automatically?

    +1... it is not "auto-rounding", it is perfectly correct integer division...
    LambdaGrok. | #ubuntu-programming on FreeNode

  4. #4
    WW is offline Iced Blended Vanilla Crème Ubuntu
    Join Date
    Oct 2004
    Beans
    1,532

    Re: [SOLVED] [Python] Divide numbers without rounding them automatically?

    Eventually the Python language will change so that division of integers automatically converts the arguments to floating point. You can access this behavior now by putting the statement
    Code:
    from __future__ import division
    in your code. Then division of integers will behave the way you want.

    For example,
    Code:
    >>> 10/3
    3
    >>> from __future__ import division
    >>> 10/3
    3.3333333333333335
    >>> 10/2
    5.0
    >>> 92/255
    0.36078431372549019
    >>>

  5. #5
    Join Date
    Jun 2008
    Location
    Sin City
    Beans
    588
    Distro
    Ubuntu UNR

    Re: [SOLVED] [Python] Divide numbers without rounding them automatically?

    Ierrrr, what got into Guido's head for that to go through?
    Warning: Any code examples I write are probably untested and contain bugs. Do not execute directly. Look for intent, not accuracy, please!
    L.A.G. - Jobs Dissembles - 2010/4/29

  6. #6
    Join Date
    Aug 2007
    Location
    Novocastria, Australia
    Beans
    751
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: [SOLVED] [Python] Divide numbers without rounding them automatically?

    Quote Originally Posted by WW View Post
    ... Then division of integers will behave the way you want.
    To me that seems somewhat... ridiculous. Every other programming language I know does integer division when presented with integers and floating point division when presented with floating point numbers. It doesn't seem like a good idea IMO to make things inconsistent just to appease beginners who might not understand the typing system correctly.

  7. #7
    Join Date
    Aug 2006
    Location
    60°27'48"N 24°48'18"E
    Beans
    3,458

    Re: [SOLVED] [Python] Divide numbers without rounding them automatically?

    My suspicion about the rationale is that it's got to do with "least surprise" with duck typing. If you have two variables, a and b which you know are at least numeric values, then a / b always produces a floating-point division result regardless of whether one or both are int. The point being here that you might actually get a nasty surprise if you're manipulating a bunch of number variables, and then all of a sudden, under the hood, two of them happen to be integers, and you get an integer division result.

    I have to agree that it sounds odd from a programmer's perspective and I don't like it, but... that could be a reason.
    LambdaGrok. | #ubuntu-programming on FreeNode

  8. #8
    Join Date
    Jun 2008
    Location
    Sin City
    Beans
    588
    Distro
    Ubuntu UNR

    Re: [SOLVED] [Python] Divide numbers without rounding them automatically?

    But... but... that is what a / float(b) is for.

    Le sigh.... I dropped Perl because of stuff like this. :/
    Warning: Any code examples I write are probably untested and contain bugs. Do not execute directly. Look for intent, not accuracy, please!
    L.A.G. - Jobs Dissembles - 2010/4/29

  9. #9
    Join Date
    Mar 2006
    Beans
    67

    Re: [SOLVED] [Python] Divide numbers without rounding them automatically?

    I don't really see the problem here. I would think the majority of the time people want to use floating point division. Seems logical that the default ' / ' operator would perform floating point and ' // ' uses the exception, integer division.

    Whether or not it needs changing is a different story. I must say I'm glad python is being progressive. There are plenty of languages that stick with tradition. -I feel like I'm coming off as a liberal =)
    Last edited by babyhuey; October 14th, 2008 at 05:07 PM.

  10. #10
    Join Date
    Jul 2008
    Location
    Dublin, Ireland
    Beans
    633
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: [SOLVED] [Python] Divide numbers without rounding them automatically?

    Quote Originally Posted by NovaAesa View Post
    To me that seems somewhat... ridiculous. Every other programming language I know does integer division when presented with integers and floating point division when presented with floating point numbers. It doesn't seem like a good idea IMO to make things inconsistent just to appease beginners who might not understand the typing system correctly.
    I would assume that it goes just in the same line as integers promoting to bigger size integer types once they would overflow. You can always back convert to an integer type to loose those 'unwanted' decimals.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •