microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
katas/content/complex_arithmetic/complex_powers_real/Verification.qs
35lines · modecode
| 1 | namespace Kata.Verification { |
| 2 | import Std.Convert.*; |
| 3 | import Std.Math.*; |
| 4 | import Std.Random.*; |
| 5 | |
| 6 | function ComplexExpReal_Reference(r : Double, x : Complex) : Complex { |
| 7 | if AbsD(r) < 1e-9 { |
| 8 | return Complex(0., 0.); |
| 9 | } |
| 10 | let real = r^x.Real * Cos(x.Imag * Log(r)); |
| 11 | let imaginary = r^x.Real * Sin(x.Imag * Log(r)); |
| 12 | return Complex(real, imaginary); |
| 13 | } |
| 14 | |
| 15 | @EntryPoint() |
| 16 | operation CheckSolution() : Bool { |
| 17 | for ind in 0..24 { |
| 18 | let x = DrawRandomComplex(); |
| 19 | let r = ind == 0 ? 0.0 | DrawRandomDouble(0., 10.); |
| 20 | |
| 21 | let expected = ComplexExpReal_Reference(r, x); |
| 22 | let actual = Kata.ComplexExpReal(r, x); |
| 23 | |
| 24 | if not ComplexEqual(expected, actual) { |
| 25 | let precision = 3; |
| 26 | Message("Incorrect"); |
| 27 | Message($"For x = {ComplexAsString(x)} and r = {DoubleAsStringWithPrecision(r, precision)} expected return {ComplexAsString(expected)}, actual return {ComplexAsString(actual)}."); |
| 28 | return false; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | Message("Correct!"); |
| 33 | return true; |
| 34 | } |
| 35 | } |
| 36 | |