Float Value appends data to the end

I have a brand new Memory Tag that is attached to nothing. I have manually typed a Float Value of 8.34 into the Value field and it keeps changing to 8.340000153. Additionally, I created a second tag with an entered value of 12.8 & it updates to 12.800000191. Does anyone have an idea what could cause this? As I mentioned it’s attached to no other tags & its a test tag I created just now. Ignition Version 8.1.39

Type in 8.34 and see what you get.

5 Likes

See also, Numeric text field does not display what the value set to - #4 by pturmel.

Changing from Float to a Double seems to have fixed the issue with the additional numbers at the end.

It doesn't fix it, it only pushes it further.

If you're not doing float arithmetic, this quirk in IEEE-754 shouldn't matter, especially if you only need 2 decimals precision. Just format your numbers to show only 2 digits after the dot.

If you're dealing with currencies, or anything else that needs exact precision, use 2 integers instead of a float, one for the integral part, one for the decimals.

3 Likes

Thanks for bringing up this point that I was unaware of. Yes we are doing calculations from these numbers. Accuracy out that far does not typically matter but it most definitely can on occasion.

Referencing my example above, you are saying to put 8 as an INT & put the 34 in another INT. I could display these as the two numbers appended together with a decimal in between. I’m not sure how the math would roll though, but displaying it makes sense. I need to do a somewhat involved 4-function math formula. The output would most definitely be a decimal number.

If you only care about a fixed level of precision, you could do everything as integer math using a single tag, and only 'format' to your required level of precision at the end.

It is pretty rare to actually need these kinds of contrivances, though not unheard of. IEEE-754 floating point representation is almost always "good enough".

1 Like

You won’t get accuracy beyond 7 significant digits. While a float can represent a 32 bit number, there are only 2^23 available precision to do so. That’s why adding a small number to a large running accumulator fails after about 8 million.

If you only care about displaying the numbers without the runoff then just use numberFormat({value}, '#0.00') but if you need it for calculations where 4-6+ decimal places matter then you can use integer math. But in many cases it’s not necessary for example:

8.34 ^ 2 as a float = 69.5556025

8.34 ^ 2 as ints = 69.5556

In the end if you round both off to 2-4 decimal places you get the same result. Generally where it matters is when you have accumulations. As adding that amount over and over compounds the error and can affect your results eventually

Ex. say it’s a volume moved every second you add that amount to a total volume for a year.

8.34 as a float x 60 sec x 60 min x 24 hrs * 365 days = 263,010,240

8.34 as ints x 60 sec x 60 min x 24 hrs * 365 days = 263,010,245

Small but different.

1 Like

Consider watching this documentary to find out more :smiley:

3 Likes