[gnome-calculator/60-split-out-a-backend-library] gcalc: implemented acos() function



commit fc9f9e3b3c741b8b34f401f6b9ae285d9cd645e0
Author: Daniel Espinosa Ortiz <esodan gmail com>
Date:   Fri Jan 4 16:26:38 2019 -0600

    gcalc: implemented acos() function
    
    Updated date copyright

 gcalc/gcalc-function-acos.vala | 54 ++++++++++++++++++++++++++++++++++++++++++
 gcalc/gcalc-function-cos.vala  |  2 +-
 gcalc/gcalc-function-exp.vala  |  2 +-
 gcalc/gcalc-function-log.vala  |  2 +-
 gcalc/gcalc-function-sin.vala  |  2 +-
 gcalc/gcalc-function-tan.vala  |  2 +-
 gcalc/meson.build              |  1 +
 tests/gcalc-solving-basic.vala | 16 ++++++++++++-
 8 files changed, 75 insertions(+), 6 deletions(-)
---
diff --git a/gcalc/gcalc-function-acos.vala b/gcalc/gcalc-function-acos.vala
new file mode 100644
index 00000000..f4e47027
--- /dev/null
+++ b/gcalc/gcalc-function-acos.vala
@@ -0,0 +1,54 @@
+/* gcalc-function-acos.vala
+ *
+ * Copyright (C) 2019  Daniel Espinosa <esodan gmail com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ *      Daniel Espinosa <esodan gmail com>
+ */
+public class GCalc.GFunctionAcos : GFunction {
+
+  public GFunctionAcos () {
+    base ("acos", 1);
+    param_types.add (new GConstant ());
+  }
+
+  public override Expression call () throws GLib.Error
+  {
+    verify_params ();
+    GConstant c = null;
+    var exp = parameters.get_item (0) as Expression;
+    if (exp == null) {
+      throw new FunctionError.INVOCATION_ERROR ("Invalid parameter type. Expected %s", 
typeof(Expression).name ());
+    }
+    var ev = exp.solve ();
+    if (ev is ErrorResult) {
+       throw new FunctionError.INVOCATION_ERROR ("Invalid expression: %s", ((ErrorResult) ev).to_string ());
+    }
+    if (ev is Result) {
+      c = ((Result) ev).expression as GConstant;
+    }
+    if (c == null) {
+       throw new FunctionError.INVOCATION_ERROR ("Invalid expression in result");
+    }
+    var p1 = MPC.Complex (1000);
+    p1.set (c.get_complex ());
+    var res = MPC.Complex (1000);
+    res.acos (p1);
+    var nc = new GConstant.internal_complex (res);
+    return nc as Expression;
+  }
+}
+
diff --git a/gcalc/gcalc-function-cos.vala b/gcalc/gcalc-function-cos.vala
index b1f92d27..ff11424d 100644
--- a/gcalc/gcalc-function-cos.vala
+++ b/gcalc/gcalc-function-cos.vala
@@ -1,6 +1,6 @@
 /* gcalc-function-cos.vala
  *
- * Copyright (C) 2018  Daniel Espinosa <esodan gmail com>
+ * Copyright (C) 2019  Daniel Espinosa <esodan gmail com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
diff --git a/gcalc/gcalc-function-exp.vala b/gcalc/gcalc-function-exp.vala
index bcdadc7c..f9add0c4 100644
--- a/gcalc/gcalc-function-exp.vala
+++ b/gcalc/gcalc-function-exp.vala
@@ -1,6 +1,6 @@
 /* gcalc-function-exp.vala
  *
- * Copyright (C) 2018  Daniel Espinosa <esodan gmail com>
+ * Copyright (C) 2019  Daniel Espinosa <esodan gmail com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
diff --git a/gcalc/gcalc-function-log.vala b/gcalc/gcalc-function-log.vala
index be955f3a..7172680e 100644
--- a/gcalc/gcalc-function-log.vala
+++ b/gcalc/gcalc-function-log.vala
@@ -1,6 +1,6 @@
 /* gcalc-function-log.vala
  *
- * Copyright (C) 2018  Daniel Espinosa <esodan gmail com>
+ * Copyright (C) 2019  Daniel Espinosa <esodan gmail com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
diff --git a/gcalc/gcalc-function-sin.vala b/gcalc/gcalc-function-sin.vala
index f7b65642..eeffc54f 100644
--- a/gcalc/gcalc-function-sin.vala
+++ b/gcalc/gcalc-function-sin.vala
@@ -1,6 +1,6 @@
 /* gcalc-function-sin.vala
  *
- * Copyright (C) 2018  Daniel Espinosa <esodan gmail com>
+ * Copyright (C) 2019  Daniel Espinosa <esodan gmail com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
diff --git a/gcalc/gcalc-function-tan.vala b/gcalc/gcalc-function-tan.vala
index 4c1ee722..e29f700e 100644
--- a/gcalc/gcalc-function-tan.vala
+++ b/gcalc/gcalc-function-tan.vala
@@ -1,6 +1,6 @@
 /* gcalc-function-tan.vala
  *
- * Copyright (C) 2018  Daniel Espinosa <esodan gmail com>
+ * Copyright (C) 2019  Daniel Espinosa <esodan gmail com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
diff --git a/gcalc/meson.build b/gcalc/meson.build
index 727142af..47a1ccaa 100644
--- a/gcalc/meson.build
+++ b/gcalc/meson.build
@@ -46,6 +46,7 @@ sources = files([
        'gcalc-expression.vala',
        'gcalc-expression-container.vala',
        'gcalc-function.vala',
+       'gcalc-function-acos.vala',
        'gcalc-function-asin.vala',
        'gcalc-function-cos.vala',
        'gcalc-function-exp.vala',
diff --git a/tests/gcalc-solving-basic.vala b/tests/gcalc-solving-basic.vala
index 4a7a7036..8526a3be 100644
--- a/tests/gcalc-solving-basic.vala
+++ b/tests/gcalc-solving-basic.vala
@@ -554,7 +554,21 @@ class Tests {
     ()=>{
       try {
         var c1 = new GConstant.@double (0.0);
-        var f = new GFunctionTan ();
+        var f = new GFunctionAsin ();
+        f.parameters.add (c1);
+        var c2 = f.call () as Constant;
+        assert (c2 != null);
+        message (c2.to_string ());
+        assert (c2.real () >= 0.0);
+      } catch (GLib.Error e) {
+        warning ("Error: %s", e.message);
+      }
+    });
+    Test.add_func ("/gcalc/solve/function/acos",
+    ()=>{
+      try {
+        var c1 = new GConstant.@double (1.0);
+        var f = new GFunctionCos ();
         f.parameters.add (c1);
         var c2 = f.call () as Constant;
         assert (c2 != null);


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