Python中数字的小数部分

Python中数字的小数部分,python,python-2.7,Python,Python 2.7,我有以下程序 def F_inf(a,b): x1=a.numerator/a.denominator x2=b.numerator/b.denominator if x1<x2: print "a<b" elif x1>x2: print "a>b" else: print "a=b" a=Fraction(10,4) b=Fraction(10,4) F_inf(a, b)

我有以下程序

def F_inf(a,b):
    x1=a.numerator/a.denominator
    x2=b.numerator/b.denominator

        if x1<x2:
            print "a<b"
    elif x1>x2:
        print "a>b"
    else: print "a=b" 

a=Fraction(10,4)
b=Fraction(10,4)

F_inf(a, b)
def_inf(a,b):
x1=a.分子/a.分母
x2=b.分子/b.分母
如果x1x2:
打印“a>b”
其他:打印“a=b”
a=分数(10,4)
b=分数(10,4)
F_inf(a,b)
当我执行它时,x1只接收分数的整数值,例如,如果我必须计算2/4,x1等于0而不是0.5。 我该怎么办?
谢谢

听起来你在用Python2。最好的解决方案是切换到Python3(不仅仅是因为划分,而是因为)

除此之外,你还有两个选择

from __future__ import division
# include ^ as the first line in your file to use float division by default


您使用的是哪个Python版本?Python2.x返回二除整数integers@MoinuddinQuadri:他们使用
print
作为语句,而不是函数调用,因此它肯定是Python2。您的建议是切换到Python3,因为Python2.x返回两个整数除以的整数?@moinuddinqueadri,不,我的建议是切换到Python3,因为Python2是一个旧版本,只建议用于支持现有的遗留软件。OP似乎仍在学习该语言,因此没有理由学习2。这是最容易切换的时间。除法位是一种奖励。谢谢,它与1的乘法一起工作。0@StefanPochmann你说得对。我把
/
/
*
的优先顺序弄错了。括号中加了一个。我想说你不能把它们的优先级“颠倒过来”,因为它们有相同的:-)
a = 1
b = 2
c = a / (1.0*b) # multiplying by 1.0 forces the right side of the division to be a float
#c == 0.5 here