Click here to Skip to main content
15,905,427 members
Home / Discussions / C#
   

C#

 
Questiondouble accuracy and equality Pin
vineas19-Jan-06 7:06
vineas19-Jan-06 7:06 
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.
AnswerRe: double accuracy and equality Pin
Michael Potter19-Jan-06 11:28
Michael Potter19-Jan-06 11:28 
GeneralRe: double accuracy and equality Pin
vineas19-Jan-06 16:36
vineas19-Jan-06 16:36 
AnswerRe: double accuracy and equality Pin
Guffa19-Jan-06 12:40
Guffa19-Jan-06 12:40 
Questionquery on data columns Pin
dvsr19-Jan-06 7:04
dvsr19-Jan-06 7:04 
AnswerRe: query on data columns Pin
vineas19-Jan-06 7:24
vineas19-Jan-06 7:24 
GeneralRe: query on data columns Pin
dvsr19-Jan-06 12:15
dvsr19-Jan-06 12:15 
AnswerRe: query on data columns Pin
Wjousts19-Jan-06 9:49
Wjousts19-Jan-06 9:49 
GeneralRe: query on data columns Pin
dvsr19-Jan-06 10:01
dvsr19-Jan-06 10:01 
QuestionHow to wrapp .net 3rdparty controls with designer support? Pin
Chris Richner19-Jan-06 7:04
Chris Richner19-Jan-06 7:04 
Questionslideshow viewer Pin
GetSharp19-Jan-06 6:47
GetSharp19-Jan-06 6:47 
QuestionHow to check if 'RPC server is available' on a remote computer. Pin
ESTAN19-Jan-06 6:38
ESTAN19-Jan-06 6:38 
AnswerRe: How to check if 'RPC server is available' on a remote computer. Pin
ESTAN19-Jan-06 22:15
ESTAN19-Jan-06 22:15 
QuestionClass Library DLL with no functions Pin
uNsignedINT19-Jan-06 6:11
uNsignedINT19-Jan-06 6:11 
AnswerRe: Class Library DLL with no functions Pin
malharone19-Jan-06 6:19
malharone19-Jan-06 6:19 
GeneralRe: Class Library DLL with no functions Pin
uNsignedINT19-Jan-06 6:26
uNsignedINT19-Jan-06 6:26 
GeneralRe: Class Library DLL with no functions Pin
malharone19-Jan-06 6:49
malharone19-Jan-06 6:49 
GeneralRe: Class Library DLL with no functions Pin
uNsignedINT19-Jan-06 7:10
uNsignedINT19-Jan-06 7:10 
GeneralRe: Class Library DLL with no functions Pin
uNsignedINT19-Jan-06 7:57
uNsignedINT19-Jan-06 7:57 
GeneralRe: Class Library DLL with no functions Pin
Dave Kreskowiak19-Jan-06 10:32
mveDave Kreskowiak19-Jan-06 10:32 
Questionstring to richtext format Pin
Agyeman19-Jan-06 6:00
Agyeman19-Jan-06 6:00 
AnswerRe: string to richtext format Pin
malharone19-Jan-06 6:21
malharone19-Jan-06 6:21 
GeneralRe: string to richtext format Pin
Dario Solera19-Jan-06 6:41
Dario Solera19-Jan-06 6:41 
GeneralRe: string to richtext format Pin
malharone19-Jan-06 6:56
malharone19-Jan-06 6:56 
GeneralRe: string to richtext format Pin
lmoelleb19-Jan-06 20:44
lmoelleb19-Jan-06 20:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.