var1=anyInteger
var2=anyInteger
(Math.round(var1/var2)*var2)
Run Code Online (Sandbox Code Playgroud)
对于上面的JavaScripts bitshift替代方法的语法是什么?
使用整数不浮动
谢谢
[更新]快速回答:
var intResult = ((((var1 / var2) + 0.5) << 1) >> 1) * var2;
它比Math.round()问题中提供的方法更快,并提供完全相同的值.
我的测试比特移位快10到20%.下面是一些比较两种方法的更新代码.
下面的代码有四个部分:首先,它创建10,000组两个随机整数; 第二,它在OP的问题中进行回合,存储该值以供以后比较并记录执行的总时间; 第三,它执行等效的位移,存储该值以供以后比较,并记录执行时间; 第四,它比较Round和Bit-shift值以找出任何差异.它应该报告没有异常.
请注意,这应适用于所有正,非零值.如果代码遇到分母的零,它将会引发错误,并且我很确定负值不会正确地进行位移,尽管我没有测试过.
var arr1 = [],
arr2 = [],
arrFloorValues = [],
arrShiftValues = [],
intFloorTime = 0,
intShiftTime = 0,
mathround = Math.round, // @trinithis's excellent suggestion
i;
// Step one: create random values to compare
for (i = 0; i < 100000; i++) {
arr1.push(Math.round(Math.random() * 1000) + 1);
arr2.push(Math.round(Math.random() * 1000) + 1);
}
// Step two: test speed of Math.round()
var intStartTime = new Date().getTime();
for (i = 0; i < arr1.length; i++) {
arrFloorValues.push(mathround(arr1[i] / arr2[i]) * arr2[i]);
}
console.log("Math.floor(): " + (new Date().getTime() - intStartTime));
// Step three: test speed of bit shift
var intStartTime = new Date().getTime();
for (i = 0; i < arr1.length; i++) {
arrShiftValues.push( ( ( ( (arr1[i] / arr2[i]) + 0.5) << 1 ) >> 1 ) * arr2[i]);
}
console.log("Shifting: " + (new Date().getTime() - intStartTime));
// Step four: confirm that Math.round() and bit-shift produce same values
intMaxAsserts = 100;
for (i = 0; i < arr1.length; i++) {
if (arrShiftValues[i] !== arrFloorValues[i]) {
console.log("failed on",arr1[i],arr2[i],arrFloorValues[i],arrShiftValues[i])
if (intMaxAsserts-- < 0) break;
}
}
Run Code Online (Sandbox Code Playgroud)
您应该能够通过添加0.5然后移除小数来舍入任何数字...
var anyNum = 3.14;
var rounded = (anyNum + 0.5) | 0;
Run Code Online (Sandbox Code Playgroud)
所以可以用这个来解决原始表达式(而不是较慢的Math.round)
((var1/var2 + 0.5)|0) * var2
运行下面的代码片段以测试不同的值...
function updateAnswer() {
var A = document.getElementById('a').value;
var B = document.getElementById('b').value;
var h = "Math.round(A/B) * B = <b>" + (Math.round(A/B) * B) + "</b>";
h += "<br/>";
h += "((A/B + 0.5)|0) * B = <b>" + ((A/B + 0.5) | 0) * B +"</b>";
document.getElementById('ans').innerHTML = h;
}Run Code Online (Sandbox Code Playgroud)
*{font-family:courier}Run Code Online (Sandbox Code Playgroud)
A: <input id="a" value="42" />
<br/>
B: <input id="b" value="7" />
<br/><br/>
<button onclick="updateAnswer()">go</button>
<hr/>
<span id="ans"></span>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6035 次 |
| 最近记录: |