Operator overloading
-
I am working on a project in C# that involves some rather convoluted formulas to calculate values. The resulting code has been very difficult to write and even harder to read/debug. What I want to do is overload some operators to make the math a bit quicker to write and easer to read. (Especially since I know that some of these formulas will change in the future) For example; I would rather do something like “10^(somevariable* somethingelse)” than “math.pow(10, (somevariable* somethingelse)” since things like this are often buried deeper in a more complicated formula. [I have at least one formula involving 12 operators, 3 of them are raising part of the equation to the power of another part] I have looked up Operator Overloading and they all refer to using it on user defined classes or UDT’s. There must be an easer way.
David Wilkes
-
I am working on a project in C# that involves some rather convoluted formulas to calculate values. The resulting code has been very difficult to write and even harder to read/debug. What I want to do is overload some operators to make the math a bit quicker to write and easer to read. (Especially since I know that some of these formulas will change in the future) For example; I would rather do something like “10^(somevariable* somethingelse)” than “math.pow(10, (somevariable* somethingelse)” since things like this are often buried deeper in a more complicated formula. [I have at least one formula involving 12 operators, 3 of them are raising part of the equation to the power of another part] I have looked up Operator Overloading and they all refer to using it on user defined classes or UDT’s. There must be an easer way.
David Wilkes
Write it in BASIC? Make a UDT... "just because"? (It's a worthwhile exercise.) In my http://www.codeproject.com/csharp/PIEBALDTypesRational.asp[^] I use ^ for Power, it just feels more natural to me.
-
I am working on a project in C# that involves some rather convoluted formulas to calculate values. The resulting code has been very difficult to write and even harder to read/debug. What I want to do is overload some operators to make the math a bit quicker to write and easer to read. (Especially since I know that some of these formulas will change in the future) For example; I would rather do something like “10^(somevariable* somethingelse)” than “math.pow(10, (somevariable* somethingelse)” since things like this are often buried deeper in a more complicated formula. [I have at least one formula involving 12 operators, 3 of them are raising part of the equation to the power of another part] I have looked up Operator Overloading and they all refer to using it on user defined classes or UDT’s. There must be an easer way.
David Wilkes
You could abuse the XOR operator ^ by wrapping your numbers in a struct with overloaded operators. You cannot introduce new operators, but you can do something similar with C# 3.0: you can go from the prefix notation "Math.Pow(b, e)" to the infix notation "b.Pow(e)" by defining an extension method.
static class ExtensionMethods
{
public static double Pow(this double b, double e)
{
return Math.Pow(b, e);
}
} -
Write it in BASIC? Make a UDT... "just because"? (It's a worthwhile exercise.) In my http://www.codeproject.com/csharp/PIEBALDTypesRational.asp[^] I use ^ for Power, it just feels more natural to me.
Basic?!?! Wish I had, but far along enough now that I don't want to start over. Thanks for the link. I will see if it helps.
David Wilkes