LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Reading INI numbers.

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,

0 Kudos
Message 1 of 8
(3,420 Views)

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

0 Kudos
Message 2 of 8
(3,418 Views)

Sorry I have no Idea what you are reffering to or how to implement it.

0 Kudos
Message 3 of 8
(3,413 Views)

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

0 Kudos
Message 4 of 8
(3,410 Views)

Okay, but what is the best way to correct for this programmatically?  Do I just make everything double?

0 Kudos
Message 5 of 8
(3,406 Views)

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)

0 Kudos
Message 6 of 8
(3,402 Views)

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

0 Kudos
Message 7 of 8
(3,371 Views)

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...

0 Kudos
Message 8 of 8
(3,367 Views)