LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

fscanf/scanf error

While debugging an instrument driver I discovered a scanf/fscanf problem.  The following code illustrates the problem.
 
char *sbuf = {"4.000000e-05"};
 
   short cnt;
   double f1 = 4.000000e-5;
   double f2 = 20e-6;
 
   cnt = (f1 / 20e-6) - 1;  // cnt = 1, right
   sscanf (sbuf, "%Lf", &f1);
   cnt = (f1 / 20e-6) - 1; // cnt = 0, wrong
I noticed that the if I set sbuf to "4.00000e-5" (1 less zero), then the division works.
 
I am using LabWindows v6.0
 
Is this a known issue? and/or is there a workaround for it?
0 Kudos
Message 1 of 2
(3,712 Views)
Since there is no exact representation for 4e-5 in IEEE floating point, I am not surprised you get the results you suggest. It is unrealistic to expect that a numeric literal in the source code will be converted to exactly the same floating point number as the result of the sscanf() run time library function.

The problems with inaccuracy in floating point representation are very well documented. You should never write code that assumes that floating point numbers will be represented exactly, since most likely they won't (unless for example they are integers within the mantissa range of the floating point representation).

Martin.
--
Martin
Certified CVI Developer
0 Kudos
Message 2 of 2
(3,706 Views)