Problems with .NET double data type
-
I am developing an MFC application on the .NET compiler. When I use atof function to convert a string to double value the compiler changes the decimal part of the result. When I try to add two doubles the compiler does the same. Below there is a simple example that I tried with the results I got: example 1: char str[11]; memset(str,0x00,11); memcpy(str,"1234567.89",10); double a = atof(str); Output : a = 1234567.8899999999 -------------------------------- example 2 double a=1234567; double b=0.89; double d = a + b; Output : a = 1234567.8899999999 Does anyone know why does this happen and what can I do to make it work properly? Thanks in Advance sirtimid
-
I am developing an MFC application on the .NET compiler. When I use atof function to convert a string to double value the compiler changes the decimal part of the result. When I try to add two doubles the compiler does the same. Below there is a simple example that I tried with the results I got: example 1: char str[11]; memset(str,0x00,11); memcpy(str,"1234567.89",10); double a = atof(str); Output : a = 1234567.8899999999 -------------------------------- example 2 double a=1234567; double b=0.89; double d = a + b; Output : a = 1234567.8899999999 Does anyone know why does this happen and what can I do to make it work properly? Thanks in Advance sirtimid
-
this is not a double problem ; it is a rounding one, known by the iso definition.
TOXCCT >>> GEII power
[toxcct][VisualCalc] -
there's not much you can do about it. there's a book/paper about that : "What Every Computer Scientist Should Know About Floating Point Arithmetic" by David Goldberg http://docs.sun.com/source/806-3568/ncg\_goldberg.html
Maximilien Lincourt Your Head A Splode - Strong Bad
-