DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

CHD versus Values object gives different results

Solved!
Go to solution

Hello,

 

I am getting rid of old commands in VBS script such as CHD and replacing them with their object-oriented counterparts. So far I had no problems but I found out one discrepancy. With For Loop I generate numbers from 0.1 to 1 with step 0.1. I write these values into channel row that is 10 times the current loop value.

It's easy enough, with command CHD(iLoop*10,"NoName/Kanal")=iLoop I get the correct results (0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, ...)

Oddly with Data.GetChannel("NoName/Kanal").Values(iLoop*10)=iLoop I get (0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.8, 0, 1, ...)

 

Call Data.Root.Clear()
Call Data.Root.ChannelGroups.Add("NoName")
Call Data.Root.ActiveChannelGroup.Channels.Add("Kanal",DataTypeFloat64)
Data.Root.ActiveChannelGroup.Channels("Kanal").ReservedSize(100)

Dim iLoop
For iLoop=0.1 To 10 step 0.1

   'Data.GetChannel("NoName/Kanal").Values(iLoop*10)=iLoop
   CHD(iLoop*10,"NoName/Kanal")=iLoop

Next

 

Maybe I'm overlooking something, any help would be appreciated.

 

Thank you,

Marek.

 

0 Kudos
Message 1 of 3
(2,084 Views)
Solution
Accepted by topic author hlavam2

Hmm, what happens here is a different behavior in converting the double to index.

While CHD seems to do a mathematical conversion, Values seems to do informatic conversion.

 

Option Explicit

Call Data.Root.Clear()
Call Data.Root.ChannelGroups.Add("NoName").Channels.Add("Kanal",DataTypeFloat64).ReservedSize(100)

' next line will write to 1, just decimal part is used
data.Root.ChannelGroups(1).Channels(1).values(1.99999999) = 1234
' next line will write to 2, index is rounded
CHD(1.99999999, "NoName/Kanal") = 4321

 

will result in

 , Kanal
1, 1234
2, 4321

 

You could avoid this behavior in your code by converting the double by using CLng which does a mathematical conversion.

Data.GetChannel("NoName/Kanal").Values(CLng(iLoop*10))=iLoop

YYou could speed up the code by using a channel object. It is even possible to drop the ".values".

 

Option Explicit

Call Data.Root.Clear()
Call Data.Root.ChannelGroups.Add("NoName")
Call Data.Root.ActiveChannelGroup.Channels.Add("Kanal",DataTypeFloat64)
Data.Root.ActiveChannelGroup.Channels("Kanal").ReservedSize(100)

dim channelObject : set channelObject = Data.GetChannel("NoName/Kanal")

Dim iLoop
For iLoop=0.1 To 10 step 0.1
   channelObject(CLng(iLoop*10))=iLoop
Next

Hope this helps a little.

 

Sorry for the inconvenience

Andreas

 

P.S.: As you see even in your 100 values floating point calculation is not as precise as mathematics wishs it to be. 

option explicit

dim sumVal : sumVal = 0.0

dim i
for i = 1 to 100
  sumVal = sumVal + 0.1
Next

MsgBox "+: " & sumVal & VBCRLF & "*:" & 100 * 0.1

even for just 100 values the result differs.

 

Message 2 of 3
(2,065 Views)

Thank you very much for your quick reply and explanation!

0 Kudos
Message 3 of 3
(2,057 Views)