Search code examples
.netdoubleprecisionmanaged-c++

Problem in double.Parse in Managed C++


I am getting a weird problem while parsing a double value in managed C++. It may be that I am doing something wrong. When I do:

double value = 0.006;
result = Math::Parse( value)

The output of result is 0.006000000000001. Why it is appending a 1?

Also when I go an round the value to 5 decimal places, it fails. I am doing:

result2 = Math::Round(result, 5)

But result2 is always 0.006000000000001. What am I doing wrong?


Solution

  • It is normal. This problem caused by IEEE format of double - in real 0.006 is represented as approximation of infinite binary fraction. So you have 3 ways -

    • use corresponding string formating to output
    • use Decimal type
    • don't use == to compare numbers, instead use < or > with constant error,e.g: (X -0.06) < Error