[gcalctool] Make able to enter floor and ceiling with the keyboard



commit 3e1bef071692b88948fa2898d1a69b472814642e
Author: Robert Ancell <robert ancell gmail com>
Date:   Wed May 12 19:37:23 2010 +0200

    Make able to enter floor and ceiling with the keyboard

 src/math-buttons.c       |    2 +-
 src/math-display.c       |   17 +++++++++++++++++
 src/mp-equation-parser.y |    2 +-
 src/mp.c                 |   42 ++++++++++++++++++++++++++++--------------
 src/mp.h                 |    5 ++++-
 src/unittest.c           |   14 +++++++-------
 6 files changed, 58 insertions(+), 24 deletions(-)
---
diff --git a/src/math-buttons.c b/src/math-buttons.c
index 0078f0f..f67523b 100644
--- a/src/math-buttons.c
+++ b/src/math-buttons.c
@@ -495,7 +495,7 @@ display_changed_cb(MathEquation *equation, GParamSpec *spec, MathButtons *button
             MPNumber max, fraction;
 
             mp_set_from_unsigned_integer(G_MAXUINT64, &max);
-            mp_fractional_component(&x, &fraction);
+            mp_fractional_part(&x, &fraction);
             if (mp_is_negative(&x) || mp_is_greater_than(&x, &max) || !mp_is_zero(&fraction))
                 enabled = FALSE;
             else
diff --git a/src/math-display.c b/src/math-display.c
index 5d5fe8c..915d9df 100644
--- a/src/math-display.c
+++ b/src/math-display.c
@@ -103,6 +103,12 @@ display_key_press_cb(GtkWidget *widget, GdkEventKey *event, MathDisplay *display
     if (state == GDK_CONTROL_MASK) {
         switch(event->keyval)
         {
+        case GDK_bracketleft:
+            math_equation_insert(display->priv->equation, "â??");
+            return TRUE;
+        case GDK_bracketright:
+            math_equation_insert(display->priv->equation, "â??");
+            return TRUE;
         case GDK_e:
             math_equation_insert_exponent(display->priv->equation);
             return TRUE;
@@ -129,6 +135,17 @@ display_key_press_cb(GtkWidget *widget, GdkEventKey *event, MathDisplay *display
              return TRUE;
         }
     }
+    if (state == GDK_MOD1_MASK) {
+        switch(event->keyval)
+        {
+        case GDK_bracketleft:
+            math_equation_insert(display->priv->equation, "â??");
+            return TRUE;
+        case GDK_bracketright:
+            math_equation_insert(display->priv->equation, "â??");
+            return TRUE;
+        }
+    }
 
     if (state == GDK_CONTROL_MASK || math_equation_get_number_mode(display->priv->equation) == SUPERSCRIPT) {
         switch(event->keyval)
diff --git a/src/mp-equation-parser.y b/src/mp-equation-parser.y
index 29e476d..ab2c66a 100644
--- a/src/mp-equation-parser.y
+++ b/src/mp-equation-parser.y
@@ -219,7 +219,7 @@ exp:
 | tLFLOOR exp tRFLOOR {mp_floor(&$2, &$$);}
 | tLCEILING exp tRCEILING {mp_ceiling(&$2, &$$);}
 | '[' exp ']' {mp_round(&$2, &$$);}
-| '{' exp '}' {mp_fractional_component(&$2, &$$);}
+| '{' exp '}' {mp_fractional_part(&$2, &$$);}
 | '|' exp '|' {mp_abs(&$2, &$$);}
 | exp '^' exp {mp_xpowy(&$1, &$3, &$$);}
 | exp tSUPNUM {mp_xpowy_integer(&$1, $2, &$$);}
diff --git a/src/mp.c b/src/mp.c
index 2533c89..331ea9b 100644
--- a/src/mp.c
+++ b/src/mp.c
@@ -450,8 +450,15 @@ mp_fractional_component(const MPNumber *x, MPNumber *z)
     }
     if (z->fraction[0] == 0)
         z->sign = 0;
-    if (z->sign < 0)
-       z->sign = -z->sign;
+}
+
+
+void
+mp_fractional_part(const MPNumber *x, MPNumber *z)
+{
+    MPNumber f;
+    mp_floor(x, &f);
+    mp_subtract(x, &f, z);
 }
 
 
@@ -459,6 +466,7 @@ void
 mp_floor(const MPNumber *x, MPNumber *z)
 {
     int i;
+    bool have_fraction = false;
 
     /* Integer component of zero = 0 */
     if (mp_is_zero(x)) {
@@ -474,8 +482,14 @@ mp_floor(const MPNumber *x, MPNumber *z)
 
     /* Clear fraction */
     mp_set_from_mp(x, z);
-    for (i = z->exponent; i < MP_SIZE; i++)
+    for (i = z->exponent; i < MP_SIZE; i++) {
+        if (z->fraction[i])
+            have_fraction = true;
         z->fraction[i] = 0;
+    }
+
+    if (have_fraction && mp_is_negative(x))
+       mp_add_integer(z, -1, z);
 }
 
 
@@ -488,11 +502,7 @@ mp_ceiling(const MPNumber *x, MPNumber *z)
     mp_fractional_component(x, &f);
     if (mp_is_zero(&f))
         return;
-
-    if (mp_is_negative(x))
-       mp_add_integer(z, -1, z);
-    else
-       mp_add_integer(z, 1, z);
+    mp_add_integer(z, 1, z);
 }
 
 
@@ -500,18 +510,22 @@ void
 mp_round(const MPNumber *x, MPNumber *z)
 {
     MPNumber f, one;
+    bool do_floor;
+
+    do_floor = !mp_is_negative(x);
 
-    mp_floor(x, z);
     mp_fractional_component(x, &f);
     mp_multiply_integer(&f, 2, &f);
+    mp_abs(&f, &f);
     mp_set_from_integer(1, &one);
-    if (mp_is_less_than(&f, &one))
-        return;
+    if (mp_is_greater_equal(&f, &one))
+        do_floor = !do_floor;
 
-    if (mp_is_negative(x))
-       mp_add_integer(z, -1, z);
+    if (do_floor)
+        mp_floor(x, z);
     else
-       mp_add_integer(z, 1, z);   
+        mp_ceiling(x, z);
+
 }
 
 int
diff --git a/src/mp.h b/src/mp.h
index 2e356dd..de9ccf6 100644
--- a/src/mp.h
+++ b/src/mp.h
@@ -167,9 +167,12 @@ void   mp_divide_integer(const MPNumber *x, int64_t y, MPNumber *z);
 /* Sets z = 1 ÷ x */
 void   mp_reciprocal(const MPNumber *, MPNumber *);
 
-/* Sets z = {x} */
+/* Sets z = x mod 1 */
 void   mp_fractional_component(const MPNumber *x, MPNumber *z);
 
+/* Sets z = {x} */
+void   mp_fractional_part(const MPNumber *x, MPNumber *z);
+
 /* Sets z = â??xâ?? */
 void   mp_floor(const MPNumber *x, MPNumber *z);
 
diff --git a/src/unittest.c b/src/unittest.c
index 3ea51a2..71bee9c 100644
--- a/src/unittest.c
+++ b/src/unittest.c
@@ -410,23 +410,23 @@ test_equations()
     test("â??3.2â??", "3", 0);
     test("â??3.2â??", "4", 0);
     test("[3.2]", "3", 0);
-    test("â??â??3.2â??", "â??3", 0);
-    test("â??â??3.2â??", "â??4", 0);
+    test("â??â??3.2â??", "â??4", 0);
+    test("â??â??3.2â??", "â??3", 0);
     test("[â??3.2]", "â??3", 0);
     test("â??3.5â??", "3", 0);
     test("â??3.5â??", "4", 0);
     test("[3.5]", "4", 0);
-    test("â??â??3.5â??", "â??3", 0);
-    test("â??â??3.5â??", "â??4", 0);
+    test("â??â??3.5â??", "â??4", 0);
+    test("â??â??3.5â??", "â??3", 0);
     test("[â??3.5]", "â??4", 0);
     test("â??3.7â??", "3", 0);
     test("â??3.7â??", "4", 0);
     test("[3.7]", "4", 0);
-    test("â??â??3.7â??", "â??3", 0);
-    test("â??â??3.7â??", "â??4", 0);
+    test("â??â??3.7â??", "â??4", 0);
+    test("â??â??3.7â??", "â??3", 0);
     test("[â??3.7]", "â??4", 0);
     test("{3.2}", "0.2", 0);
-    test("{â??3.2}", "0.2", 0);
+    test("{â??3.2}", "0.8", 0);
 
     test("|1|", "1", 0);
     test("|â??1|", "1", 0);



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