标签云

微信群

扫码加入我们

WeChat QR Code

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


Note that from __future__ import division must be at the very beginning of the file

2019年12月07日57分03秒

Also the problem is if you want the integer division in one place but the float division in another place of the file...

2019年12月07日57分03秒

"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.

2019年12月07日57分03秒

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.

2019年12月07日57分03秒

JoeCondron Did you try python -c 'a=10; b=3.0; print a/b'?

2019年12月07日57分03秒

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).

2019年12月07日57分03秒

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.

2019年12月07日57分03秒

How are you going to apply this approach if the numerator and denominator are both variables?

2019年12月07日57分03秒

I assume you refer to the first example, if that is so , i would just use float() on one of the variables.

2019年12月07日57分03秒

And how are you going to apply this approach if the numerator and denominator are both variables?

2019年12月07日57分03秒

Because it doesn't work when variables are used for abstraction. Almost no meaningful code has values hardcoded like that.

2019年12月07日57分03秒

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.

2019年12月07日57分03秒

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

2019年12月07日57分03秒

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).

2019年12月07日57分03秒

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.

2019年12月07日57分03秒