如何在float32而不是float64上强制执行python float操作

Sam*_*uel 4 python floating-point precision numpy operators

我想maths float operation(+,-,x,/)在float32而不是float64上做一些.我需要在number或上做这些操作numpy.array.还有一些numpy的数学函数,比如sqrt mean

WKP*_*lus 10

numpy.float32会有帮助吗?

>>>PI=3.1415926535897
>>> print PI*PI
9.86960440109
>>> PI32=numpy.float32(PI)
>>> print PI32*PI32
9.86961
Run Code Online (Sandbox Code Playgroud)

如果你想在float32上进行数学运算,将操作数转换为float32可能对你有帮助.

  • 是的,当使用数组时,如果它不是 float32,你可以调用它的 astype 方法:http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.astype.html (2认同)