How should I compare these doubles to get the desi

2019-07-30 11:10发布

I have a simple example application here where I am multiplying and adding double variables and then comparing them against an expected result. In both cases the result is equal to the expected result yet when I do the comparison it fails.

static void Main(string[] args)
{
    double a = 98.1;
    double b = 107.7;
    double c = 92.5;
    double d = 96.5;

    double expectedResult = 88.5;
    double result1 = (1*2*a) + (-1*1*b);
    double result2 = (1*2*c) + (-1*1*d);            

    Console.WriteLine(String.Format("2x{0} - {1} = {2}\nEqual to 88.5? {3}\n", a, b, result1, expectedResult == result1));
    Console.WriteLine(String.Format("2x{0} - {1} = {2}\nEqual to 88.5? {3}\n", c, d, result2, expectedResult == result2));

    Console.Read();
}

And here is the output:

2x98.1 - 107.7 = 88.5
Equal to 88.5? False

2x92.5 - 96.5 = 88.5
Equal to 88.5? True

I need to be able to capture that it is in fact True in BOTH cases. How would I do it?

7条回答
我命由我不由天
2楼-- · 2019-07-30 11:44

There is a whole school of thought that is against using Double.Epsilon and similar numbers...

I think they use this: (taken from https://stackoverflow.com/a/2411661/613130 but modified with the checks for IsNaN and IsInfinity suggested here by nobugz

public static bool AboutEqual(double x, double y)
{
    if (double.IsNaN(x)) return double.IsNaN(y); 
    if (double.IsInfinity(x)) return double.IsInfinity(y) && Math.Sign(x) == Math.Sign(y);

    double epsilon = Math.Max(Math.Abs(x), Math.Abs(y)) * 1E-15;
    return Math.Abs(x - y) <= epsilon;
}

The 1E-15 "magic number" is based on the fact that doubles have a little more than 15 digits of precision.

I'll add that for your numbers it returns true :-)

查看更多
beautiful°
3楼-- · 2019-07-30 11:46

Simply change your rounding to level 2 , this will give TRUE

double result1 =Math.Round ( (1 * 2 * a) + (-1 * 1 * b),2);
查看更多
别忘想泡老子
4楼-- · 2019-07-30 11:49

using the debugger,

result1=88.499999999999986;
expectedResult = 88.5

So when using the double ,these are not equal.

查看更多
混吃等死
5楼-- · 2019-07-30 11:55

using Math.Round() will round result1 to the correct decimal

result1 = Math.Round(result1, 1);
查看更多
The star\"
6楼-- · 2019-07-30 11:59

Floating point numbers often don't contain the exact value that mathematics tells us, because of how they store numbers.

To still have a reliable comparison, you need to allow some difference:

private const double DoubleEpsilon = 2.22044604925031E-16;

/// <summary>Determines whether <paramref name="value1"/> is very close to <paramref name="value2"/>.</summary>
/// <param name="value1">The value1.</param>
/// <param name="value2">The value2.</param>
/// <returns><c>true</c> if <paramref name="value1"/> is very close to value2; otherwise, <c>false</c>.</returns>
public static bool IsVeryCloseTo(this double value1, double value2)
{
    if (value1 == value2)
        return true;

    var tolerance = (Math.Abs(value1) + Math.Abs(value2)) * DoubleEpsilon;
    var difference = value1 - value2;

    return -tolerance < difference && tolerance > difference;
}

Please also make sure to read this blog post.

查看更多
唯我独甜
7楼-- · 2019-07-30 11:59

It's a problem with how floating point numbers are represented in memory.

You should read this to get a better understanding of whats going on: What Every Computer Scientist Should Know About Floating-Point Arithmetic

查看更多
登录 后发表回答