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

1. ## [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?

2. ## 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.

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

+1... it is not "auto-rounding", it is perfectly correct integer division...

4. WW
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. ## Re: [SOLVED] [Python] Divide numbers without rounding them automatically?

Ierrrr, what got into Guido's head for that to go through?

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

Originally Posted by WW
... 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. ## 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.

8. ## 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. :/

9. Just Give Me the Beans!
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. Skinny Soy Caramel Ubuntu
Join Date
Jul 2008
Location
Dublin, Ireland
Beans
633
Distro
Ubuntu 9.10 Karmic Koala

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

Originally Posted by NovaAesa
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.

#### Posting Permissions

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