[gcalctool] Add integer functions to function popup



commit 5361b8fd79cc22e6147b5c9db31ad71a2520346f
Author: Robert Ancell <robert ancell gmail com>
Date:   Mon Jun 7 11:45:22 2010 +1000

    Add integer functions to function popup

 NEWS               |    6 ++++--
 src/math-buttons.c |    8 ++++++++
 src/mp-equation.c  |   14 +++++++++++++-
 src/mp.c           |   25 +++++++++++++++++++++++++
 src/mp.h           |    5 +++++
 src/unittest.c     |    3 +++
 6 files changed, 58 insertions(+), 3 deletions(-)
---
diff --git a/NEWS b/NEWS
index 3df661e..fba333c 100644
--- a/NEWS
+++ b/NEWS
@@ -8,8 +8,10 @@ Overview of changes in gcalctool 5.31.2
 
     * Support complex trigonometry
 
-    * Add complex argument button to scientific mode, make popup for additional
-      functions
+    * Add complex argument button to scientific mode
+    
+    * make popup for additional functions, with integer/fractional component,
+      floor, ceiling, round and sign functions.
 
 Overview of changes in gcalctool 5.31.2
 
diff --git a/src/math-buttons.c b/src/math-buttons.c
index 7df1288..2e77137 100644
--- a/src/math-buttons.c
+++ b/src/math-buttons.c
@@ -1283,6 +1283,14 @@ function_cb(GtkWidget *widget, MathButtons *buttons)
               N_("Integer Component"), "int " },
             { /* Tooltip for the fractional component button */
               N_("Fractional Component"), "frac " },
+            { /* Tooltip for the round button */
+              N_("Round"), "round " },
+            { /* Tooltip for the floor button */
+              N_("Floor"), "floor " },
+            { /* Tooltip for the ceiling button */
+              N_("Ceiling"), "ceil " },
+            { /* Tooltip for the ceiling button */
+              N_("Sign"), "sgn " },
             { NULL, NULL }
         };
 
diff --git a/src/mp-equation.c b/src/mp-equation.c
index 6bdce64..34f698b 100644
--- a/src/mp-equation.c
+++ b/src/mp-equation.c
@@ -129,10 +129,14 @@ function_is_defined(MPEquationParserState *state, const char *name)
         strcmp(lower_name, "ln") == 0 ||
         strcmp(lower_name, "sqrt") == 0 ||
         strcmp(lower_name, "abs") == 0 ||
+        strcmp(lower_name, "sgn") == 0 ||
         strcmp(lower_name, "arg") == 0 ||
         strcmp(lower_name, "conj") == 0 ||
         strcmp(lower_name, "int") == 0 ||
         strcmp(lower_name, "frac") == 0 ||
+        strcmp(lower_name, "floor") == 0 ||
+        strcmp(lower_name, "ceil") == 0 ||
+        strcmp(lower_name, "round") == 0 ||
         strcmp(lower_name, "re") == 0 ||
         strcmp(lower_name, "im") == 0 ||
         strcmp(lower_name, "sin") == 0 || strcmp(lower_name, "cos") == 0 || strcmp(lower_name, "tan") == 0 ||
@@ -182,14 +186,22 @@ get_function(MPEquationParserState *state, const char *name, const MPNumber *x,
         mp_sqrt(x, z);
     else if (strcmp(lower_name, "abs") == 0) // |x|
         mp_abs(x, z);
+    else if (strcmp(lower_name, "sgn") == 0)
+        mp_sgn(x, z);
     else if (strcmp(lower_name, "arg") == 0)
         mp_arg(x, state->options->angle_units, z);
     else if (strcmp(lower_name, "conj") == 0)
         mp_conjugate(x, z);
     else if (strcmp(lower_name, "int") == 0)
-        mp_floor(x, z);
+        mp_integer_component(x, z);
     else if (strcmp(lower_name, "frac") == 0)
         mp_fractional_component(x, z);
+    else if (strcmp(lower_name, "floor") == 0)
+        mp_floor(x, z);
+    else if (strcmp(lower_name, "ceil") == 0)
+        mp_ceiling(x, z);
+    else if (strcmp(lower_name, "round") == 0)
+        mp_round(x, z);
     else if (strcmp(lower_name, "re") == 0)
         mp_real_component(x, z);
     else if (strcmp(lower_name, "im") == 0)
diff --git a/src/mp.c b/src/mp.c
index 3b7c20c..466c6cb 100644
--- a/src/mp.c
+++ b/src/mp.c
@@ -443,6 +443,31 @@ mp_subtract(const MPNumber *x, const MPNumber *y, MPNumber *z)
 
 
 void
+mp_sgn(const MPNumber *x, MPNumber *z)
+{
+    if (mp_is_zero(x))
+        mp_set_from_integer(0, z);
+    else if (mp_is_negative(x))
+        mp_set_from_integer(-1, z);
+    else
+        mp_set_from_integer(1, z);  
+}
+
+void
+mp_integer_component(const MPNumber *x, MPNumber *z)
+{
+    int i;
+
+    /* Clear fraction */
+    mp_set_from_mp(x, z);
+    for (i = z->exponent; i < MP_SIZE; i++)
+        z->fraction[i] = 0;
+    z->im_sign = 0;
+    z->im_exponent = 0;
+    memset(z->im_fraction, 0, sizeof(int) * MP_SIZE);
+}
+
+void
 mp_fractional_component(const MPNumber *x, MPNumber *z)
 {
     int i, shift;
diff --git a/src/mp.h b/src/mp.h
index 64a8664..ee02201 100644
--- a/src/mp.h
+++ b/src/mp.h
@@ -167,6 +167,11 @@ void   mp_divide_integer(const MPNumber *x, int64_t y, MPNumber *z);
 /* Sets z = 1 ÷ x */
 void   mp_reciprocal(const MPNumber *, MPNumber *);
 
+/* Sets z = sgn(x) */
+void   mp_sgn(const MPNumber *x, MPNumber *z);
+
+void   mp_integer_component(const MPNumber *x, MPNumber *z);
+
 /* Sets z = x mod 1 */
 void   mp_fractional_component(const MPNumber *x, MPNumber *z);
 
diff --git a/src/unittest.c b/src/unittest.c
index 65a662d..c0b391f 100644
--- a/src/unittest.c
+++ b/src/unittest.c
@@ -400,6 +400,9 @@ test_equations()
     test("8 mod 7", "1", 0);
     test("â??1 mod 7", "6", 0);
 
+    test("sgn 0", "0", 0);  
+    test("sgn 3", "1", 0);
+    test("sgn â??3", "â??1", 0);
     test("â??3â??", "3", 0);
     test("â??3â??", "3", 0);
     test("[3]", "3", 0);



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