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

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

问题描述

我遇到了一个情况在我的代码中,一个函数返回一个双精度,这是可能的双精度零,负零,或另一个值完全。我需要区分零和负零,但是默认的双重比较没有。由于double的格式,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.

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

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