等于运算符==可以用来做位检查吗?

发布于 2024-12-10 11:03:41 字数 158 浏览 0 评论 0原文

该函数的目的是什么?

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

时常饿 2024-12-17 11:03:41

它可用于检查 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.

剩余の解释 2024-12-17 11:03:41

这是在 C 标准附录 F:60559 浮点算术中指定的,特别是 F.8.3 关系运算符:

... 如果 xNaN,则语句 x != x 为 true

...如果 xNaN,则语句 x == x 为 false。

如果 __STDC_IEC_559__#define,则此函数将返回 false如果nNaN

This is specified in the C Standard in Annex F: 60559 Floating Point Arithmetic, specifically F.8.3 Relational operators:

... The statement x != x is true if x is a NaN

... The statement x == x is false if x is a NaN

If __STDC_IEC_559__ is #defined, then this function will return false if n is NaN.

紅太極 2024-12-17 11:03:41

它可能是为了检测 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.

梦在夏天 2024-12-17 11:03:41

不,这不是“检查位”。

我猜测它正在检查“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.

╰つ倒转 2024-12-17 11:03:41

该函数检查数字是否具有可比性。

这对于用作排序函数的键或在搜索中使用的值非常重要。排序中使用的比较期望如果 A < B 为真,则 B < A 将为假。如果 A 或 B 是不可比较的值,那么这两个陈述都是错误的。

从技术上讲,排序所需的称为严格弱排序

无法在集合中找到具有不可比较值的元素。包含不可比较值的列表是不可排序的。此外,优化的实现可能会退出正在排序的数组并开始破坏内存,或者可能永远不会终止。

我所知道的 double 唯一不可比较的值是 NaN。正如其他人指出的那样,如果用作 whatIsIt() 的参数,NaN 将返回 false。如果 NaN 是您比较的数字的可能值,那么您必须处理它,否则可能会发生不好的事情。

这篇维基百科文章中提到了 std::map 和 NaN 的问题。

您可以构造一个比较来将 NaN 排序到列表中的给定位置,但不能仅使用内置运算符来完成此操作。相反,你会做类似的事情

if ( A < B ) then return -1;
else if ( B < A ) then return 1;
else return whatIsIt(A) - whatIsIt(B);

顺便说一句,在 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

if ( A < B ) then return -1;
else if ( B < A ) then return 1;
else return whatIsIt(A) - whatIsIt(B);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文