Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I am an engineer writing fairly mathematical code and sometimes the Math.Pow() function makes complex equations extremely difficult to read and debug.

As result I wrote the following simple code to override the ^ function, it sort of helps but the only probelm is that ^ has less priority than * for example. This means adding superfluous (in my view brackets in places where they shouldn't be needed).

Is there any better way to get a power function into C#?
C#
public struct MyDouble
    {
        double var;

        private MyDouble(double var)
        {
            this.var = var;
        }

        public static implicit operator MyDouble(double value)
        {
            return new MyDouble(value);
        }

         public static explicit operator double(MyDouble value)
         {
             return value.var;
         }

        public static MyDouble operator ^(MyDouble val1, MyDouble val2)
        {
            return new MyDouble(Math.Pow(val1.var, val2.var));
        }

        public static MyDouble operator *(MyDouble val1, MyDouble val2)
        {
            return new MyDouble(val1.var * val2.var);
        }

        public static MyDouble operator /(MyDouble val1, MyDouble val2)
        {
            return new MyDouble(val1.var / val2.var);
        }

        public static MyDouble operator +(MyDouble val1, MyDouble val2)
        {
            return new MyDouble(val1.var + val2.var);
        }

        public static MyDouble operator -(MyDouble val1, double val2)
        {
            return new MyDouble(val1.var - val2);
        }

        public static MyDouble operator -(MyDouble val1, MyDouble val2)
        {
            return new MyDouble(val1.var - val2.var);
        }

        public static MyDouble operator -(MyDouble val1)
        {
            return new MyDouble(-val1.var);
        }

        public static MyDouble operator -(double val1, MyDouble val2)
        {
            return new MyDouble(val1 - val2.var);
        }

        public static bool operator >(MyDouble val1, MyDouble val2)
        {
            return val1.var > val2.var;
        }

        public static bool operator <(MyDouble val1, MyDouble val2)
        {
            return val1.var < val2.var;
        }

        public static MyDouble operator ^(MyDouble val1, double val2)
        {
            return new MyDouble(Math.Pow(val1.var, val2));
        }
}
Posted
Updated 25-Jul-14 3:57am
v3
Comments
CHill60 25-Jul-14 10:10am    
In addition to Solution 1 - brackets used for clarification are never superfluous - based on questions I've seen on forums not everyone understands operator precedence well enough.
manders999 4-Aug-14 10:24am    
Too many unnecessary brackets becomes a problem for readbaility, imagine a function with multiple embedded exponentials surrounded by up to a dozen brackets, its horrible.

You cannot change (or override) the evaluation precedence of operators in C#. Thus * will always take priority over ^ even if you were to overload them the other way round.

Is your gripe merely that Math.Pow is ugly?
 
Share this answer
 
I want to give you a good practical advice: never override mathematical operators to make them denote mathematical functions. It only seems to you now that it would improve readability, but in fact it will later confuse even you. At the same time, it can be very convenient to override mathematical operator to denote something non-arithmetic. For example '+' can build a tree from two tree operands. Or add an element to a list. Or something else.

—SA
 
Share this answer
 
Comments
Andreas Gieriet 26-Jul-14 0:25am    
My 5!
Cheers
Andi
Sergey Alexandrovich Kryukov 26-Jul-14 1:29am    
Thank you, Andi.
—SA
I understand that you are not happy with
C#
double a = ...;
double b = ...;
double c = Math.Pow(a,b);

How about
C#
...
double c = a.Pow(b);

If this is what you want, create an extension method for double type named Pow. E.g.
C#
public static class DoubleExtension
{
    public static double Pow(this double a, double b) { return Math.Pow(a, b); }
}

You may of course use other names than Pow for the extension method.
Cheers
Andi
 
Share this answer
 
v2
Comments
CHill60 26-Jul-14 9:15am    
Good idea and very readable. 5'd
Andreas Gieriet 26-Jul-14 14:16pm    
Thanks for your 5!
Cheers
Andi
manders999 4-Aug-14 10:25am    
I like this idea, it seems to give the most readable solution, will give it a try.
If you really need to write mathematical expressions using a human friendly notation then you should code a mathematical expression parser.
 
Share this answer
 
v2
http://msdn.microsoft.com/en-us/library/aa691323(v=vs.71).aspx[^]

C# does not have a operator for exponentiation (so you can't override them to this purpose), you will have to either use Math.Pow() or create your own Power function
This link above will give you every C# operator in case of curiosity

^ is the Logical XOR. If you want to use it as Power, you will have to keep brackets)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900