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.

3
  • Is floating point math broken? Commented Sep 26, 2017 at 9:13
  • @baao thanks! I understand it is an issue, my problem is I am unable to implement a solution Commented Sep 26, 2017 at 9:14
  • Just posted it because you said it's a bug, which it isn't Commented Sep 26, 2017 at 9:14

1 Answer 1

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>

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

3 Comments

It works here, but when I try that in the console it returns a Big object
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
Because I call console.log which will itself call toString on the Big object.