r/learnprogramming • • 19d ago

Debugging Stuck in a programming task.

I am learning Q# via Visual Studio Code lessons, but in "Find Complex Power of Real Number" lesson I have the task below.

Inputs:

  1. A non-negative real number r.
  2. A complex number x=a+bi.

Goal: Return the complex number r^x=r^(a+bi)

My code is below, but I don't get what is wrong with it. ComplexExponent calculates e^x and ComplexExpReal calculates r^x as (e^x)^ln(r). And I don't know how to debug a Q# code.

NOTE: I don't touch the case 0^0 though and ComplexExponent passed all the tests from Find Complex Exponent lesson.

namespace Kata {


    function ComplexExponent(x : Std.Math.Complex) : Std.Math.Complex {
        // Implement your solution here...
        return Std.Math.PowC(Std.Math.Complex(Std.Math.E(), 0.),Std.Math.Complex(x.Real, 0.))*Std.Math.Complex(Std.Math.Cos(x.Imag),Std.Math.Sin(x.Imag));
    }
    function ComplexExpReal(r : Double, x : Std.Math.Complex) : Std.Math.Complex {
        // Implement your solution here...
        if(r==0.)
        {
            return Std.Math.Complex(0.,0.);
        }
        else
        {
            return Std.Math.PowC(ComplexExponent(x),Std.Math.Complex(Std.Math.Log(r),0.));
        }
    }
}
4 Upvotes

4 comments sorted by

1

u/[deleted] 19d ago

[removed] — view removed comment

1

u/Bofact 19d ago

Hello!

Thank you for the response!

I tried to find a function to raise e to x (Std.Math.Complex), and the closest I found was

Std.Math.PowC(Std.Math.Complex(Std.Math.E(), 0.), x)

, since Std.Math.Exp returns a name error, telling that Std.Math.Exp was not found.

There is an Exp function, but it takes an array of Paulis, among other things.

Plus, Std.Math.Log(r) returns NaN for r=0., so the result without branching would be NaN + NaN i; hence why I have used branching.