Why is adding two values to decimals not an exact result?

Asked

Viewed 84 times

0

I’m trying to sum two values with decimals of two variables:

valor1 = 10.00;
valor2 = 10.99;
console.log(valor1+valor2);

When adding the two values is returned 20.990000000000002 and not 20.99.

When I add with the values with decimals other than zero, the result is exact, for example:

valor1 = 10.03;
valor2 = 10.81;
console.log(valor1+valor2);

Why the result of the first example is not exact?

  • 1

    Start by reading that answer from Jefferson. But trying to summarize as much as possible, some numbers are not representable in binary, such as 0.1, thus becoming infinite and not giving the intended result. Also read this article (in English). I’m pretty sure this question is duplicated from another, but I didn’t find

  • For the general case, the answer @Isac indicated is the most appropriate one. For the specific case, it would be necessary to take the binary-based mantissa representation in order to arrive at some conclusion. By the way, every sum with integer values (or ending in .00) small is exact in floating points =] Its summed values make me believe that the incident error of the second is more than 7 houses away, therefore the formatter considers insignificant to show. That is, I believe that it remains "not perfect", but that the imperfection is very small

  • 1

    @Jeffersonquesado I went to give a read today on the subject and managed to understand the issue of floating point. It really makes sense.

  • 1

    @dvd particularly didn’t think any of the answers already given here were good. I think it is the case that you put the result of your research (or if it is the case, if the questions that address the floating point subject are in essence the same as yours, mark as duplicate)

  • 1

    @Jeffersonquesado I agree with you. From what I researched, one of the answers gave a "pinch" but did not make a deeper approach. I’ll see if I can come up with a more complete answer.

  • 1

    @Jeffersonquesado If you want to answer too, feel free.

Show 1 more comment

3 answers

1


It is because there is no way to represent the 20.99 exactly with a floating point number. Not only does javascript have this side effect, all languages that use some kind of floating point suffer from the same problem.

You can, in this specific case, use the method toFixed to show only the 2 desired houses:

valor1 = 10.00;
valor2 = 10.99;
console.log((valor1+valor2).toFixed(2));

0

Try using the toFixed():

<script>
  var valor1 = 10.00;
  var valor2 = 10.99;
  var soma = ( valor1 + valor2 );
  console.log(soma.toFixed(2));
</script>

Reference: https://www.w3schools.com/jsref/jsref_tofixed.asp

var valor1 = 10.00;
var valor2 = 10.99;
var soma = ( valor1 + valor2 );
console.log(soma.toFixed(2));

0

I think it’s better to do roundabout, tofixed may not give the expected value.

function myRound(val, dec) {
    return (Math.round(val*Math.pow(10,dec))/Math.pow(10,dec)).toFixed(dec);
}

var valor1 = 10.00;
var valor2 = 10.99;
var soma = ( valor1 + valor2 );
soma = myRound(soma, 2);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.