[gnome-calculator/60-split-out-a-backend-library] gcalc: improved complex numbers handling



commit 068717b524180253905bd7ac071271ed13630aad
Author: Daniel Espinosa Ortiz <esodan gmail com>
Date:   Thu Jan 3 17:19:40 2019 -0600

    gcalc: improved complex numbers handling
    
    Added negation method to Constant.

 gcalc/gcalc-constant.vala      |  1 +
 gcalc/gcalc-gconstant.vala     | 23 ++++++++++++++---------
 tests/gcalc-solving-basic.vala | 17 +++++++++++++++++
 3 files changed, 32 insertions(+), 9 deletions(-)
---
diff --git a/gcalc/gcalc-constant.vala b/gcalc/gcalc-constant.vala
index 21a0670a..d7d1b6b1 100644
--- a/gcalc/gcalc-constant.vala
+++ b/gcalc/gcalc-constant.vala
@@ -25,5 +25,6 @@ public interface GCalc.Constant : Object, Expression {
   public abstract Constant add (Constant c);
   public abstract Constant multiply (Constant c);
   public abstract Constant divide (Constant c);
+  public abstract Constant neg ();
 }
 
diff --git a/gcalc/gcalc-gconstant.vala b/gcalc/gcalc-gconstant.vala
index 463864f0..e95c9b00 100644
--- a/gcalc/gcalc-gconstant.vala
+++ b/gcalc/gcalc-gconstant.vala
@@ -20,14 +20,13 @@
  */
 public class GCalc.GConstant : GExpression, Constant {
   private MPC.Complex _complex = MPC.Complex (1000);
-  private bool imaginary = false;
 
   internal unowned MPC.Complex get_complex () { return _complex; }
 
   construct {
     _complex.set_double (0.0);
   }
-  internal GConstant.complex (MPC.Complex complex) {
+  internal GConstant.internal_complex (MPC.Complex complex) {
     _complex.set (complex);
   }
   public GConstant.integer (int val) {
@@ -39,9 +38,8 @@ public class GCalc.GConstant : GExpression, Constant {
   public GConstant.@double (double val) {
     _complex.set_double (val);
   }
-  public GConstant.new_imag (double real, double imag) {
+  public GConstant.complex (double real, double imag) {
     _complex.set_double (real, imag);
-    imaginary = true;
   }
 
   // Constant Interface
@@ -63,7 +61,7 @@ public class GCalc.GConstant : GExpression, Constant {
     var p1 = MPC.Complex (1000);
     p1.set ((c as GConstant).get_complex ());
     res.add (_complex, p1);
-    var nc = new GConstant.complex (res);
+    var nc = new GConstant.internal_complex (res);
     return nc as Constant;
   }
   public Constant subtract (Constant c)
@@ -73,7 +71,7 @@ public class GCalc.GConstant : GExpression, Constant {
     var p1 = MPC.Complex (1000);
     p1.set ((c as GConstant).get_complex ());
     res.subtract (_complex, p1);
-    var nc = new GConstant.complex (res);
+    var nc = new GConstant.internal_complex (res);
     return nc as Constant;
   }
   public Constant multiply (Constant c)
@@ -83,7 +81,7 @@ public class GCalc.GConstant : GExpression, Constant {
     var p1 = MPC.Complex (1000);
     p1.set ((c as GConstant).get_complex ());
     res.multiply (_complex, p1);
-    var nc = new GConstant.complex (res);
+    var nc = new GConstant.internal_complex (res);
     return nc as Constant;
   }
   public Constant divide (Constant c)
@@ -93,12 +91,19 @@ public class GCalc.GConstant : GExpression, Constant {
     var p1 = MPC.Complex (1000);
     p1.set ((c as GConstant).get_complex ());
     res.divide (_complex, p1);
-    var nc = new GConstant.complex (res);
+    var nc = new GConstant.internal_complex (res);
+    return nc as Constant;
+  }
+  public Constant neg ()
+  {
+    var res = MPC.Complex (1000);
+    res.neg (_complex);
+    var nc = new GConstant.internal_complex (res);
     return nc as Constant;
   }
   // Expression interface
   public override string to_string () {
-    if (imaginary) {
+    if (imag () != 0.0) {
       return MPC.Complex.to_string (10, 10, _complex);
     }
     return "%g".printf (real ());
diff --git a/tests/gcalc-solving-basic.vala b/tests/gcalc-solving-basic.vala
index 5d54ee31..6489772f 100644
--- a/tests/gcalc-solving-basic.vala
+++ b/tests/gcalc-solving-basic.vala
@@ -59,6 +59,23 @@ class Tests {
       message (c3.to_string ());
       assert (c3.real () == 3.0);
     });
+    Test.add_func ("/gcalc/solve/constant/negation",
+    ()=>{
+      var c1 = new GConstant.@double (9.0);
+      var c3 = c1.neg ();
+      assert (c3 != null);
+      message (c3.to_string ());
+      assert (c3.real () == -9.0);
+    });
+    Test.add_func ("/gcalc/solve/constant/complex",
+    ()=>{
+      var c1 = new GConstant.complex (10.0, 15.0);
+      var c3 = c1.neg ();
+      assert (c3 != null);
+      message (c3.to_string ());
+      assert (c3.real () == -10.0);
+      assert (c3.imag () == -15.0);
+    });
     return Test.run ();
   }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]