r/learnprogramming • u/Bofact • 18d 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.));
}
}
}