# Sum number error?

**URL:** <https://discourse.nodered.org/t/sum-number-error/59027>\
**Category:** General\
**Created:** [26 February 2022 16:21 UTC](https://discourse.nodered.org/t/sum-number-error/59027 "2022-02-26T16:21:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Giamma](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@Giamma](https://discourse.nodered.org/u/Giamma)\
**Post date:** [26 February 2022 16:21 UTC](https://discourse.nodered.org/t/sum-number-error/59027/1 "2022-02-26T16:21:57Z")

</div>

This is my simple function ...

```auto
var x = msg.payload;
var temp = x.toFixed(1);
var tempNumber = parseFloat(temp);
var z = (tempNumber + 0.3);
node.status(tempNumber + " - " + z);
global.set("var_temperaturaCamerina", z);
msg.payload = z;
return msg;

```

Look at the node status and the "z" value ...  
Why? Why not 17.9 simply?

![error](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/c/ac9325c6238dead64e06abc305a8d3583521ce8c.png)

---

<div class="post-metadata">

**Author:** ![EmilWesselink](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/emilwesselink/32/65829_2.png) [@EmilWesselink](https://discourse.nodered.org/u/EmilWesselink)\
**Post date:** [26 February 2022 16:32 UTC](https://discourse.nodered.org/t/sum-number-error/59027/2 "2022-02-26T16:32:40Z")

</div>

I tried to reproduce it, but here your code works as you expect....

So I would guess a node or system setting.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/6/66c11e7046a477f741c31d811fc3f4472e8fd0aa.png)

I tried it on windows and linux node red 2.2.

---

<div class="post-metadata">

**Author:** ![Giamma](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@Giamma](https://discourse.nodered.org/u/Giamma)\
**Post date:** [26 February 2022 16:33 UTC](https://discourse.nodered.org/t/sum-number-error/59027/3 "2022-02-26T16:33:34Z")

</div>

Try 17.6 ....

 ![err2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/d/eda82207ba5a88b0d821a1d7f7bb56b7b2cd14a1.png)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [26 February 2022 16:38 UTC](https://discourse.nodered.org/t/sum-number-error/59027/4 "2022-02-26T16:38:44Z")

</div>

Try

```auto
var x = msg.payload;
var tempNumber = Math.round(x*10)/10;
var z = (tempNumber + 0.3);
node.status(tempNumber + " - " + z);
global.set("var_temperaturaCamerina", z);
msg.payload = z;
return msg;

```

or

```auto
var x = msg.payload;
var z = (x + 0.3);
node.status(x.toFixed(1) + " - " + z.toFixed(1));
global.set("var_temperaturaCamerina", z);
msg.payload = z;
return msg;

```

I see the same error with yours if temp is a float. The above codes do not show the error for me.

---

<div class="post-metadata">

**Author:** ![Giamma](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@Giamma](https://discourse.nodered.org/u/Giamma)\
**Post date:** [26 February 2022 16:50 UTC](https://discourse.nodered.org/t/sum-number-error/59027/5 "2022-02-26T16:50:06Z")

</div>

Your 2° example work fine for me .... 👍 (not the 1°)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [26 February 2022 17:03 UTC](https://discourse.nodered.org/t/sum-number-error/59027/6 "2022-02-26T17:03:45Z")

</div>

The reason your original version does not do what you expect is rounding errors in the floating point arithmetic. Any time you do arithmetic there is the possibility of rounding errors. The solution, as you have seen, is to round to the required precision when you want to display it, not before.

---

<div class="post-metadata">

**Author:** ![HaroldPetersInskipp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haroldpetersinskipp/32/42319_2.png) [@HaroldPetersInskipp](https://discourse.nodered.org/u/HaroldPetersInskipp)\
**Post date:** [26 February 2022 17:06 UTC](https://discourse.nodered.org/t/sum-number-error/59027/7 "2022-02-26T17:06:40Z")

</div>

### Why don’t my numbers, like 0.1 + 0.2 add up to a nice round 0.3, and instead I get a weird result like 0.30000000000000004?

Because internally, computers use a format ([binary](https://floating-point-gui.de/formats/binary/) [floating-point](https://floating-point-gui.de/formats/fp/)) that cannot accurately represent a number like 0.1, 0.2 or 0.3 _at all_ .

When the code is compiled or interpreted, your “0.1” is already rounded to the nearest number in that format, which results in a small [rounding error](https://floating-point-gui.de/errors/rounding/) even before the calculation happens.

### Why do computers use such a stupid system?

It’s not stupid, just different. Decimal numbers cannot accurately represent a number like 1/3, so you have to round to something like 0.33 - and you don’t expect 0.33 + 0.33 + 0.33 to add up to 1, either - do you?

Computers use [binary numbers](https://floating-point-gui.de/formats/binary/) because they’re faster at dealing with those, and because for most calculations, a tiny error in the 17th decimal place doesn’t matter at all since the numbers you work with aren’t round (or that precise) anyway.

### What can I do to avoid this problem?

That depends on what kind of calculations you’re doing.

- If you really need your results to add up exactly, especially when you work with money: use a special [decimal datatype](https://floating-point-gui.de/formats/exact/).
- If you just don’t want to see all those extra decimal places: simply format your result rounded to a fixed number of decimal places when displaying it.
- If you have no decimal datatype available, an alternative is to work with [integers](https://floating-point-gui.de/formats/integer/), e.g. do money calculations entirely in cents. But this is more work and has some drawbacks.

### Why do other calculations like 0.1 + 0.4 work correctly?

In that case, the result (0.5) _can_ be represented exactly as a floating-point number, and it’s possible for rounding errors in the input numbers to cancel each other out - But that can’t necessarily be relied upon (e.g. when those two numbers were stored in differently sized floating point representations first, the rounding errors might not offset each other).

In other cases like 0.1 + 0.3, the result actually isn’t _really_ 0.4, but close enough that 0.4 is the shortest number that is closer to the result than to any other floating-point number. Many languages then display that number instead of converting the actual result back to the closest decimal fraction.

---

<div class="post-metadata">

**Author:** ![Giamma](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@Giamma](https://discourse.nodered.org/u/Giamma)\
**Post date:** [26 February 2022 17:14 UTC](https://discourse.nodered.org/t/sum-number-error/59027/8 "2022-02-26T17:14:15Z")

</div>

Ok, thank you for explanation .....

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [12 March 2022 17:14 UTC](https://discourse.nodered.org/t/sum-number-error/59027/9 "2022-03-12T17:14:46Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
