double accuracy and equality
-
I'm working on a project with some pretty math intensive calculations that must output double values. After much testing, it was determined that we have an accuracy out to 10 digits after the decimal point. This is adequate for what I'm working with so we kept going forward. Now, in the next phase of the project, comparisons have to be made between two structures that contain the double results, so I overloaded Equals() to do a comparison to the tested accuracy. Immediately I found some big problems with this. Because of the way Equals() is determined, I run into the problem where the following is true: A==B, B==C, A!=C. I'm a bit stumped on how to fix this. There must be a way, but I'm not really seeing anything. I created the following test code to illustrate my problem:
using System; using System.Collections; namespace TestIt { class Class1 { [STAThread] static void Main(string[] args) { SomeStruct val1 = new SomeStruct(1.0); SomeStruct val2 = new SomeStruct(1.0); SomeStruct val3 = new SomeStruct(1.00000000009999); SomeStruct val4 = new SomeStruct(0.99999999999); Console.WriteLine("Starting . . ."); if (val1.Equals(val2)) Console.WriteLine("val1 == val2"); if (val2.Equals(val3)) Console.WriteLine("val2 == val3"); if (val1.Equals(val3)) Console.WriteLine("val1 == val3"); if (val1.Equals(val4)) Console.WriteLine("val1 == val4"); if (val3.Equals(val4)) Console.WriteLine("val3 == val4"); // problem, this doesn't happen Console.WriteLine("Done"); } public struct SomeStruct { private static double accuracy = 0.0000000001; public double Value; public SomeStruct(double val) { Value = val; } public override bool Equals(object obj) { if (obj is SomeStruct) return (Math.Abs(Value - ((SomeStruct)obj).Value) < accuracy); return false; } } } }
Anyone have any ideas? ----- In the land of the blind, the one eyed man is king. -
I'm working on a project with some pretty math intensive calculations that must output double values. After much testing, it was determined that we have an accuracy out to 10 digits after the decimal point. This is adequate for what I'm working with so we kept going forward. Now, in the next phase of the project, comparisons have to be made between two structures that contain the double results, so I overloaded Equals() to do a comparison to the tested accuracy. Immediately I found some big problems with this. Because of the way Equals() is determined, I run into the problem where the following is true: A==B, B==C, A!=C. I'm a bit stumped on how to fix this. There must be a way, but I'm not really seeing anything. I created the following test code to illustrate my problem:
using System; using System.Collections; namespace TestIt { class Class1 { [STAThread] static void Main(string[] args) { SomeStruct val1 = new SomeStruct(1.0); SomeStruct val2 = new SomeStruct(1.0); SomeStruct val3 = new SomeStruct(1.00000000009999); SomeStruct val4 = new SomeStruct(0.99999999999); Console.WriteLine("Starting . . ."); if (val1.Equals(val2)) Console.WriteLine("val1 == val2"); if (val2.Equals(val3)) Console.WriteLine("val2 == val3"); if (val1.Equals(val3)) Console.WriteLine("val1 == val3"); if (val1.Equals(val4)) Console.WriteLine("val1 == val4"); if (val3.Equals(val4)) Console.WriteLine("val3 == val4"); // problem, this doesn't happen Console.WriteLine("Done"); } public struct SomeStruct { private static double accuracy = 0.0000000001; public double Value; public SomeStruct(double val) { Value = val; } public override bool Equals(object obj) { if (obj is SomeStruct) return (Math.Abs(Value - ((SomeStruct)obj).Value) < accuracy); return false; } } } }
Anyone have any ideas? ----- In the land of the blind, the one eyed man is king.I think you will find
val3 - val4
is greater thenaccuracy
. The program is working as written. You will have to change the value ofaccuracy
to get the results you want. If you want to useaccuracy
as defined, you may want to truncate all your numbers to 10 digits before testing for equality. Take a look atdouble.Epsilon
. It contains a number you can use to test for equality on doubles but, it carries it to the least significant digit. Also, take a look at theDecimal
structure. It may suit your needs out of the box. -
I'm working on a project with some pretty math intensive calculations that must output double values. After much testing, it was determined that we have an accuracy out to 10 digits after the decimal point. This is adequate for what I'm working with so we kept going forward. Now, in the next phase of the project, comparisons have to be made between two structures that contain the double results, so I overloaded Equals() to do a comparison to the tested accuracy. Immediately I found some big problems with this. Because of the way Equals() is determined, I run into the problem where the following is true: A==B, B==C, A!=C. I'm a bit stumped on how to fix this. There must be a way, but I'm not really seeing anything. I created the following test code to illustrate my problem:
using System; using System.Collections; namespace TestIt { class Class1 { [STAThread] static void Main(string[] args) { SomeStruct val1 = new SomeStruct(1.0); SomeStruct val2 = new SomeStruct(1.0); SomeStruct val3 = new SomeStruct(1.00000000009999); SomeStruct val4 = new SomeStruct(0.99999999999); Console.WriteLine("Starting . . ."); if (val1.Equals(val2)) Console.WriteLine("val1 == val2"); if (val2.Equals(val3)) Console.WriteLine("val2 == val3"); if (val1.Equals(val3)) Console.WriteLine("val1 == val3"); if (val1.Equals(val4)) Console.WriteLine("val1 == val4"); if (val3.Equals(val4)) Console.WriteLine("val3 == val4"); // problem, this doesn't happen Console.WriteLine("Done"); } public struct SomeStruct { private static double accuracy = 0.0000000001; public double Value; public SomeStruct(double val) { Value = val; } public override bool Equals(object obj) { if (obj is SomeStruct) return (Math.Abs(Value - ((SomeStruct)obj).Value) < accuracy); return false; } } } }
Anyone have any ideas? ----- In the land of the blind, the one eyed man is king. -
I think you will find
val3 - val4
is greater thenaccuracy
. The program is working as written. You will have to change the value ofaccuracy
to get the results you want. If you want to useaccuracy
as defined, you may want to truncate all your numbers to 10 digits before testing for equality. Take a look atdouble.Epsilon
. It contains a number you can use to test for equality on doubles but, it carries it to the least significant digit. Also, take a look at theDecimal
structure. It may suit your needs out of the box.Yes, val3 - val4 is greater than accuracy, but that leaves me with the problem that val3 == val1 is true, val1 == val4 is true, but val3 == val4 is false. A==B, B==C therefore A==C is a basic mathematical principal that this is breaking, and I'm afraid will cause problems, and I can't figure out a way around it. Truncation isn't quite right either, that way 1.0000000001 and 1.00000000009 wouldn't match up. I've since rewritten the equality method, to get much better results but no matter how I change it, I still can't get around that basic problem. BTW, I've thought about using the Decimal structure for internal calculations, but I don't know how much it would gain me when most of the intermediate results will be stored outside of my class and will be in double. ----- In the land of the blind, the one eyed man is king.