It may be me doing something wrong, but I seem to have some trouble calculating bignumbers when using large integers with more than 15 digits (see code fragment below).
I never experienced any issues when using arbitrary precision with decimal places but when it comes to large integers, I somehow run into issues when trying to do regular calculations with numbers that have more than 15 digits. The issues occur across all standard operations such as math.eval, math.pow. math.multiply etc. I suspect that I'm not using the data type 'bignumber' consistently so that variables get converted to regular numbers, but I can't find the reason despite the documentation.
So this may actually not be an issue/bug but rather a question/request for help.
Thank you!
EXAMPLE:
math.config({number: 'BigNumber', precision: 128});
math.format({notation: 'auto', exponential: {lower: 100e-299, upper: 100e+299}});
var a = math.bignumber(131072);
var b = math.bignumber(7718293884959939922);
var c = math.multiply(a, b);
// output a: 131072
// output b: 7718293884959940000
// output c: 1.01165221608946925568e+24
In your example you first parse your values into a JavaScript Number with limited precision, and after that you convert it in a BigNumber. But any precision is already lost when creating it as a Number.
Instead, you should create a bigNumber from a _string_ containing your value, like
var a = math.bignumber('131072');
var b = math.bignumber('7718293884959939922');
Thanks a lot, that seems to work fine for the variables a and b. The output for c, however, is displayed in exponential notation. Is there any way to display c with all its digits?
How you print c?
The way you use math.format in your example is at least not correct (just check the docs)
For the output I use:
document.write(c);
For determining the notation I assume I could use:
math.format(c, {notation: 'fixed'});
According to the documentation, this should work for displaying all digits, but I'm still getting the exponential notation as a result.
yes indeed, that's the right way using format with notation fixed.
Shouldn't you do document.write(math.format(c, {notation: 'fixed'}));? document.write(c) will use the .toString() of c and not use math.format.
Yes, that works perfectly fine! I guess this thread can be closed then. And thanks again for your support, much appreciated...
:+1: