1

I have the following using MathNet library where child1 is -4.09 and child2 is -4.162. The result after Expression.Real((double1 - double2)) returns 0.072000000000000064. It should be 0.072. Can anyone please help me understand what is going on?

private static Expression GetSimplifiedExpression(Expression child1, Expression child2, LaTeXTokenType tokenType)
{
    double double1 = Evaluate.Evaluate(null, child1).RealValue;
    double double2 = Evaluate.Evaluate(null, child2).RealValue;

    return Expression.Real((double1 - double2));
}
klkj898
  • 31
  • 5

1 Answers1

0

First, let's convert decimal to binary:

-4.09 = -100.00010111000010100011110101110000101000111101011100001010001111...

-4.162 = -100.00101001011110001101010011111101111100111011011001000101101000...

Then, subtract those two binaries. The result in binary is:

0.00010010011011101001011110001101010011111101111100111011011001...

which is approximately equal to decimal 0.07199999999999999998.

It is not exactly 0.072000000000000064, but I think you could get the idea behind it. If you want the exact result, you could cast double to decimal:

var decimal1 = (decimal) double1;
var decimal2 = (decimal) double2;
Han Zhao
  • 1,932
  • 7
  • 10