int a; ...; (a == a)失败? [英] int a; ...; (a == a) fails?

查看:121
本文介绍了int a; ...; (a == a)失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们将 float double 键入 NaN ,则它们不等于自己

这样的事情发生在 int

解决方案

不正常,C ++允许 int NaN



首先, int 可以存储除了其数值之外的信息。 §3.9.1/ 1:


对于字符类型,对象表示的所有位都参与值表示。对于无符号字符类型,值表示的所有可能的位模式表示数字。 这些要求不适用于其他类型。


然后, numeric_limits< int> ; ::对于任何类型,:: has_quiet_NaN has_signaling_NaN 不需要为 false §18.2.1.2/ 34-36:


  static const bool has_quiet_NaN; 

34如果类型有
表示静默



35对所有浮点
类型都有意义。



36对于所有
专业,对is_iec559!=
false都是如此。


对于所有浮点类型是有点麻烦,因为它打开的值可能是无意义的,但这当然是外推。



如果 numeric_limits< int> :: has_quiet_NaN == true ,则 numeric_limits< int> :: quiet_NaN()返回这样的值。

显然,这不是你应该在你的代码中担心的。


if we set float and double type to NaN then they are not equal to anything including themselves.
can such a thing happens for int?

解决方案

Although it's certainly unusual, C++ does allow int to be NaN.

First, it's possible for int to store information besides its numeric value. §3.9.1/1:

For character types, all bits of the object representation participate in the value representation. For unsigned character types, all possible bit patterns of the value representation represent numbers. These requirements do not hold for other types.

Then, numeric_limits<int>::has_quiet_NaN and has_signaling_NaN are not required to be false for any type. §18.2.1.2/34-36:

static const bool has_quiet_NaN;

34 True if the type has a representation for a quiet (non-signaling) ‘‘Not a Number.’’

35 Meaningful for all floating point types.

36 Shall be true for all specializations in which is_iec559 != false.

"Meaningful for all floating point types" is a little troublesome as it opens the possibility that the value could be meaningless, but that's certainly an extrapolation.

If numeric_limits<int>::has_quiet_NaN == true, then numeric_limits<int>::quiet_NaN() returns such a value.

Obviously, this is not something you should worry about in your code.

这篇关于int a; ...; (a == a)失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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