LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Discrepancy when initializing a shift register

Solved!
Go to solution

Hello,

 

I have noticed an apparent discrepancy when initializing a shift register for use in a while loop. The attached diagram illustrates the problem. A value of 0 is used (rather than the initial value of 5) unless I enable Highlight Execution.

 

Initialization of shift register.jpg

The same issue exists in both LabVIEW 2016 and LabVIEW 2017.

 

Thanks,
David

0 Kudos
Message 1 of 7
(4,253 Views)

I suspect what is happening is that the I32 overflows and gets replaced by a zero before you can notice. If you create an auto-indexing tunnel to see all the values stuffed into the shift register, you will see the sequence of outputs that leads to a result of zero by the time you stop the VI. Even with a 100ms wait it only takes about 4 seconds to occur, so with no wait it happens almost instantaenously.

 

5
10
30
120
600
3600
25200
201600
1814400
18144000
199584000
-1899959296
1070332928
2099759104
1431615488
1431011328
-1442611200
-197197824
548208640
-1920729088
-1680605184
1681391616
17301504
415236096
1790967808
-679477248
-1166016512
1711276032
-1912602624
-1543503872
-603979776
-2147483648
-2147483648
0
0
0
0

Message 2 of 7
(4,242 Views)

What you are trying to compute is basically a factorial:

5*(i+1)!


It's not the fact that the I32 overflows that is giving you a zero result. It actually starts overflowing after 12 iterations (i=12)

 

5*(12+1)! = 31135104000

[32bit integer -2147483648 to 2147483647]

 

But as i continues to increase, the LS bits of the shift register continue to fill with zeros. Eventually when i=33, all bits are zeros, and zero x anything = zero

 

You can see the effect better if you display the resulting array in binary.

 

Capture.JPG

0 Kudos
Message 3 of 7
(4,205 Views)

Thanks for your reply. I agree that an overflow will occur, but that is not what I'm seeing. I modified the VI to prevent an overflow and ran it twice (once with execution highlighting disabled, and a second time with execution highlighting enabled. Here are the results in each case:

With execution highlighting disabled, the shift register does not initialize and the output value is always zero.With execution highlighting disabled, the shift register does not initialize and the output value is always zero.With execution highlighting enabled, the shift register is initialized to a value of 5 and the output value is eventually clipped at 1000000.With execution highlighting enabled, the shift register is initialized to a value of 5 and the output value is eventually clipped at 1000000.

0 Kudos
Message 4 of 7
(4,182 Views)
Solution
Accepted by topic author DB@KMC4ni

The in-range and coerce does not change anything from the others have said. When it overflows, it coerces the value to zero and ...

 

Mod your code to stop after five iterations.

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 5 of 7
(4,175 Views)

You know, I have to admit that I just had a similar issue where I was raising an integer to a power and couldn't understand why sometimes the answer was incorrect.  Turns out that sometimes the resulting number was larger than the datatype could handle.  So I coerced the original number to a larger datatype so the resulting calculation would also use that datatype.  Yikes.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 6 of 7
(4,141 Views)

If you want to verify that your shift register is being initialized to 5, you can add a Wait (ms) function to your while loop. This will slow down the execution. You can then probe the wire coming out of the shift register and watch the value of that wire increase. By slowing down the while loop using the Wait (ms) function you won't need to turn on highlight execution. I have attached screenshot below as well as my VI. 

 

Initializing shift register.PNG

Adena L.
Technical Support Engineer
National Instruments
0 Kudos
Message 7 of 7
(3,994 Views)