[gnome-calculator/60-split-out-a-backend-library] gcalc: implemented asin() function
- From: Daniel Espinosa Ortiz <despinosa src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-calculator/60-split-out-a-backend-library] gcalc: implemented asin() function
- Date: Sat, 5 Jan 2019 02:34:04 +0000 (UTC)
commit 8faa8e91c11edf4e3e944e1b89a1ca1d5d5acfcf
Author: Daniel Espinosa Ortiz <esodan gmail com>
Date: Fri Jan 4 16:22:02 2019 -0600
gcalc: implemented asin() function
Fixed function's names
gcalc/gcalc-function-asin.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/gcalc-gmath-equation-manager.vala | 5 +++
gcalc/meson.build | 1 +
tests/gcalc-solving-basic.vala | 14 +++++++++
9 files changed, 79 insertions(+), 5 deletions(-)
---
diff --git a/gcalc/gcalc-function-asin.vala b/gcalc/gcalc-function-asin.vala
new file mode 100644
index 00000000..911a9785
--- /dev/null
+++ b/gcalc/gcalc-function-asin.vala
@@ -0,0 +1,54 @@
+/* gcalc-function-asin.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.GFunctionAsin : GFunction {
+
+ public GFunctionAsin () {
+ base ("asin", 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.asin (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 52cbd2c4..b1f92d27 100644
--- a/gcalc/gcalc-function-cos.vala
+++ b/gcalc/gcalc-function-cos.vala
@@ -21,7 +21,7 @@
public class GCalc.GFunctionCos : GFunction {
public GFunctionCos () {
- base ("sqrt", 1);
+ base ("cos", 1);
param_types.add (new GConstant ());
}
diff --git a/gcalc/gcalc-function-exp.vala b/gcalc/gcalc-function-exp.vala
index 4114b74a..bcdadc7c 100644
--- a/gcalc/gcalc-function-exp.vala
+++ b/gcalc/gcalc-function-exp.vala
@@ -21,7 +21,7 @@
public class GCalc.GFunctionExp : GFunction {
public GFunctionExp () {
- base ("sqrt", 1);
+ base ("exp", 1);
param_types.add (new GConstant ());
}
diff --git a/gcalc/gcalc-function-log.vala b/gcalc/gcalc-function-log.vala
index 540e081f..be955f3a 100644
--- a/gcalc/gcalc-function-log.vala
+++ b/gcalc/gcalc-function-log.vala
@@ -21,7 +21,7 @@
public class GCalc.GFunctionLog : GFunction {
public GFunctionLog () {
- base ("sqrt", 1);
+ base ("log", 1);
param_types.add (new GConstant ());
}
diff --git a/gcalc/gcalc-function-sin.vala b/gcalc/gcalc-function-sin.vala
index 031174d0..f7b65642 100644
--- a/gcalc/gcalc-function-sin.vala
+++ b/gcalc/gcalc-function-sin.vala
@@ -21,7 +21,7 @@
public class GCalc.GFunctionSin : GFunction {
public GFunctionSin () {
- base ("sqrt", 1);
+ base ("sin", 1);
param_types.add (new GConstant ());
}
diff --git a/gcalc/gcalc-function-tan.vala b/gcalc/gcalc-function-tan.vala
index 914a1caf..4c1ee722 100644
--- a/gcalc/gcalc-function-tan.vala
+++ b/gcalc/gcalc-function-tan.vala
@@ -21,7 +21,7 @@
public class GCalc.GFunctionTan : GFunction {
public GFunctionTan () {
- base ("sqrt", 1);
+ base ("tan", 1);
param_types.add (new GConstant ());
}
diff --git a/gcalc/gcalc-gmath-equation-manager.vala b/gcalc/gcalc-gmath-equation-manager.vala
index 99143228..9adcf7cc 100644
--- a/gcalc/gcalc-gmath-equation-manager.vala
+++ b/gcalc/gcalc-gmath-equation-manager.vala
@@ -30,6 +30,11 @@ public class GCalc.GMathEquationManager : Object, MathEquationManager {
// Initialize default Functions
functions.add (new GFunctionSqrt ());
functions.add (new GFunctionExp ());
+ functions.add (new GFunctionLog ());
+ functions.add (new GFunctionSin ());
+ functions.add (new GFunctionCos ());
+ functions.add (new GFunctionTan ());
+ functions.add (new GFunctionAsin ());
}
}
diff --git a/gcalc/meson.build b/gcalc/meson.build
index a980c384..727142af 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-asin.vala',
'gcalc-function-cos.vala',
'gcalc-function-exp.vala',
'gcalc-function-log.vala',
diff --git a/tests/gcalc-solving-basic.vala b/tests/gcalc-solving-basic.vala
index 08b2feee..4a7a7036 100644
--- a/tests/gcalc-solving-basic.vala
+++ b/tests/gcalc-solving-basic.vala
@@ -550,6 +550,20 @@ class Tests {
warning ("Error: %s", e.message);
}
});
+ Test.add_func ("/gcalc/solve/function/asin",
+ ()=>{
+ try {
+ var c1 = new GConstant.@double (0.0);
+ var f = new GFunctionTan ();
+ 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);
+ }
+ });
return Test.run ();
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]