T
trevor
Incorrect values when using float.Parse(string)
I have discovered a problem with float.Parse(string) not getting values
exactly correct in some circumstances(CSV file source) but in very
similar circumstances(XML 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 GetAttributeFloat(XmlAttributeCollection
_Attributes, AttributeName _AttributesName)
{
float _ValidFloat = 0.0F;
foreach(XmlAttribute _Attribute in _Attributes)
{
if(Equals(_Attribute.Name,_AttributesName))
{
try
{
_ValidFloat = float.Parse(_Attribute.Value);
}
catch(Exception)
{ }
}
}
return _ValidFloat;
}
CSV file source
int[] CurveADC = new int[EnergyCompTableValues];
float[] CurveDose = new float[EnergyCompTableValues];
for(int y=0;y < EnergyCompTableValues;y++)
{
// CurveADC[y] = int.Parse(CalData[CalDataIndex++]);
// float temp = 0.0F;
// string datas = CalData[CalDataIndex++];
// double temp2 = Convert.ToDouble(datas);
// float temp3 = (float)temp2;
// temp = float.Parse(datas,new
System.Globalization.CultureInfo("en-GB", true));
// CurveDose[y] = temp;
CurveDose[y] = float.Parse(CalData[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(string) not getting values
exactly correct in some circumstances(CSV file source) but in very
similar circumstances(XML 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 GetAttributeFloat(XmlAttributeCollection
_Attributes, AttributeName _AttributesName)
{
float _ValidFloat = 0.0F;
foreach(XmlAttribute _Attribute in _Attributes)
{
if(Equals(_Attribute.Name,_AttributesName))
{
try
{
_ValidFloat = float.Parse(_Attribute.Value);
}
catch(Exception)
{ }
}
}
return _ValidFloat;
}
CSV file source
int[] CurveADC = new int[EnergyCompTableValues];
float[] CurveDose = new float[EnergyCompTableValues];
for(int y=0;y < EnergyCompTableValues;y++)
{
// CurveADC[y] = int.Parse(CalData[CalDataIndex++]);
// float temp = 0.0F;
// string datas = CalData[CalDataIndex++];
// double temp2 = Convert.ToDouble(datas);
// float temp3 = (float)temp2;
// temp = float.Parse(datas,new
System.Globalization.CultureInfo("en-GB", true));
// CurveDose[y] = temp;
CurveDose[y] = float.Parse(CalData[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.