2

I am experimenting with a JavaScript library for working with big integers called BigInteger.js.

Specifically, I am trying to print a big integer. However, I am getting puzzling results. For instance, when I try this:

var x= bigInt("53542885039854749852");
document.write(x+"<br>");

...I am getting 53542885039854750000. And when I try this:

var x= bigInt("104156103156113102156118165104101120101");
document.write(x+"<br>");

...the result is 1.041561031561131e+38.

Can someone help me understand why I am not getting the results I expect?

0

1 Answer 1

7

You could use the toString() method to get a string with all digits.

Note that arithmetical operators will trigger the valueOf function rather than the toString function. When converting a bigInteger to a string, you should use the toString method or the String function instead of adding the empty string.

  • bigInt("999999999999999999").toString() => "999999999999999999"
  • String(bigInt("999999999999999999")) => "999999999999999999"
  • bigInt("999999999999999999") + "" => 1000000000000000000

var x= bigInt("53542885039854749852");
document.write(x + "<br>");             // this calls valueOf
document.write(x.valueOf() + "<br>");
document.write(x.toString() + "<br>");
<script src="http://peterolson.github.com/BigInteger.js/BigInteger.min.js"></script>

To calculate something, you could use the methods of the class.

   
var y = bigInt("104156103156113102156118165104101120101");
document.write(y.plus(1).toString() + "<br>");
document.write(y.multiply(2).toString() + "<br>");
document.write(y.plus(y).toString() + "<br>");
<script src="http://peterolson.github.com/BigInteger.js/BigInteger.min.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.