Re: How to overload == for System.Double

Shawn Wildermuth <[email protected]> Thu, 1 Feb 2007 16:21:11 -0500
Newsgroups gmane.comp.windows.devel.dotnet.cx
Message-ID <[email protected]>
<sarcasm>
   You should be able to do this with C# 3.0 Extension Methods (or whatever
they are called).  Of course, it’s an impossibly bad idea so don't try...but
luckily we'll be able to do it soon. 
</sarcasm>


Thanks,
 
Shawn Wildermuth
Wildermuth Consulting Services, LLC
http://wildermuthconsulting.com
http://adoguy.com
Microsoft MVP (C#), MCSD.NET, Author and Speaker

-----Original Message-----
From: Discussion relating to the specifics of the C# and Managed C++
languages [mailto:[email protected]] On Behalf Of Eames, Andrew
Sent: Thursday, February 01, 2007 4:00 PM
To: [email protected]
Subject: Re: [DOTNET-CX] How to overload == for System.Double

 Even if you could do this it is probably a really bad idea. Instead I
suggest you write a separate function 

Static bool Equallish(doubl lhs,double rhs)

Andrew

-----Original Message-----
From: Discussion relating to the specifics of the C# and Managed C++
languages [mailto:[email protected]] On Behalf Of Jack Brown
Sent: Thursday, February 01, 2007 3:33 PM
To: [email protected]
Subject: [DOTNET-CX] How to overload == for System.Double

Is it possible?

If I put these in a project:

                public static bool operator ==( double lhs, double rhs )
                {
                }
                public static bool operator !=( double lhs, double rhs )
                {
                }

I get a compiler error: One of the parameters of a binary operator must
be the containing type

I get this since the overloads are inside a class so they need to have
parameters that are of the containing class type. My problem is that I
can't put the operator overloads outside a class.

The reason I'm doing this is that I want to avoid the problems
associated with exact equality comparisons of floating point numbers. I
plan/wish to add a Math.Abs(lhs-rhs) <= epsilon where epsilon is some
suitably small value.

Any help, or at least confirmation that I can't do what I'd like to
would be appreciated.

--
Jack Brown
Controls Engineer
Electroimpact, Inc.
Desk: 425-609-4988
Cell: 206-617-6482
Office: 425-348-8090
[email protected]

===================================
This list is hosted by DevelopMentor(r)  http://www.develop.com

View archives and manage your subscription(s) at
http://discuss.develop.com


-- 
BEGIN-ANTISPAM-VOTING-LINKS
------------------------------------------------------

NOTE: This message was trained as non-spam.  If this is wrong,
please correct the training as soon as possible.

Teach CanIt if this mail (ID 7375907) is spam:
Spam:
http://mail-gw.cognex.com/canit/b.php?c=s&i=7375907&m=aef34f0d2fc9
Not spam:
http://mail-gw.cognex.com/canit/b.php?c=n&i=7375907&m=aef34f0d2fc9
Forget vote:
http://mail-gw.cognex.com/canit/b.php?c=f&i=7375907&m=aef34f0d2fc9
------------------------------------------------------
END-ANTISPAM-VOTING-LINKS

===================================
This list is hosted by DevelopMentor.  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com