-1

I did a code in python

total = 1.56 + 6
print("Total:", total)

the result is: Total: 7.5600000000000005

so the result is wrong. while the same calculation run correct in JavaScript

<script>
     total  = 1.56 + 6;
     alert(a);
</script>

but JavaScript is also inaccuracy with the code below

<script>
     a = 7.5600000000000005 * Math.pow(10, 15);
     alert(a);
</script>

any one have a solution for it?

Võ Minh
  • 155
  • 2
  • 12
  • this question gets asked over and over. floating point numbers are not exact due to the way they work – Keith Nicholas Oct 06 '16 at 04:31
  • Some numbers cant be exactly represented with a limited number of bits. That's why there are packages that are able to calculate exactly, especially for computational geometry. However, that's probably no what you want, so dont worry about such discrepancies, as they can happen. – samwise Oct 06 '16 at 04:36
  • I ask for a solution, not why it happen like this. I read through the other question but it's not satisfy. I want to bring it up for some one to fix it. – Võ Minh Oct 06 '16 at 05:00

1 Answers1

1

total  = 1.56 + 6;
console.log(total);
console.log(total*100/100);

Try this, But I don't know why + operator is not working as expected