Unsigned arithmatic, ticket #54
authorRobert Bradshaw <robertwb@math.washington.edu>
Sun, 14 Sep 2008 03:29:53 +0000 (20:29 -0700)
committerRobert Bradshaw <robertwb@math.washington.edu>
Sun, 14 Sep 2008 03:29:53 +0000 (20:29 -0700)
Cython/Compiler/PyrexTypes.py
tests/run/unsigned.pyx [new file with mode: 0644]

index 263abb57816028116e19e64e7288340d8b31137f..682c4741bba72c62f535d3d7c99519a184ac2b3d 100644 (file)
@@ -1181,11 +1181,16 @@ def widest_numeric_type(type1, type2):
     # Given two numeric types, return the narrowest type
     # encompassing both of them.
     if type1.is_enum and type2.is_enum:
-        widest_type = c_int_type
-    elif type2.rank > type1.rank:
-        widest_type = type2
+        return c_int_type
+    elif type1 is type2:
+        return type1
+    elif (type1.signed and type2.signed) or (not type1.signed and not type2.signed):
+        if type2.rank > type1.rank:
+            return type2
+        else:
+            return type1
     else:
-        widest_type = type1
+        return sign_and_rank_to_type[min(type1.signed, type2.signed), max(type1.rank, type2.rank)]
     return widest_type
 
 def simple_c_type(signed, longness, name):
diff --git a/tests/run/unsigned.pyx b/tests/run/unsigned.pyx
new file mode 100644 (file)
index 0000000..86d2cda
--- /dev/null
@@ -0,0 +1,19 @@
+__doc__ = """
+    >>> test_signed()
+    3 <type 'int'>
+    9 <type 'long'>
+    6 <type 'long'>
+    12 <type 'long'>
+"""
+
+
+cdef int i = 1
+cdef long l = 2
+cdef unsigned int ui = 4
+cdef unsigned long ul = 8
+
+def test_signed():
+    print i + l, type(i+l)
+    print i + ul, type(i+ul)
+    print ui + l, type(ui+l)
+    print ui + ul, type(ui+ul)