Reputation: 35
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?
Upvotes: 2
Views: 3430
Reputation: 322
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! :-)
Upvotes: 0
Reputation: 196
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."
Upvotes: 2