AnsweredAssumed Answered

Problems with Pi float Values conversion from and to C# double

Question asked by VarbanVarbanov on Nov 24, 2015
Latest reply on Dec 3, 2015 by Marcos Vainer Loeff

Hi everyone,

We use AF Sdk to read and write values from and to PI and double in C# for representing them.


For reading we use the following method:


var afValues = point.RecordedValues(new AFTimeRange(afStartTime, afEndTime), boundaryType, string.Empty, false);


var value = Convert.ToDouble(piValue.Value);


and for writting:


var afValues = values.Select(t => new AFValue(t.Value, GetAfTime(new TimeseriesProviderTime(t.Key), timeSeriesId))).ToList();

point.UpdateValues(afValues, AFUpdateOption.Replace);


where point is of type OSIsoft.AF.PI.PIPoint.


However sometimes (often actially) when we convert the value for example 3.2 which comes from pi, the value in C# it becomes something like 3.20000004768372 and vice versa - when we pass AFValue, created with double value 3.2, the value, written in Pi is something like 3.20000004768372. The first situation ("wrong" reading) happens when the Pi Point type is float32, and the second situatuion ("wrong" writting) happens when Pi Point type is float64.


I guess this happens during conversion because of different representation of C# double and the types in PI. But the question is:
What is the way for this behavior to be avoided?