LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Coercion problems with double data type controls.

I'm pretty sure this has been discussed before, but I could find any topics that contained any solution of sorts.

 

I am observing a coercion problem when using a double data-type control to increment its value in 0.1 steps. I know that doubles cannot always be accurately represented in a binary system, but this particular calculation is puzzling me.

 

I have set up two side-by-side sections of the same code, one taking values from the control and the other making its own values.  There is a difference in the outputs. I want to be able to use the control, but to achieve the "Sim Data" output. Any ideas how I can force the issue?

 

The code sections laying side-by-side:

calculations 1.JPG

 

The results:

calculations 2.JPG

 

The left output is desired (I only put it in an array so we can easily see the history of outputs) but I need to use the manual control.



Never say "Oops." Always say "Ah, interesting!"

0 Kudos
Message 1 of 5
(2,449 Views)

Even with this slight modfication, it doesn't work as I'd like (I also tried converting to U32 instead of using "round to nearest", but to no avail).

 

calculations 3.JPG



Never say "Oops." Always say "Ah, interesting!"

0 Kudos
Message 2 of 5
(2,444 Views)

The problem is, as you suspected, related to the representation of doubles in binary. 0.1 has an infinitely repeating representation in binary. So as you increment the 0.1 steps in the double control, the round-off error accumulates and eventually produces the results you see.  Change the display format for the indicators in the Man Data array to show 16-20 significant figures to see what I am talking about. Changing the display format similarly on the y control may be informative.

 

In the Sim Data calculation the input is always an integer.  So the round-off error does not accumulate.

 

Lynn

0 Kudos
Message 3 of 5
(2,441 Views)

Hi,

 

multiply your "y" values by 10, then round to nearest, then use the calculation for "sim data"...

 

As you noted before: DBL cannot represent fractions of 10 accurately!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 4 of 5
(2,438 Views)

Make the y control an integer and use a double indicator to show the value of y as calculated in Man Data.

 

Lynn

0 Kudos
Message 5 of 5
(2,435 Views)