# HG changeset patch
# User Vincent Delecroix <20100.delecroix at gmail.com>
# Date 1345570167 -7200
# Node ID f7235cdcbec643df1a88894374d61c29cf4c398c
# Parent 361d834fbcd60757f8d71f636c4c13d5dac1df6a
trac 13386: comparison of Sage integers with numpy integers
diff --git a/sage/rings/integer.pyx b/sage/rings/integer.pyx
a
|
b
|
|
43 | 43 | |
44 | 44 | - Vincent Delecroix (2010-12-28): added unicode in Integer.__init__ |
45 | 45 | |
| 46 | - Vincent Delecroix (2012-08): comparisons of Integer with numpy.integer |
| 47 | |
46 | 48 | EXAMPLES: |
47 | 49 | |
48 | 50 | Add 2 integers:: |
… |
… |
|
837 | 839 | def __richcmp__(left, right, int op): |
838 | 840 | """ |
839 | 841 | cmp for integers |
840 | | |
| 842 | |
841 | 843 | EXAMPLES:: |
842 | | |
| 844 | |
843 | 845 | sage: 2 < 3 |
844 | 846 | True |
845 | 847 | sage: 2 > 3 |
… |
… |
|
855 | 857 | False |
856 | 858 | sage: 1000000000000000000000000000000000000000000000000000.1r==1000000000000000000000000000000000000000000000000000 |
857 | 859 | False |
858 | | |
| 860 | |
859 | 861 | Canonical coercions are used but non-canonical ones are not. |
860 | | |
| 862 | |
861 | 863 | :: |
862 | | |
| 864 | |
863 | 865 | sage: 4 == 4/1 |
864 | | True |
| 866 | True |
865 | 867 | sage: 4 == '4' |
866 | 868 | False |
867 | 869 | |
… |
… |
|
930 | 932 | (False, False, False) |
931 | 933 | sage: a >= n, b >= n, n <= b |
932 | 934 | (False, False, False) |
| 935 | |
| 936 | Verify that trac 13386 was fixed:: |
| 937 | |
| 938 | sage: import numpy |
| 939 | sage: numpy.int32(12) == 12 and 12 == numpy.int32(12) |
| 940 | True |
| 941 | sage: -13 == numpy.int32(-13) and -13 == numpy.int32(-13) |
| 942 | True |
| 943 | sage: numpy.int32(2147483647) == 2147483647 and 2147483647 == numpy.int32(2147483647) |
| 944 | True |
| 945 | sage: numpy.int32(-2147483648) == -2147483648 and -2147483648 == numpy.int32(-2147483648) |
| 946 | True |
933 | 947 | """ |
934 | 948 | cdef int c |
935 | 949 | cdef double d |
… |
… |
|
946 | 960 | if isnan(d): return op == 3 |
947 | 961 | c = mpz_cmp_d((<Integer>left).value, d) |
948 | 962 | else: |
949 | | return (<sage.structure.element.Element>left)._richcmp(right, op) |
950 | | |
| 963 | # added in trac 13386 |
| 964 | # without the following lines we have |
| 965 | # sage: import numpy |
| 966 | # sage: numpy.int32(12) == 12 |
| 967 | # True |
| 968 | # sage: 12 == numpy.int32(12) |
| 969 | # False |
| 970 | import numpy |
| 971 | if PY_TYPE_CHECK(right, numpy.integer): |
| 972 | c = mpz_cmp_si((<Integer>left).value, right.__long__()) |
| 973 | else: |
| 974 | return (<sage.structure.element.Element>left)._richcmp(right, op) |
| 975 | |
951 | 976 | else: # right is an Integer |
952 | 977 | if PyInt_CheckExact(left): |
953 | 978 | c = -mpz_cmp_si((<Integer>right).value, PyInt_AS_LONG(left)) |