首页 > 网站 > WEB开发 > 正文

JavaScript中的两个“0”(翻译)

2024-04-27 14:21:05
字体:
来源:转载
供稿:网友

javaScript中的两个“0”(翻译

本文翻译自Javascript’s two zerosJavaScript has two zeros: −0 and +0. This post explains why that is and where it matters in PRactice.JavaScript 中有两个“0”: -0 和 +0 。这篇文章解释了为什么,并且指出实际生产中会造成什么影响1.The signed zero1.“-0”Numbers always need to be encoded to be stored digitally. Why do some encodings have two zeros? As an example, let’s look at encoding integers as 4-digit binary numbers, via the sign-and-magnitude method. There, one uses one bit for the sign (0 if positive, 1 if negative) and the remaining bits for the magnitude (absolute value). Therefore, −2 and +2 are encoded as follows.Binary 1010 is decimal −2Binary 0010 is decimal +2Naturally, that means that there will be two zeros: 1000 (−0) and 0000 (+0).为了储存数字,需要将其编码为二级制,但为什么会编码出两个“0”呢,举个例子,将整数编码为4位二进制,由于整数有正有负,通过符号数值表示法,用第一位来表示符号(0 表示正数,1 表示负数),剩下的位表示数值(数的绝对值)。所以,-2 和 +2 被编码为下面的形式:二进制 1010 代表 -2二进制 0010 代表 +2很自然的,对于“2”也将出现两个:1000(-0) 和 0000(+0)In JavaScript, all numbers are floating point numbers, encoded in double precision according to the IEEE 754 standard for floating point arithmetic. That standard handles the sign in a manner similar to sign-and-magnitude encoding for integers and therefore also has a signed zero. Whenever you represent a number digitally, it can become so small that it is indistinguishable from 0, because the encoding is not precise enough to represent the difference. Then a signed zero allows you to record “from which direction” you approached zero, what sign the number had before it was considered zero. Wikipedia nicely sums up the pros and cons of signed zeros:在JavaScript中,所有的数字都被存储为浮点型数字,根据IEEE 754 标准的浮点数算法编码为双精度浮点数。该标准类似于用符号数值表示法来编码整数,所以也会出现“-0”。当你要表示一个数字时,他可以表示一个小到与“0”区别不出来的数,因为编码方式无法足够精确的表示这种区别。用“-0”便可以记录一个数字在被认为是“0”之前,是“从坐标轴的那个方向”趋近真正的“0”的。关于“-0”的优劣,维基百科做了很好的总结。

引用It is claimed that the inclusion of signed zero in IEEE 754 makes it much easier to achieve numerical accuracy in some critical problems, in particular when computing with complex elementary functions. On the other hand, the concept of signed zero runs contrary to the general assumption made in most mathematical fields (and in most mathematics courses) that negative zero is the same thing as zero. Representations that allow negative zero can be a source of errors in programs, as software developers do not realize (or may forget) that, while the two zero representations behave as equal under numeric comparisons, they are different bit patterns and yield different results in some Operations.

引用在 IEEE 754 标准中使用“-0”,更容易解决复杂数学计算中的精度等关键问题。另一方面,“-0”的概念与大多数数学领域(和数学课程)中的数学假设背道而驰,因为“-0”和“+0”是相同的。允许“-0”的存在,开发人员可能会没有认识到(或忘了)这一点,由于两个“0”的二进制表示不同,“-0”的存在,在某些计算中会产生不同的结果,导致判断两个数字相等的代码可能隐含一些错误。此段维基百科引用的英文没有对应的中文版,所以自己做了翻译。

JavaScript goes to some lengths to hide the fact that there are two zeros.JavaScript 做了很多工作来隐藏有两个“0”的事实。2.Hiding the zero’s sign2.隐藏“0”的符号In JavaScript, you’ll usually write 0, which always means +0. But it also displays −0 simply as 0. The following is what you see when you use any browser command line or the Node.js REPL:通常认为,JavaScript 中显示的“0”,表示的都是“+0”。其实“-0”也直接显示为“0”,下面的例子显示了浏览器命令行和Node.js中的执行情况:

Js代码收藏代码
  1. >-0
  2. 0

The reason is that the standard toString() method converts both zeros to the same "0".原因是按照规则两个“0”都通过调用 toString() 方法转换成了相同的结果“0”:

Js代码收藏代码
  1. >(-0).toString()
  2. '0'
  3. >(+0).toString()
  4. '0'

The illusion of a single zero is also perpetrated by the equals operators. Even strict equality considers both zeros the same, making it very hard to tell them apart (in the rare case that you want to).等于“==”操作符同样这样对待“-0”,甚至全等符号“===”也判断为他们相等,这使他们很难被区别(但在某些情况下需要区分)。

Js代码收藏代码
  1. >+0===-0
  2. true

The same holds for the less-than and greater-than operators – they consider both zeros equal.大于“>”小于“<”符号同样判断两个“0”相等。

Js代码收藏代码
  1. >-0<+0
  2. false
  3. >+0<-0
  4. false

3.Where the zero’s sign matters3.“0”的符号都影响了哪些地方The sign of the 0 rarely influences results of computations. And +0 is the most common 0. Only a few operations produce −0, most of them just pass an existing −0 through. This section shows a few examples where the sign matters. For each example, think about whether it could be used to tell −0 and +0 apart, a task that we will tackle in Sect. 4. In order to make the sign of a zero visible, we use the following function.“-0”在一些很罕见的地方影响计算结果。通常“+0”就是通常的“0”。只有少数运算产生“-0”的结果,大多数都直接忽略“-0”的存在。这一段将展示“-0”在哪些情况下产生影响。想一想下面每一个例子要区别“-0”和“+0”的原因,在展示过程中,为了能清晰的看到“-0”,我们将使用下面这个函数。

Js代码
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表