I'm writting a calculator class that takes a math expression as string, solves it, and returns the result as double.
So far everything works as intended.
My problem is that one of my unit tests fails.
// this test should pass but fails with messag:
// Assert.AreEqual failed. Expected:<0,6000000000000001>. Actual:<1>.
Assert.AreEqual(Solve("1 + 2 - 3 * 4 / 5"), ( 1 + 2 - 3 * 4 / 5));
You can test the problem with this code:
using System;
public class Program
{
public static void Main()
{
double r = 1 + 2 - 3 * 4 / 5; // should be 0.6 or 0.6000000000000001
Console.WriteLine(r == 1); // Prints True
Console.WriteLine("Result: " + r); // Prints 1
}
}
Fiddle https://dotnetfiddle.net/rRZtAu
How do i get the correct mathematical result?