0

I use a lot of powers in a script for Unity, so I want to reduce the code needed for raising a variable to a power.

For small powers, I could use Mathf.Pow(a,2) = a*a, but I am looking for maybe something better or else I'll do my " a * a * a * a * a * a * a " tricks.

Note: I am a CEID undergraduate, and I have never used C# before, and it somehow bothers me that there isn't a '^' operator for raising to powers, maybe I could overload '^' if it is easy and not excessive time consuming.

EDIT: This is one of many MATLAB value assignments, I am trying to convert to C#, I wrote down on paper angle(a+bi) as atan(b/a), and I am doing it manually as I dont know of anything trustful to help me and I have a deadline problem:

q(2) = angle(d1*pz*2.0i-sqrt(d1*pz^3*4.0+d1^3*pz*4.0-a1^4-a2^4-a3^4-d1^4-py^4-pz^4-C1^4*px^4+C1^2*py^4*2.0-C1^4*py^4+a1^2*a2^2*2.0+a1^2*a3^2*2.0+a2^2*a3^2*2.0-a1^2*d1^2*2.0+a2^2*d1^2*2.0+a3^2*d1^2*2.0-a1^2*py^2*6.0-a2^2*py^2*2.0+a3^2*py^2*2.0-a1^2*pz^2*2.0+a2^2*pz^2*2.0+a3^2*pz^2*2.0-d1^2*py^2*2.0-d1^2*pz^2*6.0-py^2*pz^2*2.0+d1*py^2*pz*4.0+C1^3*a1*px^3*4.0+S1^3*a1*py^3*4.0-C1^2*a1^2*px^2*6.0+C1^2*a2^2*px^2*2.0+C1^2*a3^2*px^2*2.0+C1^2*a1^2*py^2*6.0+C1^2*a2^2*py^2*1.0e1-C1^2*a3^2*py^2*2.0-C1^4*a2^2*py^2*1.2e1+C1^6*a2^2*py^2*4.0-C1^2*d1^2*px^2*2.0+C1^2*d1^2*py^2*2.0-C1^2*px^2*py^2*6.0+C1^4*px^2*py^2*6.0-C1^2*px^2*pz^2*2.0+C1^2*py^2*pz^2*2.0+S1^6*a2^2*py^2*4.0+C1*a1^3*px*4.0+S1*a1^3*py*4.0+a1^2*d1*pz*4.0-a2^2*d1*pz*4.0-a3^2*d1*pz*4.0-C1*a1*a2^2*px*4.0-C1*a1*a3^2*px*4.0-C1*S1*px^3*py*4.0+C1*a1*d1^2*px*4.0+C1*a1*px*py^2*1.2e1+C1*a1*px*pz^2*4.0+S1*a1*a2^2*py*4.0-S1*a1*a3^2*py*4.0+S1*a1*d1^2*py*4.0+S1*a1*px^2*py*4.0+S1*a1*py*pz^2*4.0-C1*S1^3*px*py^3*4.0+C1*S1^3*px^3*py*4.0-C1^3*a1*px*py^2*1.2e1-S1^3*a1*a2^2*py*8.0+C1^2*d1*px^2*pz*4.0-C1^2*d1*py^2*pz*4.0-S1^3*a1*px^2*py*4.0-C1*a1*d1*px*pz*8.0-S1*a1*d1*py*pz*8.0-C1*S1*a1^2*px*py*1.2e1-C1*S1*a2^2*px*py*4.0+C1*S1*a3^2*px*py*4.0-C1*S1*d1^2*px*py*4.0-C1*S1*px*py*pz^2*4.0-C1^2*S1*a1*a2^2*py*8.0+C1^2*S1*a1*px^2*py*8.0+C1*S1^3*a2^2*px*py*8.0+C1^3*S1*a2^2*px*py*8.0+C1*S1*d1*px*py*pz*8.0)-a1^2*1i-a2^2*1i+a3^2*1i-d1^2*1i-py^2*1i-pz^2*1i-C1^2*px^2*1i+C1^2*py^2*1i+C1*a1*px*2.0i+S1*a1*py*2.0i-C1*S1*px*py*2.0i)-angle(-a2*(a1*-1i-d1+pz+C1*px*1i+S1^3*py*1i+C1^2*S1*py*1i));
hey
  • 13
  • 4
  • http://stackoverflow.com/questions/13259162/vb-net-power-operator-overloading-from-c-sharp looks interesting – stuartd Aug 16 '16 at 14:01
  • you could always implement it, but ias @QadeerMangrio said, just use the `Math.Pow` – Noctis Aug 16 '16 at 14:03
  • There *is* a `^` operator, but it doesn't something completely different. DO NOT overload it, for the sake of future programmers using your code, or you when you forget. – rory.ap Aug 16 '16 at 14:03
  • From MSDN: `For integral types, ^ computes the bitwise exclusive-OR of its operands. For bool operands, ^ computes the logical exclusive-or of its operands` – Noctis Aug 16 '16 at 14:05
  • Why do you want to "reduce the code needed for raising a variable to a power"? – Luaan Aug 16 '16 at 14:07
  • I'm struggling to see how `a * a * a * a * a * a * a` is more readable than `Math.Pow(a,7)`. – Baldrick Aug 16 '16 at 14:12
  • @Luaan well, for whoever reads the code to be able to recognise the values in use, including me, as I have 2 A4 pages of arithmetic operations for one variable. The copy/paste will take much and I dont want to do many mistakes and trying to find them 2 hours later – hey Aug 16 '16 at 14:15
  • @Baldrick well you are right, i stated it falsely. I just want to recognise what I am using as fast as possible – hey Aug 16 '16 at 14:15
  • 1
    @hey: Maybe you should post a code snippet on CodeReview - if you have a problem like that, it's quite possible you could restructure things in a way that would mitigate it. – Baldrick Aug 16 '16 at 14:18
  • @Baldrick Makes things clearer? – hey Aug 16 '16 at 14:24
  • i use the same powers over and over so ill just have variables: say ax that is going to be a^x – hey Aug 16 '16 at 14:52

1 Answers1

2

C# doesn't allow you to override operators in quite this way - ^ is simply XOR and you can't change it.

If you want to save on typing, you could write an extension method:

public static int Pow(this int base, int exponent)
{
  // Return the exponentiated value
}

Which allows you to write e.g.

42.Pow(2)

However, I wouldn't recommend doing that. Code is read more often than it is written, and the few extra characters don't quite justify an extension method.

If you really want to go for the "as few characters as possible approach", you might have a lot better success with a language like F#, which allows you to define your own inline operators, for example. And VB.NET already has ^ that does what you want.

Luaan
  • 62,244
  • 7
  • 97
  • 116
  • maybe i won't then – hey Aug 16 '16 at 14:16
  • You gave me an idea, I'll write all of them as Pw(a,x) and I'll replace, after I'm done and double checked, Pw with Mathf.Pow – hey Aug 16 '16 at 14:39
  • @hey Just talking from experience here: if you feel the need to do something like this, you're going to make mistakes. The code you write should make sense and be "parseable" by a human, line-by-line - from what you're saying it seems that you have a huge method that contains some arcane algorithm that's completely incomprehensible. It might be that its unavoidable, but you just need to be extra careful - and you'll probably make plenty of mistakes anyway :) Consider spliting the whole thing into a bunch of meaningful functions that make it easier to read and understand. – Luaan Aug 16 '16 at 14:41
  • Almost like that, I'm going to make it multiline and separate all the factors 3 or 4 per line, and at this time it seems unavoidable. – hey Aug 16 '16 at 14:44
  • @hey Well, if I were doing something like this (copying expressions from matlab), I'd probably consider writing a parser instead of trying to manually copy the expression to C#. There's just so many opportunities for errors it isn't pretty - and you'll have hardly any chance of debugging the issues that inevitable crop up. Write a parser that obeys the rules in matlab, and make it either result in C# code (e.g. using T4) or possibly a runtime method (probably not useful here). Maybe you could even find a library that already does this? – Luaan Aug 16 '16 at 14:50
  • @hey And you could also try just pasting the code to F# (where it should compile well), and decompile the results to C# - not pretty, but may be the simplest and safest option. – Luaan Aug 16 '16 at 14:51