1

How to get numbers toPrecision higher than 21 in javascript?

I am getting this number 8.426661309628124e+22

When it should be this 8.4266613096281243382112

When I use toPrecision(21) I get this 8.42666130962812428616e+22

// my result is a result of this calculation  
 var val=10;
 feb=[0,1];
 for(var i=2; i<=val; i++){
    feb[i]= feb[i-2] + Math.pow(feb[i-1],2);
    }    
    console.log(feb[val-1])
Rick
  • 1,035
  • 10
  • 18
  • 1
    It is implementation dependent: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toPrecision – Hunter McMillen May 22 '17 at 20:19
  • Is there any way around it? – Rick May 22 '17 at 20:20
  • Try using `.toFixed(22)` instead. Keep in mind however that 32-bit floats have an accuracy of around [7 significant digits](https://stackoverflow.com/questions/5098558/float-vs-double-precision) (the rest are likely garbage) – James May 22 '17 at 20:20
  • toFixed is between 0 and 20 – Rick May 22 '17 at 20:23
  • 2
    Do you have a requirement for this level of precision? What problem are you trying to solve? – Hunter McMillen May 22 '17 at 20:26
  • it is a result from this calculation feb[i]= feb[i-2] + Math.pow(feb[i-1],2); It has nothing to do with my method, but the precision involved. – Rick May 22 '17 at 20:28
  • @Hunter McMillen if you think I should include it in the question because it might be relevant, let me know. Maybe I require a second algo to extend the precisison. – Rick May 22 '17 at 20:34
  • 1
    If you need an exact number you should look at javascript libraries that implement BigNumber or some such. Your number is > [MAX_SAFE_INTEGER](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER). – James May 22 '17 at 20:34
  • 1
    Use a [BigInteger library](https://github.com/peterolson/BigInteger.js), or another programming language that can deal with large integers, like Python (see this [example script](https://repl.it/H7ck/2)). – trincot May 22 '17 at 21:34
  • @trincot python is good at handling big numbers but I would prefer a solution in js – Rick May 22 '17 at 21:40

1 Answers1

2

As the precision of JavaScript's internal representation of numbers (double-precision floating point) is limited, you'd need one of the existing Big Integer libraries that overcomes this limitation.

For instance, you could use bignumber.js:

// Configure to (almost) never use scientific notation:
BigNumber.config({ EXPONENTIAL_AT: 1e+9 });
var val=10;
// Store the feb numbers as BigNumber instances:
feb=[new BigNumber(0), new BigNumber(1)];
for(var i=2; i<=val; i++){
    feb[i] = feb[i-2].plus(feb[i-1].toPower(2));
    console.log(feb[i].toString());
}    
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/bignumber.js/4.0.2/bignumber.min.js"></script>

Note that some other languages provide big integers automatically, such as Python:

val=10
feb=[0,1]
for i in range(2, val+1):
    feb.append(feb[i-2] + feb[i-1] ** 2)
    print(feb[i])

See it run on repl.it.

trincot
  • 317,000
  • 35
  • 244
  • 286