4

Using the javascript function

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

When given the number 4294967296 the function returns 18446744073709552000 is returned

Everyone knows the real answer is 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?

Dalorzo
  • 19,834
  • 7
  • 55
  • 102
ChrisV
  • 2,213
  • 1
  • 18
  • 30

4 Answers4

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:

javascript:4294967296*4294967296
Community
  • 1
  • 1
RichardOD
  • 28,883
  • 9
  • 61
  • 81
2

what about this

function squareIt(number){
return Math.pow(number,2)
}
Kostas Konstantinidis
  • 13,347
  • 10
  • 48
  • 61
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.

pauljwilliams
  • 19,079
  • 3
  • 51
  • 79
0

Here is one more example based on BigInteger.js.

alert(bigInt(4294967296).square());
<script src="https://cdnjs.cloudflare.com/ajax/libs/big-integer/1.6.40/BigInteger.min.js"></script>
Penny Liu
  • 15,447
  • 5
  • 79
  • 98