r/learnprogramming • u/Bofact • 23d 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:
- A non-negative real number r.
- 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
View all comments
Show parent comment
1
u/Bofact 23d 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
, 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.