0

I'm not getting one significant digit to the right of the decimal when using _wtof. Here is my code:

DOUBLE fVersion = 0.0;  // Initialize to zero; confirmed fVersion is 0.00000000000000000    
TCHAR sVersion[64] = {0};
_tcscpy_s(sVersion,64,L"1.1");
fVersion = _wtof(sVersion);

fVersion is 1.1000000000000001 after _wtof is used, not 1.1.

Can anyone shed some light on why? But most importantly, how to fix so it's just 1.1?

JeffR
  • 765
  • 2
  • 8
  • 23
  • 2
    floating point numbers are normally not 100% accurate: http://stackoverflow.com/questions/10334688/how-dangerous-is-it-to-compare-floating-point-values/10335601#10335601 – thumbmunkeys Jul 11 '14 at 12:40

1 Answers1

0

You cannot accurately represent 1/10 (decimal) in binary floating point. Thus, there will be an error.

Consider representing 1/3 in decimal floating point, you end up with 0.3333333... and it just goes on forever, because it's not possible, either.

The typical recommended reading is What Every Computer Scientist Should Know About Floating-Point Arithmetic. It's really very good, give it a read.

unwind
  • 391,730
  • 64
  • 469
  • 606