CString to float conversion
-
Hello everybody, I am trying to convert CString to float using atof(). but its not giving the exact value. for eg.
CString strVal; GetDlgItem(IDC\_MAXLAYER)->GetWindowText(strVal); float mVal = atof(strVal);
IDC_MAXLAYER is edit box and in that, i am having the value 386.080, but after atof() conversion, mVal shows 386.07999 is that possible to get exact 386.080 by converting? Thanks in advance. A. Gopinath.
-
Hello everybody, I am trying to convert CString to float using atof(). but its not giving the exact value. for eg.
CString strVal; GetDlgItem(IDC\_MAXLAYER)->GetWindowText(strVal); float mVal = atof(strVal);
IDC_MAXLAYER is edit box and in that, i am having the value 386.080, but after atof() conversion, mVal shows 386.07999 is that possible to get exact 386.080 by converting? Thanks in advance. A. Gopinath.
Floating point numbers can not represent all real numbers accurately. Generally speaking: floating point numbers have a precision that is the greatest when the value is near zero. As soon as the whole part of the floating point number contains more and more digits, the fraction part quickly becomes super-inaccurate. Try entering numbers to your editbox like "234224.34654" into your editbox, you will see that the digits in the whole part will "steal" the precision from the fraction part because the value of this number is quite "far from zero". Read some more about floating point numbers: http://blogs.msdn.com/b/dwayneneed/archive/2010/05/07/fun-with-floating-point.aspx[^]. http://en.wikipedia.org/wiki/IEEE_754[^] People tend to use floating point numbers as a golden hammer when they don't know about its dark side. Sometimes its better to use just integers or fixed point arithmetic.