?
Solved

How to use ^ as a power in c#

Posted on 2009-04-27
24
Medium Priority
?
395 Views
Last Modified: 2012-05-06
I am new to C# and am converting a bunch of code from various sources over to c# and I have one constant headache.  How can I overload ^ so that double^double is the equivelent of Math.Pow(double,double).  A) how can I do this, and B) are there any drawbacks to doing this?    I tried doing the code in the code block in one of my main calculation namespaces and got an error basically saying that it needed to be in the 'double' class.
public static double operator ^(double A, double B)
        {
            Math.Pow(A,B);
        }

Open in new window

0
Comment
Question by:perrytaylor
  • 10
  • 8
  • 5
23 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24243655
You can overload the operator but only for your self-made classes. That means that you will have to use the OO equivalent to the ^ operator of C++, which is indeed Math.Pow, or you need to create your own double type that you use throughout.
0
 

Author Comment

by:perrytaylor
ID: 24243697
Could one inherit the double class to a class called custom_double and set the operator there?
0
 

Author Comment

by:perrytaylor
ID: 24243893
I can't derive from double because it is sealed.  Does anyone know of simple workaround for this that does not involve reinvent the wheel?  i.e. the double class.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 800 total points
ID: 24243932
Why not define a struct called Double: (you'd probably want to return a Double instead of a double (redundant, huh?) for intuitiveness, but I wanted to show that you could do what you are looking for.
public struct Double
    {
        public double value;
 
        public static double operator ^ (Double A, Double B)
        {
            return Math.Pow(A.value, B.value);
        }
    }

Open in new window

0
 

Author Comment

by:perrytaylor
ID: 24244004
That is getting me a lot closer to where I would like to be, but is there any way we could make it so I didn't have to deal with the .value aspect of it?  I just see it dirtying up my code a fair amount.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24244047
Well you're dealing with a struct which has to have at least one member. I tried to make it as simple as possible so it would not be cluttered, but what I posted is as simple as I can think to make it. I welcome other insights (maybe I'll learn something too!).
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24244053
I made "value" public so you wouldn't have to write accessor methods/properties to get to the value.
0
 

Author Comment

by:perrytaylor
ID: 24244082
I'm good with the "Double" Structure being more complicated, I'm just trying to figure out how to make it so that I can declare a variable like this
Double C = 6;

Instead of

Double C = new Double();
C.value = 6;

This whole deal is fairly new to me and I am trying to do this as cleanly as possible while having a ton of code copied over from matlab and a ton of mathematics that involve powers.

0
 
LVL 39

Accepted Solution

by:
abel earned 1200 total points
ID: 24244251
I like the approach taken on this thread. Here's the solution that's started by kaufmed, extended to allow assignment and to allow code like this to work:

Double myDbl = 4.0;
Double otherDbl = myDbl ^ myDbl;
double yetOtherDbl = 2.0 ^ myDbl;

you can extend it in the same way to allow the ^ operator to act with integers and Double etc.

public struct Double
{
    public double value;
 
    public Double(double d)
    {
        value = d;
    }
 
    public Double(Double D)
    {
        value = D.value;
    }
 
    public static double operator ^(Double A, Double B)
    {
        return Math.Pow(A.value, B.value);
    }
 
    public static double operator ^(Double A, double B)
    {
        return Math.Pow(A.value, B);
    }
 
    public static double operator ^(double A, Double B)
    {
        return Math.Pow(A, B.value);
    }
 
    public static implicit operator Double(double d)
    {
        return fromdouble(d);
    }
 
    public static implicit operator double(Double D)
    {
        return fromDouble(D);
    }
 
    public static double fromDouble(Double D)
    {
        return D.value;
    }
 
    public static Double fromdouble(double d)
    {
        return new Double(d);
    }
}

Open in new window

0
 
LVL 39

Expert Comment

by:abel
ID: 24244300
Note that this is still not possible:

double otherDbl = 4.0 ^ 5.0;   // this
Double otherDbl = 4.0 ^ 5.0;   // or this doesn't work

0
 
LVL 39

Expert Comment

by:abel
ID: 24244350
Btw, you are not alone in your thoughts on wanting this feature, operator overloading for value types: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=168224
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24244367
Well what do you know...  I learned something too. I had no idea about the implicit keyword. And here I was just about to tell the guy he was screwed!!
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24244370
Thnx abel :)
0
 

Author Comment

by:perrytaylor
ID: 24244371
I am sure there is a lot more going on that I don't understand, but isn't the benefit of a language like C# that you are not protected from yourself and therefore you can implement things as you see fit?
0
 
LVL 39

Expert Comment

by:abel
ID: 24244386
Tx for the points, but I see you are new here and I would like to point you to the 10-points must system of EE. Experts will work there butts off to get an A-grade, and only an A-grade. If you are not satisfied, keep asking (on the subject, of course). If a workaround is provided because a real solution is not possible, don't blame the experts for giving you a half-breed workaround...the blame should go to the vendor, not them. Here's the link: 10pts must

Of course, when experts do not respond, do not go into you follow-ups or just don't give an answer, you can use the B/C grades, they are there for that reason.
0
 
LVL 39

Expert Comment

by:abel
ID: 24244411
> but isn't the benefit of a language like C# that you are not protected from yourself and therefore you can implement things as you see fit

No, that's actually the other way around (imo). C# is meant to protect you, the coder, from making mistakes. It was supposed to be an answer to Java (which is is) and they wanted to keep the language as clean and understandable as possible.

C++, C, are their to give you full freedom and to not protect you. They will let you make mistakes when you want to.
0
 

Author Comment

by:perrytaylor
ID: 24244445
I apologize for not giving you and A.  Thank you for pointing out what the standard for an A is, I agree both of you more than exceeded that standard.  Is there a way to change a grade that I don't see, or is it locked in stone?
0
 

Author Comment

by:perrytaylor
ID: 24244464
I guess I am going slowly up the ladder then from way more protected languages like vba, vb, and matlab.  
0
 
LVL 39

Expert Comment

by:abel
ID: 24244490
> Is there a way to change a grade that I don't see, or is it locked in stone?

there's a way: click "request attention", which is located next to your question, give a brief description of what you want to be changed (i.e., "please change the grade to an A, I accidentally checked B") and you should be fine. Moderators will monitor those requests and usually check back within 12 hours.
0
 

Author Comment

by:perrytaylor
ID: 24244629
I just discovered something while implementing this solution.  Order of operations is no where near correct.  Is there an easy fix for that, should I open a new question, or should I get really familiar w/ parenthesis?
0
 
LVL 39

Expert Comment

by:abel
ID: 24244849
the short answer is: get familiar with parentheses. But I'll have to look into it tomorrow, have to rush off now...
0
 

Author Comment

by:perrytaylor
ID: 24244870
FYI I opened a new question, figured it was different enough to warrant a new question.

http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_24359410.html
0
 

Author Closing Comment

by:perrytaylor
ID: 31575053
Thanks for your help, I split up the points because I believe both of you had good contributions to the final answer.  
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Integration Management Part 2
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question