[gcalctool] Make boolean operations work with zero, e.g. "0 XOR 5"



commit e6fbebc37d9a48601802896994203649829009fd
Author: Robert Ancell <robert ancell gmail com>
Date:   Mon Feb 22 09:55:20 2010 +1100

    Make boolean operations work with zero, e.g. "0 XOR 5"

 NEWS            |    2 ++
 src/mp-binary.c |   16 ++++++++--------
 src/mp.c        |   10 ++++++++++
 src/mp.h        |    3 +++
 src/unittest.c  |   14 ++++++++++++++
 5 files changed, 37 insertions(+), 8 deletions(-)
---
diff --git a/NEWS b/NEWS
index 889d6a3..439f4a0 100644
--- a/NEWS
+++ b/NEWS
@@ -9,6 +9,8 @@ Overview of changes in gcalctool 5.29.91
     * Fix bug where backspace deletes all input in locales with no thousands
       separator
 
+    * Make boolean operations work with zero, e.g. "0 XOR 5"
+
     * Change boolean functions to uppercase (xor -> XOR)
 
     * Documentation rewritten
diff --git a/src/mp-binary.c b/src/mp-binary.c
index acd4ec9..b57ab46 100644
--- a/src/mp-binary.c
+++ b/src/mp-binary.c
@@ -97,10 +97,10 @@ mp_is_overflow (const MPNumber *x, int wordlen)
 void
 mp_and(const MPNumber *x, const MPNumber *y, MPNumber *z)
 {
-    if (!mp_is_natural(x) || !mp_is_natural(y))
+    if (!mp_is_positive_integer(x) || !mp_is_positive_integer(y))
     {
         /* Translators: Error displayed when boolean AND attempted on non-integer values */
-        mperr(_("Boolean AND is only defined for natural numbers"));
+        mperr(_("Boolean AND is only defined for positive integers"));
     }
 
     mp_bitwise(x, y, mp_bitwise_and, z, 0);
@@ -110,10 +110,10 @@ mp_and(const MPNumber *x, const MPNumber *y, MPNumber *z)
 void
 mp_or(const MPNumber *x, const MPNumber *y, MPNumber *z)
 {
-    if (!mp_is_natural(x) || !mp_is_natural(y))
+    if (!mp_is_positive_integer(x) || !mp_is_positive_integer(y))
     {
         /* Translators: Error displayed when boolean OR attempted on non-integer values */
-        mperr(_("Boolean OR is only defined for natural numbers"));
+        mperr(_("Boolean OR is only defined for positive integers"));
     }
 
     mp_bitwise(x, y, mp_bitwise_or, z, 0);
@@ -123,10 +123,10 @@ mp_or(const MPNumber *x, const MPNumber *y, MPNumber *z)
 void
 mp_xor(const MPNumber *x, const MPNumber *y, MPNumber *z)
 {
-    if (!mp_is_natural(x) || !mp_is_natural(y))
+    if (!mp_is_positive_integer(x) || !mp_is_positive_integer(y))
     {
         /* Translators: Error displayed when boolean XOR attempted on non-integer values */
-        mperr(_("Boolean XOR is only defined for natural numbers"));
+        mperr(_("Boolean XOR is only defined for positive integers"));
     }
 
     mp_bitwise(x, y, mp_bitwise_xor, z, 0);
@@ -138,10 +138,10 @@ mp_not(const MPNumber *x, int wordlen, MPNumber *z)
 {
     MPNumber temp;
 
-    if (!mp_is_natural(x))
+    if (!mp_is_positive_integer(x))
     {
         /* Translators: Error displayed when boolean XOR attempted on non-integer values */
-        mperr(_("Boolean NOT is only defined for natural numbers"));
+        mperr(_("Boolean NOT is only defined for positive integers"));
     }
 
     mp_set_from_integer(0, &temp);
diff --git a/src/mp.c b/src/mp.c
index ee70319..67ecf1f 100644
--- a/src/mp.c
+++ b/src/mp.c
@@ -763,6 +763,16 @@ mp_is_integer(const MPNumber *x)
 
 
 int
+mp_is_positive_integer(const MPNumber *x)
+{
+    if (mp_is_complex(x))
+        return 0;
+    else
+        return x->sign >= 0 && mp_is_integer(x);
+}
+
+
+int
 mp_is_natural(const MPNumber *x)
 {
     if (mp_is_complex(x))
diff --git a/src/mp.h b/src/mp.h
index d771091..3ebcc80 100644
--- a/src/mp.h
+++ b/src/mp.h
@@ -93,6 +93,9 @@ int    mp_is_negative(const MPNumber *x);
 /* Return true if x is integer */
 int    mp_is_integer(const MPNumber *x);
 
+/* Return true if x is a positive integer */
+int    mp_is_positive_integer(const MPNumber *x);
+
 /* Return true if x is a natural number (an integer â?¥ 0) */
 int    mp_is_natural(const MPNumber *x);
 
diff --git a/src/unittest.c b/src/unittest.c
index 0f3988d..15b0059 100644
--- a/src/unittest.c
+++ b/src/unittest.c
@@ -374,8 +374,22 @@ test_parser()
     options.angle_units = MP_GRADIANS;
     test("sin 100", "1", 0);
 
+    test("0 and 0", "0", 0);
+    test("1 and 0", "0", 0);
+    test("0 and 1", "0", 0);
+    test("1 and 1", "1", 0);
     test("3 and 5", "1", 0);
+
+    test("0 or 0", "0", 0);  
+    test("1 or 0", "1", 0);
+    test("0 or 1", "1", 0);  
+    test("1 or 1", "1", 0);
     test("3 or 5", "7", 0);
+
+    test("0 xor 0", "0", 0);
+    test("1 xor 0", "1", 0);
+    test("0 xor 1", "1", 0);
+    test("1 xor 1", "0", 0);
     test("3 xor 5", "6", 0);
 
     base = 16;



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