标签云

微信群

扫码加入我们

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


Wish there was more of an explanation here for us python noobs. It looks like a snark about a "better" future version of python. edit: never mind, it is explained down stream...

2019年07月24日56分24秒

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

2019年07月24日56分24秒

I feel the answer below by Aaron-hall is more complete.

2019年07月24日56分24秒

rebusB I just edited it to explain. Though still, Aaron Hall's answer is more detailed.

2019年07月24日56分24秒

Hi, I am from the future doing my first baby steps on Python. My next question will be "how do I update to Python 3" :)

2019年07月24日56分24秒

"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年07月24日56分24秒

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年07月24日56分24秒

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

2019年07月24日56分24秒

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年07月23日56分24秒

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年07月24日56分24秒

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

2019年07月24日56分24秒

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

2019年07月24日56分24秒

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

2019年07月24日56分24秒

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

2019年07月24日56分24秒

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年07月24日56分24秒

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年07月24日56分24秒

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年07月24日56分24秒

Is this in Python 2?

2019年07月24日56分24秒