Skip to content

Commit

Permalink
Added DoubleAsStringWithPrecision function - Complex Arithmetics (#1883)
Browse files Browse the repository at this point in the history
Added DoubleAsStringWithPrecision function in Complex Arithmetics Kata

This is to address the issue #1788
  • Loading branch information
devikamehra authored Aug 28, 2024
1 parent adcefe8 commit cf36843
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
8 changes: 5 additions & 3 deletions katas/content/complex_arithmetic/Common.qs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ namespace Kata.Verification {
}

function ComplexAsString(x : Complex) : String {
let precision = 3;
if x.Imag < 0.0 {
$"{x.Real} - {AbsD(x.Imag)}i"
$"{DoubleAsStringWithPrecision(x.Real, precision)} - {DoubleAsStringWithPrecision(AbsD(x.Imag), precision)}i"
} else {
$"{x.Real} + {x.Imag}i"
$"{DoubleAsStringWithPrecision(x.Real, precision)} + {DoubleAsStringWithPrecision(x.Imag, precision)}i"
}
}

function ComplexPolarAsString(x : ComplexPolar) : String {
$"{x.Magnitude} * exp({x.Argument}i)"
let precision = 3;
$"{DoubleAsStringWithPrecision(x.Magnitude, precision)} * exp({DoubleAsStringWithPrecision(x.Argument, precision)}i)"
}

operation CheckTwoComplexOpsAreSame(sol : (Complex, Complex) -> Complex, ref : (Complex, Complex) -> Complex) : Bool {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Kata.Verification {
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Math;

@EntryPoint()
Expand All @@ -10,8 +11,9 @@ namespace Kata.Verification {
let actual = Kata.ComplexModulus(x);

if AbsD(expected - actual) > 1e-6 {
let precision = 3;
Message("Incorrect");
Message($"For x = {ComplexAsString(x)} expected return {expected}, actual return {actual}.");
Message($"For x = {ComplexAsString(x)} expected return {DoubleAsStringWithPrecision(expected, precision)}, actual return {DoubleAsStringWithPrecision(actual, precision)}.");
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Kata.Verification {
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Random;

Expand All @@ -21,8 +22,9 @@ namespace Kata.Verification {
let actual = Kata.ComplexExpReal(r, x);

if not ComplexEqual(expected, actual) {
let precision = 3;
Message("Incorrect");
Message($"For x = {ComplexAsString(x)} and r = {r} expected return {ComplexAsString(expected)}, actual return {ComplexAsString(actual)}.");
Message($"For x = {ComplexAsString(x)} and r = {DoubleAsStringWithPrecision(r, precision)} expected return {ComplexAsString(expected)}, actual return {ComplexAsString(actual)}.");
return false;
}
}
Expand Down

0 comments on commit cf36843

Please sign in to comment.