[gnome-calculator] Added support for Tau (fixes #46)



commit e3d38af7ecab6073af3fa2286b44424b4f31211b
Author: Ángel Guzmán Maeso <shakaran gmail com>
Date:   Thu Jun 4 17:45:07 2020 +0300

    Added support for Tau (fixes #46)

 lib/equation.vala      |  4 +++-
 lib/number.vala        |  6 ++++++
 src/math-display.vala  |  3 +++
 tests/test-number.vala | 14 ++++++++++++++
 vapi/mpfr.vapi         |  7 +++++++
 5 files changed, 33 insertions(+), 1 deletion(-)
---
diff --git a/lib/equation.vala b/lib/equation.vala
index 50fda3a7..ed416458 100644
--- a/lib/equation.vala
+++ b/lib/equation.vala
@@ -166,7 +166,7 @@ private class EquationParser : Parser
     protected override bool variable_is_defined (string name)
     {
         /* FIXME: Make more generic */
-        if (name == "e" || name == "i" || name == "π" || name == "pi")
+        if (name == "e" || name == "i" || name == "π" || name == "pi" || name == "τ" || name == "tau")
             return true;
 
         return equation.variable_is_defined (name);
@@ -180,6 +180,8 @@ private class EquationParser : Parser
             return new Number.i ();
         else if (name == "π" || name == "pi")
             return new Number.pi ();
+        else if (name == "τ" || name == "tau")
+            return new Number.tau ();
         else
             return equation.get_variable (name);
     }
diff --git a/lib/number.vala b/lib/number.vala
index c8f58755..c1d5c741 100644
--- a/lib/number.vala
+++ b/lib/number.vala
@@ -115,6 +115,12 @@ public class Number : Object
         num.get_imag ().val.set_zero ();
     }
 
+    public Number.tau ()
+    {
+        num.get_real ().val.const_tau ();
+        num.get_imag ().val.set_zero ();
+    }
+
     /* Sets z to be a uniform random number in the range [0, 1] */
     public Number.random ()
     {
diff --git a/src/math-display.vala b/src/math-display.vala
index a56a012c..d810b260 100644
--- a/src/math-display.vala
+++ b/src/math-display.vala
@@ -316,6 +316,9 @@ public class MathDisplay : Gtk.Viewport
             case Gdk.Key.p:
                 equation.insert ("π");
                 return true;
+           case Gdk.Key.t:
+                equation.insert ("τ");
+                return true;
             case Gdk.Key.r:
                 equation.insert ("√");
                 return true;
diff --git a/tests/test-number.vala b/tests/test-number.vala
index cb346a4e..3426ce64 100644
--- a/tests/test-number.vala
+++ b/tests/test-number.vala
@@ -150,6 +150,19 @@ private void test_pi ()
     pass ();
 }
 
+private void test_tau ()
+{
+    var z = new Number.tau ();
+    var expected = Math.PI * 2.0;
+    if (!double_matches (z, expected))
+    {
+        fail ("Number.tau () -> %f, expected %f".printf (z.to_double (), expected));
+        return;
+    }
+
+    pass ();
+}
+
 private void test_eulers ()
 {
     var z = new Number.eulers ();
@@ -1081,6 +1094,7 @@ static int main (string[] args)
     test_eulers ();
     test_i ();
     test_pi ();
+    test_tau ();
     //test_random ();
     test_is_zero ();
     test_is_negative ();
diff --git a/vapi/mpfr.vapi b/vapi/mpfr.vapi
index ec484915..52b80441 100644
--- a/vapi/mpfr.vapi
+++ b/vapi/mpfr.vapi
@@ -67,6 +67,13 @@ namespace MPFR {
         public double get_double (Round rnd = Round.NEAREST);
 
         public int const_pi (Round rnd = Round.NEAREST);
+        
+        public int const_tau (Round rnd = Round.NEAREST) {
+            int i = const_pi (rnd);
+            multiply_signed_integer(this, 2, rnd);
+            return i;
+        }
+
         [CCode (cname="mpfr_zero_p")]
         public bool is_zero ();
         public int sgn ();


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