开发者

python floating point nature and converting to a smaller type

I am trying to understand the exact nature of float when it comes to python. Apparently, python stores f开发者_高级运维loats in a PyFloatObject type which contains a double. However, how can I force it to behave like a 32 bit floating point variable ? Is there another type for this ?


The built-in float type of Python always uses double precision (unless you compile a custom version of the interpreter). You can use the array module to get 32-bit floats:

a = array.array("f")

will define an array of 32-bit floats.

The external NumPy package also provides a scalar 32-bit floating-point type, namely numpy.float32.


There isn't a built-in type in Python that's represented using 32-bit floats.

However, see Sven Marnach's answer for alternatives.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜