LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Defining arrays in MathScript

Solved!
Go to solution

I've noticed an issue when using arrays that have been defined using MathScript. 

 

If I say enter A=0.1:0.1:3 then I get A=0.1, 0.2, 0.3...all the way up to and including 3.

 

However if I enter a larger final value, say A=0.1:0.1:10 then the array only goes up to 9.9.

 

Is there a deliberate reason for this or is it just some sort of bug?

 

James

0 Kudos
Message 1 of 8
(2,543 Views)
Solution
Accepted by topic author Jiminy

James,

 

While I have not checked this with Mathscript, it is likely that what you are seeing is the common result of the finite representation of numbers in binary.  Values like 0.1 are infinitely repeating fractions in binary. So when you sum 100 of them the accumulated error may be sufficient to cause the effect you have seen.

 

Lynn

Message 2 of 8
(2,532 Views)

I don't see what you are seeing. Are you doing this in a MathScript node, or the MathScript window? Which version of LabVIEW?

0 Kudos
Message 3 of 8
(2,523 Views)

I'm using labview 2010 and a MathScript node.

 

See attached

0 Kudos
Message 4 of 8
(2,521 Views)

I don't have the MathScript module for LV 2010, but I can tell you it does not happen in LV2011 (or 8.2 for that matter). One thing that seems odd about your image is that the "B" is on the wire, when it should be inside the box. Could be an artifact of whatever you used to capture the screenshot, though.

0 Kudos
Message 5 of 8
(2,505 Views)

Just an update here, 

 

If I save the vi and run it on other computers with exactly the same LabView build on them, it works! But it still wont work on mine.

I know other ways of defining arrays that work but I'd still like to know why this is happening.

 

0 Kudos
Message 6 of 8
(2,483 Views)

I've confirmed that the problem was indeed a cumulative error due to 0.1 not being exactly represented in binary. The data into and out of the Matscript node was single precision. 

 

Switching to double solved the problem.

0 Kudos
Message 7 of 8
(2,465 Views)

Jiminy,

 

Switching to double does not solve the problem - it just moves it several decimal points to the right.  You may get the values you want for this particular problem, but in general the same issue exists.

 

Try generating an integer array from 1 to 30 and then divide it by 10.  Compare the results to the array generated by your original code.  Look at the attached VI and notice that you got lucky.

 

Lynn

 

0 Kudos
Message 8 of 8
(2,451 Views)