2

I need to do some work with variables of the type double and in the calculations I've come across something I do not understand. I know floating point types are not exact but it has enough precision for what I need.

First I multiply a value of 200 with a multiplier of 1.1 and get 220.00000000000003 which is expected and close enough to 220 and then I subtract that from the expected value (220). The result should be 0 or very close to 0. Instead I get -2.8421709430404007E-14 which I cannot explain.

using System;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            double u = 200;
            double v = 1.1;
            double x = u * v;
            double y = 220;
            double z = y - x;

            Console.WriteLine(u + " * " + v + " = " + x);

            Console.WriteLine(y + " - " + x + " = " + z);

            Console.ReadLine();
        }
    }
}

Any ideas?

5
  • 9
    -2.8421709430404007E-14 is close to zero (i.e. move the decimal place 14 times to the left).
    – crashmstr
    Oct 27, 2015 at 19:32
  • 1
    -2.8421709430404007E-14 means -2.84 * 10 ^ -14. really close. more information about Scientific notation Oct 27, 2015 at 19:33
  • 2
    Please learn scientific notation. Oct 27, 2015 at 19:34
  • 1
    In other words, the result is very close to (but not quite) -0.00000000000003
    – crashmstr
    Oct 27, 2015 at 19:34
  • That's very close to 0.
    – xxbbcc
    Oct 27, 2015 at 19:46

2 Answers 2

2

That is a number very close to 0. It's scientific notation; the E-14 means 10^-14. In other words, shift the decimal point 14 places to the left.

There are libraries that make working with floating-point operations a little more intuitive; otherwise, you'll need to figure out and test against a tolerance in order to see whether you're "close enough."

0

You should look into using the "decimal" type - your code looks like C#.net. This has high precision and is designed to work as you would expect (for financial calculation etc.).

Give it a try! :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.