通过商索引数组
Indexing an array by a quotient
在下面的代码中,i
总是一个偶数,所以商 i / 2
应该总是一个整数。为了安全起见,我还是应该使用 Math.floor(i / 2)
吗?我问是因为 JavaScript 确实将所有数字都视为浮点数,所以我担心舍入误差。
for (var i = 0; i < data.length; i = i + 2) {
var name = names[i / 2];
...
}
没有。在这种情况下您不必使用 Math.floor()
。
因为 i
总是偶数而且 names[1.00]
等价于 names[1]
.
要检查,请在 javascript 控制台中尝试以下操作。
数组的长度为 20,将打印前 10 个数组项
var names = ["nums1", "nums2", "nums3","nums4", "nums5", "nums6","nums7",
"nums8", "nums9","nums10", "nums11","nums12", "nums13","nums14", "nums15",
"nums16","nums17", "nums18", "nums19","nums20"];
for (var i = 0; i < names.length; i = i + 2) {
console.log(names[i/2]);
}
偶数除以 2 总是 returns 整数只要不溢出
Number.MAX_SAFE_INTEGER
在那之后,整数以 2 的幂存储,因此它们失去了精度。
然而,大于这个大小的数组在访问它们的元素时会遇到问题(因为索引变得不精确),你可以放入数组的最大元素数被限制为这个最大安全整数。所以基本上你的代码将始终有效,如果它不起作用,那是因为数组溢出而不是因为索引错误。但是,我更推荐你这样做:
var array = ["one", "two", "three","four", "five", "six","seven",
"eight", "nine","ten", "eleven","twelve", "thirteen","fourteen", "fifteen",
"sixteen","seventeen", "eighteen", "nineteen","twenty"];
for(var i=0,l=array.length/2;i<l;i++){
console.log(array[i]);
}
因为它省去了这些不必要的数学运算...
根据JavaScript(ECMAScript®) Language Specification 如果
i <= Number.MAX_SAFE_INTEGER + 1
那么 i
就可以准确表示了,除法应该没问题。
The value of Number.MAX_SAFE_INTEGER
is the largest integer n such that n and n + 1 are both exactly representable as a Number value.
The value of Number.MAX_SAFE_INTEGER
is 9007199254740991 (253−1).
Section 12.7.3.2 Applying the / Operator
In the remaining cases, where neither an infinity, nor a zero, nor NaN is involved, the quotient is computed and rounded to the nearest representable value using IEEE 754-2008 round to nearest, ties to even mode. If the magnitude is too large to represent, the operation overflows; the result is then an infinity of appropriate sign. If the magnitude is too small to represent, the operation underflows and the result is a zero of the appropriate sign. The ECMAScript language requires support of gradual underflow as defined by IEEE 754-2008.
在下面的代码中,i
总是一个偶数,所以商 i / 2
应该总是一个整数。为了安全起见,我还是应该使用 Math.floor(i / 2)
吗?我问是因为 JavaScript 确实将所有数字都视为浮点数,所以我担心舍入误差。
for (var i = 0; i < data.length; i = i + 2) {
var name = names[i / 2];
...
}
没有。在这种情况下您不必使用 Math.floor()
。
因为 i
总是偶数而且 names[1.00]
等价于 names[1]
.
要检查,请在 javascript 控制台中尝试以下操作。
数组的长度为 20,将打印前 10 个数组项
var names = ["nums1", "nums2", "nums3","nums4", "nums5", "nums6","nums7",
"nums8", "nums9","nums10", "nums11","nums12", "nums13","nums14", "nums15",
"nums16","nums17", "nums18", "nums19","nums20"];
for (var i = 0; i < names.length; i = i + 2) {
console.log(names[i/2]);
}
偶数除以 2 总是 returns 整数只要不溢出
Number.MAX_SAFE_INTEGER
在那之后,整数以 2 的幂存储,因此它们失去了精度。
然而,大于这个大小的数组在访问它们的元素时会遇到问题(因为索引变得不精确),你可以放入数组的最大元素数被限制为这个最大安全整数。所以基本上你的代码将始终有效,如果它不起作用,那是因为数组溢出而不是因为索引错误。但是,我更推荐你这样做:
var array = ["one", "two", "three","four", "five", "six","seven",
"eight", "nine","ten", "eleven","twelve", "thirteen","fourteen", "fifteen",
"sixteen","seventeen", "eighteen", "nineteen","twenty"];
for(var i=0,l=array.length/2;i<l;i++){
console.log(array[i]);
}
因为它省去了这些不必要的数学运算...
根据JavaScript(ECMAScript®) Language Specification 如果
i <= Number.MAX_SAFE_INTEGER + 1
那么 i
就可以准确表示了,除法应该没问题。
The value of
Number.MAX_SAFE_INTEGER
is the largest integer n such that n and n + 1 are both exactly representable as a Number value.The value of
Number.MAX_SAFE_INTEGER
is 9007199254740991 (253−1).
Section 12.7.3.2 Applying the / Operator
In the remaining cases, where neither an infinity, nor a zero, nor NaN is involved, the quotient is computed and rounded to the nearest representable value using IEEE 754-2008 round to nearest, ties to even mode. If the magnitude is too large to represent, the operation overflows; the result is then an infinity of appropriate sign. If the magnitude is too small to represent, the operation underflows and the result is a zero of the appropriate sign. The ECMAScript language requires support of gradual underflow as defined by IEEE 754-2008.