为什么我们有“不是数字” (isNan) 函数?

发布于 2024-09-26 09:46:15 字数 215 浏览 5 评论 0原文

许多语言都有 isNaN() 函数。我问自己:为什么要检查不是一个数字

原因是纯逻辑的还是检查不是数字比检查是数字更快?

注意这是一个纯粹的理解问题。例如,我知道我可以否定 isNaN() 来实现 isNumber() 函数。
然而,我正在寻找一个原因,为什么我们要检查而不是数字

Many languages have an isNaN() function. I am asking myself: why check for not being a number?

Is the reason purely logical or is it faster to check for not a number instead of is a number?

Note that this is a pure question of understanding. I know that I can negate isNaN() to achieve an isNumber() function for example.
However I am searching for a reason WHY we are checking for not a number?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

眼波传意 2024-10-03 09:46:15

在计算中,NaN(非数字)是一个
数值数据类型的值
代表未定义或
无法体现的价值,尤其是在
浮点计算。

Wiki 文章

因为 Not a Number 是表达式的一种特殊情况。

你不能只使用 0 或 -1 或类似的数字,因为这些数字已经有意义。

不是数字意味着计算中出现了错误,无法从中计算出有效的数字。

它与 null 的思路相同。当然,我们可以指定一个任意数值来表示 null,但这会令人困惑,并且我们会在极端情况下遇到各种奇怪的错误。

In computing, NaN (Not a Number) is a
value of numeric data type
representing an undefined or
unrepresentable value, especially in
floating-point calculations.

Wiki Article

Because Not a Number is a special case of an expression.

You can't just use 0 or -1 or something like that because those numbers already have meanings.

Not a Number means something went awry in a calculation and a valid number cannot be computed out of it.

It's on the same line of thinking as having null. Sure, we could assign an arbitrary numerical value to mean null but it would be confusing and we'd hit all sorts of weird errors on corner cases.

草莓酥 2024-10-03 09:46:15

“Not a Number”是特定浮点计算的结果。这不是关于“嘿,这个变量保存的是 120 还是“abc”?

'Not a Number' is the result of specific floating point calculations. It's not about "hey, is this variable holding 120 or "abc"?'

转身以后 2024-10-03 09:46:15

isThisCaseExceptional 对我来说似乎比 isEverythingNormal 更合理,因为我可能会写

possible_number = some_calculation();

if (inNaN(possible_number)) handle_the_surprise;

// .. keep going 

而不是

possible_number = some_calculation();

if (inANumber(possible_number)) {

   // .. keep going 

} else {
   // handle the surprise
}

isThisCaseExceptional seems more reasonable to me than isEverythingNormal because I'm likely to write

possible_number = some_calculation();

if (inNaN(possible_number)) handle_the_surprise;

// .. keep going 

instead of

possible_number = some_calculation();

if (inANumber(possible_number)) {

   // .. keep going 

} else {
   // handle the surprise
}
暖伴 2024-10-03 09:46:15

检查它是否不是数字,因为假设它是数字。当您期望一个数值时,NaN 是一种特殊情况,因此对我来说这样做是有意义的。毕竟,我宁愿很少检查 isNaN 也不愿频繁检查值是否 isNum 。

The check is for whether it is not a number, because the assumption is that it is a number. NaN is the exceptional case when you're expecting a numeric value, so it makes sense to me that it is done this way. I'd rather check for isNaN infrequently than check if a value isNum frequently, after all.

ゃ懵逼小萝莉 2024-10-03 09:46:15

这是一种报告错误情况(数学上未定义或超出技术限制)的方法。

It is a way to report an error condition (mathematically undefined or outside of technical limits).

晨光如昨 2024-10-03 09:46:15

如果计算结果不能用数字表示,许多语言都有特殊的表示形式,例如 NaN 和 Inf。因此 isNaN() 检查结果是否实际上是数字或 NaN 的特殊标记。

Many languages have special representations if the result of a computation is not representable by a number, for example NaN and Inf. So isNaN() checks, if a result is actually a number or the special marker for NaN.

清风夜微凉 2024-10-03 09:46:15

NaN 用于:

  • 非实数或未定义的值。
  • 错误处理。使用 NaN 初始化变量允许您测试 NaN 并确保它已设置为有效值。
  • 忽略成员或属性的对象。例如,WPF 使用 NaN 来表示未定义自身尺寸的视觉元素的宽度和高度。

NaNs are used for:

  • Nonreal or undefined values.
  • Error handling. Initializing a variable with NaN allows you to test for NaN and make sure it has been set with a valid value.
  • Objects that ignore a member or property. For example, WPF uses NaN to represent the Width and Height of visual elements that do not define their own size.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文