Javascript - 如何平方数字?

时间:2022-08-27 10:50:12

Using the javascript function

使用javascript函数

function squareIt(number) {
   return number * number;
}

When given the number 4294967296 the function returns 18446744073709552000 is returned

当给出数字4294967296时,返回函数返回18446744073709552000

Everyone knows the real answer is 18446744073709551616 :-)

大家都知道真正的答案是18446744073709551616 :-)

I guess this is to to with rounding on my 32-bit machine. However, would this script give the right answer on a 64 bit machine? Has anyone tried this?

我想这是在我的32位机器上进行舍入。但是,这个脚本会在64位机器上给出正确答案吗?有没人试过这个?

3 个解决方案

#1


3  

ChrisV- see this post. Also it easier for people to evaluate your question by typing the following JavaScript directly into the browser URL textbox:

ChrisV-看到这篇文章。此外,通过直接在浏览器URL文本框中键入以下JavaScript,人们可以更轻松地评估您的问题:

javascript:4294967296*4294967296

#2


1  

Javascript uses 64 bit floating point arithmetic internally for numerical calculations - the results you see are a reflection of this, and will happene regardless of the underlying architecture.

Javascript在内部使用64位浮点运算进行数值计算 - 您看到的结果是对此的反映,并且无论底层架构如何都会发生。

#3


1  

what about this

那这个呢

function squareIt(number){
return Math.pow(number,2)
}

#1


3  

ChrisV- see this post. Also it easier for people to evaluate your question by typing the following JavaScript directly into the browser URL textbox:

ChrisV-看到这篇文章。此外,通过直接在浏览器URL文本框中键入以下JavaScript,人们可以更轻松地评估您的问题:

javascript:4294967296*4294967296

#2


1  

Javascript uses 64 bit floating point arithmetic internally for numerical calculations - the results you see are a reflection of this, and will happene regardless of the underlying architecture.

Javascript在内部使用64位浮点运算进行数值计算 - 您看到的结果是对此的反映,并且无论底层架构如何都会发生。

#3


1  

what about this

那这个呢

function squareIt(number){
return Math.pow(number,2)
}