05-04-2011 10:59 AM
When I read a sting of values, "0.1, 1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1, 8.1, 9.1", from the INI file using
Ini_GetStringIntoBuffer (iniText, CONST_LEGEND_INI_STRING,ININamebuf, INIbuf, FILELINELENGTH+1);
I convert over to floats using
sscanf (INIbuf, "%f,%f,%f,%f,%f,%f,%f,%f,%f,%f",
&fTempVal[0], &fTempVal[1], &fTempVal[2],
&fTempVal[3], &fTempVal[4], &fTempVal[5],
&fTempVal[6], &fTempVal[7], &fTempVal[8], &fTempVal[9]);
when I check the values in the floats some are correct but some are 5.0999999 for 5.1 or 6.0999999 for 6.1.
How do I correct for this?
Thanks,
05-04-2011 11:06 AM
remember that binary representations with a finite number of digits can not represent all decimal numbers, due to the limited precision. That's why there is FLT_EPSILON and DBL_EPSILON... FLT_EPSILON is 1.19...e-7
05-04-2011 11:17 AM
Sorry I have no Idea what you are reffering to or how to implement it.
05-04-2011 11:27 AM
Sorry if it wasn't clear: FLOAT numbers have a precision epsilon of about 1.2E-7, where epsilon is the smallest number such that 1.0+epsilon != 1.0.
Phrased differently, 1.0+1.1E-7 and 1.0 are numerically identical (for type float). As a consequence, your values 5.0999999 and 5.1 are numerically identical, there are simply not sufficient bits available
You may also want to read here
05-04-2011 11:33 AM
Okay, but what is the best way to correct for this programmatically? Do I just make everything double?
05-04-2011 11:44 AM
Of course, double will not help much, it is still a finite numeric representation, with a smaller error...
'correct' depends on what you want to do... you can not have unlimited precision
in any case you should never have a floating point comparison such as if (fTempVal[0] == 6.0); instead use if (fabs(fTempVal[0]-6.0) < FLT_EPSILON)
05-10-2011 03:39 AM
hello , pls try below
Scan(info,"%s>%f,%f,%f,%f,%f,%f,%f,%f,%f,%f",&val[0],&val[1],&val[2],&val[3],&val[4],&val[5],&val[6],&val[7],&val[8],&val[9]);
or
Scan(info,"%s>%f%s%f%s%f%s%f%s%f%s%f%s%f%s%f%s%f%s%f",&val[0],temp,&val[1],temp,&val[2],temp,&val[3],temp,&val[4],temp,&val[5],temp,&val[6],temp,&val[7],temp,&val[8],temp,&val[9]);
i have tested with CVI 2010 , it's working.
Hope can help!
B.R
Gerry
05-10-2011 03:53 AM
Gerryli,
According to the help of the Scan function, the source specifier may be omitted, in which case the source is assumed to be %s (string). For example, the following calls are equivalent:
x = Scan (s, "%d", i)
x = Scan (s, "%s›%d", i)
Hence the suggested addition of %s in the format string should not make any difference...