How can you distinguish positive zero from negative zero?

signbit reads the floating sign bit, distinguishing signed zeros that compare equal in ordinary equality tests.

numpy.signbit in practice

A device can emit -0.0 and +0.0, producing true and false sign bits. Consult the device protocol before interpreting either as a physical direction.

import numpy as np
np.signbit([-0.0,0.0,-2.0])

Result [true, false, true]

中文

如何辨别浮点的正零和负零?

signbit 读取符号位,因此能区分普通比较下相等的两个带符号零。

numpy.signbit 算例

设备可能输出 -0.0 与 +0.0,符号位分别为真和假。将其解释为实际运动方向之前仍需了解传感器协议。

import numpy as np
np.signbit([-0.0,0.0,-2.0])

结果 [true, false, true]

References / 参考来源

Keep reading / 继续阅读