Urgent: "atof" function cannot outcome precise result

The following test code is compiled and debuged with VS.NET2003 IDE, the calculation is always wrong. The real calculation in my debugging process are

d1: 21.329999999999998

d2: 191.96999999999997

n: 19196

The expected correct is supposed to be

d1: 21.33

d2: 191.97

n: 19197

The test code is from my project algorithm code which outcome is seriously complained from QA team. I urgently need help to solve it.

Thanks


The test code is:

#include <math.h>

int main(int argc, char* argv[])
{
    char str[] = "21.33";

    double d1 = atof(str);

    double d2 = d1*9.0;

    long n = (long)(d2*100.0);

    return 0;
}


 



Answer this question

Urgent: "atof" function cannot outcome precise result

  • Marlon Chen

    Hi,

    Take a look at the following article:

    http://docs.sun.com/source/806-3568/ncg_goldberg.html

    Ronald Laeremans
    Visual C++ team

     


  • Dmitry Goncharenko - MSFT

    I read a double type value from GUI like 4.56 and when check it from debugger it became 4.5599999999999996 and such difference always cause my following calculation uncorrect result.

    Urgent help is appreicated!

    XZH

  • Urgent: "atof" function cannot outcome precise result