Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

max(int, unsigned int) missbehaving #1327

Open
robertwb opened this issue May 10, 2012 · 2 comments
Open

max(int, unsigned int) missbehaving #1327

robertwb opened this issue May 10, 2012 · 2 comments

Comments

@robertwb
Copy link
Contributor

def max_test():
    cdef unsigned int x = 0
    cdef int smid = 4
    print(x,smid - x, -smid, max(smid - x, -smid))

Running this function prints:

(0, 4, -4, 4294967292)

I would expect it to print:

(0, 4, -4, 4)

Although from a C point of view the "worng" result may be the right one. However, then smid - x should be printed as 4294967292.

Either way, note that this behavior causes the fully optimized example on:
http://docs.cython.org/src/tutorial/numpy.html
to break. Presumably this example worked with older cython versions.

Migrated from http://trac.cython.org/ticket/772

@robertwb
Copy link
Contributor Author

scoder changed milestone from 0.16 to wishlist
commented

@robertwb
Copy link
Contributor Author

scoder changed cc to mauro, scoder
commented

Adding the following as a test case to "min_max_optimisation.pyx" makes it fail for me with the error you noted:

def max_partially_unsigned():
    """
    >>> max_partially_unsigned()
    (0, 4, -4, 4)
    """
    cdef unsigned int x = 0
    cdef int smid = 4
    return x, smid - x, -smid, max(smid - x, -smid)

Note that the unexpected last value that you see is not the result of "smid-x" but the unsigned result of "-smid", because the unsigned (and thus wrapped around) result of "-smid" is actually way bigger than the unsigned result of "smid-x", which is just 4.

I'm not sure if this is really a bug. Mixing signed and unsigned integer types is worth the C compiler warning that this code gives me, and using unsigned values to subtract them from signed values mostly screams for trouble. That fact that you seem to have misinterpreted the (certainly non-ovious) result speaks volumes, I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant