为什么-0存在? [英] Why does -0 exist?
问题描述
尝试使用JavaScript时。我正在测试一些奇怪的小代码片段,这里有一些我的发现(为了帮助我理解我的生活方式 -0
),
While experimenting with JavaScript. I was testing around with some odd little code snippets, here are a few of my findings (to help understand how I came upon -0
),
在控制台中执行 + []
时,返回 0
。我不确定为什么,但确实如此。
所以,这意味着数组的正数是 0
..
While doing +[]
in console, this returns 0
. I'm not sure why, but it does.
So, this implies the positive of a array is 0
..
这样做后,我好奇并尝试了以下内容:
After doing so, I got curious and tried the following:
console.log(-[]);
这会返回 -0
...
-0是什么意思? 0和-0都没有值,所以负面实际上是不必要的......或者是它?也许JavaScript的目的是-0?
What is the point of -0? 0 and -0 both hold no value, so the negative is really unnecessary... Or is it? Perhaps JavaScript has a purpose for -0?
添加到它上面。除了使用 -0
本身或使用 -0
> - [] ...
Adding onto that. I cannot find any other way to reproduce -0
other than using -0
itself, or using -[]
...
其他一些奇怪的发现,继续我的问题 -0
Some other weird findings, furthing my question of -0
(-0) + (-0) = 0
(-0) - (-0) = -0
(-0) * (-0) = 0
(-0) / (-0) = NaN // of course
推荐答案
JavaScript使用 IEEE 754标准代表数字。来自维基百科:
JavaScript uses IEEE 754 standard to represent numbers. From Wikipedia:
有符号零为零且带有相关符号。在普通算术中,-0 = + 0 = 0.但是,在计算中,某些数字表示允许存在两个零,通常用 -0(负零)和 +0表示(正零)。这发生在整数的一些有符号数表示中,并且在大多数浮点数表示中。数字0通常编码为+0,但可以用+0或-0表示。
Signed zero is zero with an associated sign. In ordinary arithmetic, −0 = +0 = 0. However, in computing, some number representations allow for the existence of two zeros, often denoted by −0 (negative zero) and +0 (positive zero). This occurs in some signed number representations for integers, and in most floating point number representations. The number 0 is usually encoded as +0, but can be represented by either +0 or −0.
用于浮点运算的IEEE 754标准(目前大多数都使用)支持浮点数的计算机和编程语言需要+0和-0。零可以被认为是扩展实数线的变体,使得1 / -0 =-∞和1 / + 0 = +∞,除以0仅对于±0 /±0和±∞/±∞未定义。
The IEEE 754 standard for floating point arithmetic (presently used by most computers and programming languages that support floating point numbers) requires both +0 and −0. The zeroes can be considered as a variant of the extended real number line such that 1/−0 = −∞ and 1/+0 = +∞, division by zero is only undefined for ±0/±0 and ±∞/±∞.
从这个发布
这篇关于为什么-0存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!