Python 浮点数与 numpy.float32 [英] Python float vs numpy.float32

查看:84
本文介绍了Python 浮点数与 numpy.float32的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 numpy.float32.

t = numpy.float32(.3)
x = numpy.float32(1)
r = numpy.float32(-.3)
_t = t+x+r
_t == 1 # -> False

使用常规 Python float.

Using regular Python float.

t = .3
x = 1
r = -.3
_t = t+x+r
_t == 1 # -> True

为什么?

推荐答案

Python float 是 C double 类型:文档:

Python float is a C double type: documentation:

浮点数在C中通常使用double来实现;sys.float_info 中提供了有关运行程序的机器的浮点数的精度和内部表示的信息.

Floating point numbers are usually implemented using double in C; information about the precision and internal representation of floating point numbers for the machine on which your program is running is available in sys.float_info.

因此,您要比较 32 和 64 精度浮点数.以下将起作用:

Therefore, you are comparing 32 and 64 precision floating point numbers. The following will work:

t = numpy.float64(.3)
x = numpy.float64(1)
r = numpy.float64(-.3)
_t = t+x+r
_t == 1

这篇关于Python 浮点数与 numpy.float32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆