【发布时间】:2020-07-30 10:49:00
【问题描述】:
请帮我理解这段代码sn-p:
def binary_float_to_int(float_number: float) -> int:
return ctypes.c_uint.from_buffer(ctypes.c_float(float_number)).value
这些输入的结果:
print(binary_float_to_int(7.1746481e-43))
print(binary_float_to_int(5.3809861e-43))
是:512 & 384
为什么简单的 Python 转换 int(7.1746481e-43) 不起作用?
还有其他方法可以进行这种类型的转换吗?
【问题讨论】:
-
int(7.1746481e-43)据我所知工作正常。int()向零舍入,7.1746481e-43小于 1。我不确定binary_float_to_int()在做什么,但7.1746481e-43与512相差甚远。 -
@brunns 转换为表示该浮点数的 32 位 IEEE 754 浮点值的 32 位整数值。
标签: python floating-point type-conversion ctypes