且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将整数转换为浮点数?

更新时间:2023-08-27 21:54:40

2月27日下午7:05,ying ... @ gmail.com < ying ... @ gmail.comwrote:

我有以下功能,但''dx = abs(i2 - i1)/ min(i2 ,

i1)''总是返回0,你能告诉我怎样才能将它从

整数转换成浮点数?


def compareValue(n1,n2):

i1 = int(n1)

i2 = int(n2)


dx = abs(i2 - i1)/ min(i2,i1)

print dx

return dx< 0.05



x = x + 0.0


yi ***** @ gmail.com 写道:

我有以下功能,但是''dx = abs(i2 - i1)/ min(i2,

i1)''总是返回0,你能告诉我怎样才能将它转换成

要浮动的整数?



当一个分区中涉及两个整数时,结果也将是一个

分区。如果其中一个操作数是浮点数,那么结果将是一个

浮点数。因此,尝试将其中一个值投射到浮点数之前执行除法:
执行除法:


dx = float(abs(i2 - i1))/ min(i2 ,i1)


-Farshid


Farshid Lashkari写道:

当一个分区涉及两个整数时,结果也将是一个

分区。



我的坏,我的意思是结果也是*整数*


-Farshid


Hi, I have the following functions, but '' dx = abs(i2 - i1)/min(i2,
i1)'' always return 0, can you please tell me how can i convert it from
an integer to float?
def compareValue(n1, n2):
i1 = int(n1)
i2 = int(n2)

dx = abs(i2 - i1)/min(i2, i1)
print dx
return dx < 0.05

On Feb 27, 7:05 pm, "ying...@gmail.com" <ying...@gmail.comwrote:
Hi, I have the following functions, but '' dx = abs(i2 - i1)/min(i2,
i1)'' always return 0, can you please tell me how can i convert it from
an integer to float?

def compareValue(n1, n2):
i1 = int(n1)
i2 = int(n2)

dx = abs(i2 - i1)/min(i2, i1)
print dx
return dx < 0.05

x = x + 0.0


yi*****@gmail.com wrote:
Hi, I have the following functions, but '' dx = abs(i2 - i1)/min(i2,
i1)'' always return 0, can you please tell me how can i convert it from
an integer to float?

When two integers are involved in a division, the result will also be a
division. If one of the operands is a float, then the result will be a
float instead. So try casting one of the values to a float before
performing the division:

dx = float(abs(i2 - i1))/min(i2, i1)

-Farshid


Farshid Lashkari wrote:
When two integers are involved in a division, the result will also be a
division.

My bad, I meant the result will also be an *integer*

-Farshid