1

I am experiencing the following bug in my JS.

(1.001 * Math.pow(10, 3))

Instead of returning 1001 this returns 1000.99999999. I am trying to eradicate this bug and have been looking at using this big.js library.

I am unsure what to do to fix this issue.

I have tried the following but it doesn't seem to work.

var x = new Big(10);
(1.001 * x.pow(3));

This produces the same bug as without the library.

1 Answers1

3

You have to use Big.js that way, I think you have to forget using conventional operators :

console.log(Big(1.001).times(Big(10).pow(3)))
<script src="https://cdnjs.cloudflare.com/ajax/libs/big.js/3.2.0/big.min.js"></script>
Serge K.
  • 5,303
  • 1
  • 20
  • 27
  • It works here, but when I try that in the console it returns a `Big` object –  Sep 26 '17 at 09:15
  • When I use `toPrecision` it works. however, when ive just put your answer into my code and I get returned a Big object. why am i seeing a diff value to you do you know? you get the right value but do not use `toPrecision` –  Sep 26 '17 at 09:29
  • Because I call `console.log` which will itself call `toString` on the `Big` object. – Serge K. Sep 26 '17 at 09:37