区分零和负零 [英] Distinguish zero and negative zero

查看:212
本文介绍了区分零和负零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码中遇到了一个函数返回双精度型的情况,该双精度型可能是零,负零或完全是另一个值。我需要区分零和负零,但默认的双重比较不需要。由于双精度格式,C ++不允许使用按位运算符比较双精度,因此我不确定如何继续。如何区分两者?

I have run into a situation in my code where a function returns a double, and it is possible for this double to be a zero, a negative zero, or another value entirely. I need to distinguish between zero and negative zero, but the default double comparison does not. Due to the format of doubles, C++ does not allow for comparison of doubles using bitwise operators, so I am unsure how to procede. How can I distinguish between the two?

推荐答案

致电 std :: signbit() 来确定符号位的状态。

Call std::signbit() to determine the state of the sign bit.

这篇关于区分零和负零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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