Incorrect values when using float.Parse(str ing)
I have discovered a problem with float.Parse(str ing) not getting values
exactly correct in some circumstances(C SV file source) but in very
similar circumstances(X ML file source) and with exactly the same value
it gets it perfectly correct all the time.
These are the results I got, XML is always correct, CSV are only
incorrect for some of the values (above about 0.01) but always gives
the same incorrect value.
StringValue XMLfloat CSVfloat
0.001380697 0.001380697 0.001380697 OK
0.008801419 0.008801419 0.008801419 OK
0.011789167 0.011789167 0.0117891673 Error
1.115063567 1.115063567 1.11506355 Error
2.233219287 2.233219287 2.23321939 Error
As you can see in the (CSV file source) I have tried parsing to double
then to float but the error still accurse when going into float.
The two bits of code are in the same class just different methods.
Are there any environmental parameters that can affect the way floats
operate?
XML file source
private static float GetAttributeFlo at(XmlAttribute Collection
_Attributes, AttributeName _AttributesName )
{
float _ValidFloat = 0.0F;
foreach(XmlAttr ibute _Attribute in _Attributes)
{
if(Equals(_Attr ibute.Name,_Att ributesName))
{
try
{
_ValidFloat = float.Parse(_At tribute.Value);
}
catch(Exception )
{ }
}
}
return _ValidFloat;
}
CSV file source
int[] CurveADC = new int[EnergyCompTable Values];
float[] CurveDose = new float[EnergyCompTable Values];
for(int y=0;y < EnergyCompTable Values;y++)
{
// CurveADC[y] = int.Parse(CalDa ta[CalDataIndex++]);
// float temp = 0.0F;
// string datas = CalData[CalDataIndex++];
// double temp2 = Convert.ToDoubl e(datas);
// float temp3 = (float)temp2;
// temp = float.Parse(dat as,new
System.Globaliz ation.CultureIn fo("en-GB", true));
// CurveDose[y] = temp;
CurveDose[y] = float.Parse(Cal Data[CalDataIndex++]);
}
Background info:
Basically I have got two files XML & CSV format which contain
identically the same data. The data is converted into a binary image
for uploading into hardware, which ever file is used identical binary
images are required.
I have discovered a problem with float.Parse(str ing) not getting values
exactly correct in some circumstances(C SV file source) but in very
similar circumstances(X ML file source) and with exactly the same value
it gets it perfectly correct all the time.
These are the results I got, XML is always correct, CSV are only
incorrect for some of the values (above about 0.01) but always gives
the same incorrect value.
StringValue XMLfloat CSVfloat
0.001380697 0.001380697 0.001380697 OK
0.008801419 0.008801419 0.008801419 OK
0.011789167 0.011789167 0.0117891673 Error
1.115063567 1.115063567 1.11506355 Error
2.233219287 2.233219287 2.23321939 Error
As you can see in the (CSV file source) I have tried parsing to double
then to float but the error still accurse when going into float.
The two bits of code are in the same class just different methods.
Are there any environmental parameters that can affect the way floats
operate?
XML file source
private static float GetAttributeFlo at(XmlAttribute Collection
_Attributes, AttributeName _AttributesName )
{
float _ValidFloat = 0.0F;
foreach(XmlAttr ibute _Attribute in _Attributes)
{
if(Equals(_Attr ibute.Name,_Att ributesName))
{
try
{
_ValidFloat = float.Parse(_At tribute.Value);
}
catch(Exception )
{ }
}
}
return _ValidFloat;
}
CSV file source
int[] CurveADC = new int[EnergyCompTable Values];
float[] CurveDose = new float[EnergyCompTable Values];
for(int y=0;y < EnergyCompTable Values;y++)
{
// CurveADC[y] = int.Parse(CalDa ta[CalDataIndex++]);
// float temp = 0.0F;
// string datas = CalData[CalDataIndex++];
// double temp2 = Convert.ToDoubl e(datas);
// float temp3 = (float)temp2;
// temp = float.Parse(dat as,new
System.Globaliz ation.CultureIn fo("en-GB", true));
// CurveDose[y] = temp;
CurveDose[y] = float.Parse(Cal Data[CalDataIndex++]);
}
Background info:
Basically I have got two files XML & CSV format which contain
identically the same data. The data is converted into a binary image
for uploading into hardware, which ever file is used identical binary
images are required.
Comment