等于运算符==可以用来做位检查吗?
该函数的目的是什么?
bool whatIsIt(double n)
{
return n == n;
}
它可用于检查 n 中的每一位?
我对此表示怀疑。
如有任何意见,我们将不胜感激。
What is the purpose of the function ?
bool whatIsIt(double n)
{
return n == n;
}
It can be used to check every bit in n ?
I doubt that .
Any comments are appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它可用于检查 n 是否为 NaN(不是数字),因为 NaN 不与其自身进行比较。
可能是一种挑剔且不完全可靠的方法。 (参见 Billy 的各种评论)C99 和 C++11 有
isnan()
函数。It could be used to check if n is NaN (not a number), since NaN does not compare equal to itself.
Probably a finnicky and not entirely reliable way to do it. (see Billy's various comments) C99 and C++11 have the
isnan()
function.这是在 C 标准附录 F:60559 浮点算术中指定的,特别是 F.8.3 关系运算符:
如果
__STDC_IEC_559__
为#define
,则此函数将返回 false如果n
是NaN
。This is specified in the C Standard in Annex F: 60559 Floating Point Arithmetic, specifically F.8.3 Relational operators:
If
__STDC_IEC_559__
is#defined
, then this function will return false ifn
isNaN
.它可能是为了检测 NaN(它们永远不等于任何东西,甚至彼此不相等)——尽管这取决于您特定的编译器/平台/设置/等。严格来说,该标准没有说明如何处理浮点数学。
It's probably there to detect NaNs (which are never equal to anything, even each other) -- though that's going to depend on your particular compiler/platform/settings/etc. The standard, strictly speaking, does not say how floating point math is handled.
不,这不是“检查位”。
我猜测它正在检查“NaN”。如果输入为 NaN,它将返回 FALSE,如果输入是任何其他浮点值,则返回 TRUE。
No, it's not "checking bits".
I'm guessing that it IS checking for "NaN'. It will return FALSE if the input is NaN, and return TRUE if it's any other floating point value.
该函数检查数字是否具有可比性。
这对于用作排序函数的键或在搜索中使用的值非常重要。排序中使用的比较期望如果 A < B 为真,则 B < A 将为假。如果 A 或 B 是不可比较的值,那么这两个陈述都是错误的。
从技术上讲,排序所需的称为严格弱排序。
无法在集合中找到具有不可比较值的元素。包含不可比较值的列表是不可排序的。此外,优化的实现可能会退出正在排序的数组并开始破坏内存,或者可能永远不会终止。
我所知道的 double 唯一不可比较的值是 NaN。正如其他人指出的那样,如果用作
whatIsIt()
的参数,NaN 将返回 false。如果 NaN 是您比较的数字的可能值,那么您必须处理它,否则可能会发生不好的事情。这篇维基百科文章中提到了 std::map 和 NaN 的问题。
您可以构造一个比较来将 NaN 排序到列表中的给定位置,但不能仅使用内置运算符来完成此操作。相反,你会做类似的事情
顺便说一句,在 SQL 中,NULL 也不能在兼容的实现中进行比较。
其中的奥秘是为什么不使用 isnan() ,除非这是一个面试问题或其他问题。
That function checks that a number is comparable.
This can be very important for values used as a key to a sort function or used in a search. The compare used in a sort expects that if A < B is true, then B < A will be false. If A or B were an uncomparable value, both those statements would be false.
Technically what sort requires is called strict weak ordering.
An element in a collection that has an uncomparable value can't be found. A list that contains an uncomparable value is unsortable. Further, an optimized implementation may walk out of the array being sorted and start corrupting memory or may never terminate.
The only non-comparable value I know of for a double is NaN. As others have pointed out, NaN will return false if used as a parameter to
whatIsIt()
. If NaN is a possible value for numbers you compare, then you have to handle it or bad things may happen.Issues with std::map and NaN are mentioned in this wikipedia article.
You can construct a compare to sort NaN to a given spot in a list, but you can't do it with just the built in operators. Instead you'd do something like
As an aside, in SQL, NULL also can't be compared in compliant implementations.
The mystery in this is why isnan() wasn't used unless this was an interview question or something.