783

I have two integer values a and b, but I need their ratio in floating point. I know that a < b and I want to calculate a / b, so if I use integer division I'll always get 0 with a remainder of a.

How can I force c to be a floating point number in Python 2 in the following?

c = a / b

In 3.x, the behaviour is reversed; see Why does integer division yield a float instead of another integer? for the opposite, 3.x-specific problem.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319

11 Answers11

851

In Python 2, division of two ints produces an int. In Python 3, it produces a float. We can get the new behaviour by importing from __future__.

>>> from __future__ import division
>>> a = 4
>>> b = 6
>>> c = a / b
>>> c
0.66666666666666663
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Michael Fairley
  • 12,980
  • 4
  • 26
  • 23
  • 91
    If you go this route and still need some integer division, you can do it like `a // b` – Steve Trout Aug 12 '09 at 18:35
  • 34
    Note that in Python 3, this is the default behavior. – Sam DeFabbia-Kane Aug 12 '09 at 21:23
  • 7
    It's also the default behavior in the Idle (ipython) shell, but not the ipython script interpreter, which can really cause headaches when debugging scripts run by the default system python interpreter. – hobs Sep 23 '11 at 04:53
  • 4
    This doesn't change the behavior of operator.div, though. To do that, use operator.truediv(). – Caltor Oct 03 '13 at 10:57
  • 19
    Note that `from __future__ import division` must be at the very beginning of the file – yannis Aug 20 '17 at 17:50
  • Thanks for this answer! Was driving me crazy. Why on earth did they choose to do that in Python 2? I've never seen that before. – xxSithRagexx Feb 27 '21 at 18:31
  • 2
    @xxSithRagexx Python 2 followed a common convention from other languages, particularly C - int divided by int produced int result. In Python 3 they realized that was non-intuitive and changed it. – Mark Ransom Jul 20 '22 at 04:04
752

You can cast to float by doing c = a / float(b). If the numerator or denominator is a float, then the result will be also.


A caveat: as commenters have pointed out, this won't work if b might be something other than an integer or floating-point number (or a string representing one). If you might be dealing with other types (such as complex numbers) you'll need to either check for those or use a different method.

Steve Trout
  • 9,261
  • 2
  • 19
  • 30
  • I would cast both operands to a float just to make it clearer. Casting just the one that happens to trigger a special code path will be confusing to many people reading the code. You also get stuck in situations that Acute was in. – Charlie Apr 08 '15 at 16:14
  • 8
    this is *not* the better answer than using `__future__`. `b` might lose information when cast as `float`! The correct workaround is to multiply other multiplicand by `1.0`. – Antti Haapala -- Слава Україні Apr 12 '15 at 15:28
  • @AnttiHaapala is there a specific case where casting to `float` doesn't work but `__future__` does? I'd expect future division to use this same method as its implementation strategy. And how is multiplying by 1.0 different than casting to float? – Mark Ransom Sep 18 '15 at 14:29
  • 2
    It will fail for example if the casted argument is something else than a `float` or `int` already. The correct approach is discussed in detail in [PEP 238](https://www.python.org/dev/peps/pep-0238/). – Antti Haapala -- Слава Україні Sep 18 '15 at 15:23
210

How can I force division to be floating point in Python?

I have two integer values a and b, but I need their ratio in floating point. I know that a < b and I want to calculate a/b, so if I use integer division I'll always get 0 with a remainder of a.

How can I force c to be a floating point number in Python in the following?

c = a / b

What is really being asked here is:

"How do I force true division such that a / b will return a fraction?"

Upgrade to Python 3

In Python 3, to get true division, you simply do a / b.

>>> 1/2
0.5

Floor division, the classic division behavior for integers, is now a // b:

>>> 1//2
0
>>> 1//2.0
0.0

However, you may be stuck using Python 2, or you may be writing code that must work in both 2 and 3.

If Using Python 2

In Python 2, it's not so simple. Some ways of dealing with classic Python 2 division are better and more robust than others.

Recommendation for Python 2

You can get Python 3 division behavior in any given module with the following import at the top:

from __future__ import division

which then applies Python 3 style division to the entire module. It also works in a python shell at any given point. In Python 2:

>>> from __future__ import division
>>> 1/2
0.5
>>> 1//2
0
>>> 1//2.0
0.0

This is really the best solution as it ensures the code in your module is more forward compatible with Python 3.

Other Options for Python 2

If you don't want to apply this to the entire module, you're limited to a few workarounds. The most popular is to coerce one of the operands to a float. One robust solution is a / (b * 1.0). In a fresh Python shell:

>>> 1/(2 * 1.0)
0.5

Also robust is truediv from the operator module operator.truediv(a, b), but this is likely slower because it's a function call:

>>> from operator import truediv
>>> truediv(1, 2)
0.5

Not Recommended for Python 2

Commonly seen is a / float(b). This will raise a TypeError if b is a complex number. Since division with complex numbers is defined, it makes sense to me to not have division fail when passed a complex number for the divisor.

>>> 1 / float(2)
0.5
>>> 1 / float(2j)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't convert complex to float

It doesn't make much sense to me to purposefully make your code more brittle.

You can also run Python with the -Qnew flag, but this has the downside of executing all modules with the new Python 3 behavior, and some of your modules may expect classic division, so I don't recommend this except for testing. But to demonstrate:

$ python -Qnew -c 'print 1/2'
0.5
$ python -Qnew -c 'print 1/2j'
-0.5j
Community
  • 1
  • 1
Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
  • 4
    "1 // 2 = 0", "1 // 2.0 = 0.0" -- interesting little gotcha, even if it's an integer division, if any of the operands is float then the result is a whole number but also float. I was using an integer division to calculate a list index and getting an error because of that. – R. Navega Dec 16 '18 at 20:46
  • I prefer `a / (b + 0.0)` just because addition is usually faster than multiplication, even if the difference is negligible. – Mark Ransom Jul 20 '22 at 04:08
137
c = a / (b * 1.0)
Pinochle
  • 5,515
  • 2
  • 26
  • 20
67

In Python 3.x, the single slash (/) always means true (non-truncating) division. (The // operator is used for truncating division.) In Python 2.x (2.2 and above), you can get this same behavior by putting a

from __future__ import division

at the top of your module.

newacct
  • 119,665
  • 29
  • 163
  • 224
32

Just making any of the parameters for division in floating-point format also produces the output in floating-point.

Example:

>>> 4.0/3
1.3333333333333333

or,

>>> 4 / 3.0
1.3333333333333333

or,

>>> 4 / float(3)
1.3333333333333333

or,

>>> float(4) / 3
1.3333333333333333
gsbabil
  • 7,505
  • 3
  • 26
  • 28
  • 5
    But you might later be tempted to do `1.0 + 1/3` or `float(c) + a/b` or `float(a/b)` and you'll be disappointed with the answer. Better to use python 3+ or import the `__future__.division` module, (see accepted answer), to always get the answer you expect. The existing division rules create insidious, hard-to-trace math error. – hobs Sep 23 '11 at 04:47
  • @JoeCondron Did you try `python -c 'a=10; b=3.0; print a/b'`? – gsbabil Sep 18 '15 at 18:51
  • I didn't have to because it obviously works in this scenario. However, what if `a` and 'b', e.g., are the outputs of an integer-value function? E.g., `a = len(list1), b = len(list2)`. – JoeCondron Sep 18 '15 at 20:44
  • @JoeCondron: good point. I just updated the answer to include `float(..)`. I think multiplying by `1.0`, as @Pinochle suggested below, could also be useful. – gsbabil Sep 18 '15 at 21:18
22

Add a dot (.) to indicate floating point numbers

>>> 4/3.
1.3333333333333333
Georgy
  • 12,464
  • 7
  • 65
  • 73
Alexander
  • 12,424
  • 5
  • 59
  • 76
14

This will also work

>>> u=1./5
>>> print u
0.2
Georgy
  • 12,464
  • 7
  • 65
  • 73
Gaurav Agarwal
  • 18,754
  • 29
  • 105
  • 166
  • 5
    And how are you going to apply this approach if the numerator and denominator are both variables? – stackoverflowuser2010 Aug 12 '16 at 19:02
  • 1
    Because it doesn't work when variables are used for abstraction. Almost no meaningful code has values hardcoded like that. – Keir Simmons Mar 02 '17 at 06:01
  • 1
    This has little votes because this answer doesn't answer the question, and isn't a general answer at all. In an answer it's also important first to show why this works. It's very simple: if the numerator or denominator is a float, the result will be a float. Usually you don't use python as a plaintext calculator, so you want an answer for variables `a` and `b`. – TimZaman Aug 07 '17 at 08:51
  • For the longest time, I actually thought `./` was a valid operator in python that allows you to do floating point division. This is why it is good to use white space judiciously, in any programming language – smac89 Nov 28 '19 at 22:50
7

If you want to use "true" (floating point) division by default, there is a command line flag:

python -Q new foo.py

There are some drawbacks (from the PEP):

It has been argued that a command line option to change the default is evil. It can certainly be dangerous in the wrong hands: for example, it would be impossible to combine a 3rd party library package that requires -Qnew with another one that requires -Qold.

You can learn more about the other flags values that change / warn-about the behavior of division by looking at the python man page.

For full details on division changes read: PEP 238 -- Changing the Division Operator

stephenbez
  • 5,598
  • 3
  • 26
  • 31
2
from operator import truediv

c = truediv(a, b)
JoeCondron
  • 8,546
  • 3
  • 27
  • 28
  • 2
    That's not ideal, though, since it doesn't work in the case where `a` is an int and `b` is a float. A better solution along the same lines is to do `from operator import truediv` and then use `truediv(a, b)`. – Mark Dickinson Sep 18 '15 at 12:39
  • Yeah you're right. I was assuming both integers as this is the only time when the division ops differ but you really want a general solution. I didn't actually know you could import the operator or that it doesn't work at all for float divisors. Answer edited. – JoeCondron Sep 18 '15 at 14:17
-1
from operator import truediv

c = truediv(a, b)

where a is dividend and b is the divisor. This function is handy when quotient after division of two integers is a float.

Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
Chetan Yeshi
  • 125
  • 1
  • 3