On Mar 13, 2009, at 5:30 AM, Georg S. Weber wrote: > On 12 Mrz., 16:51, Carl Witty <carl.wi...@gmail.com> wrote: >> On Thu, Mar 12, 2009 at 4:16 AM, David Joyner <wdjoy...@gmail.com> >> wrote: >> >>> I have an ignorant question: what are the canonical reps of >>> ZZZ/nZZZ in C? (-n/2,n/2]? >>> Is the issue to decide between the interval [0,n-1] as reps of ZZ/ >>> nZZ (Python) >>> vs (-n/2,n/2] (C)? >> >> In C, ZZ/nZZ does not have canonical representatives. For example, >> the equivalent of [n%4 for n in [-7 .. 7]] would give: >> >> -3, -2, -1, 0, -3, -2, -1, 0, 1, 2, 3, 0, 1, 2, 3 > > Hi Carl, > > the situation might be worse: > AFAIK, that choice ist *not* defined by the ANSI C standard, but left > complier-dependent. In other words: you might get different answers on > the same system using different C compilers, or even the same C > compiler with different options. C is fun, isn't it?
Interestingly enough, the Python sources are written to work no matter what C's behavior. > So the original question turns into: > Do we want to (potentially) sacrifice speed in favor of portability? Yes, I at least want to have that option. Also, on a pragmatic note, Sage has already been programed with this assumption and I would hate to slow it down due to language changes. > If so, we should make the Cython behaviour predictable, i.e. "fixed" > --- favourably identical to the Python behaviour. > And in the Cython build scripts we should check whether we have e.g. > GCC/architecture xyz and set respective options, to not lose speed in > the cases it is not necessary so. > > Cheers, > gsw > >> >> This is annoying to a mathematician. The big reason in favor of this >> is to align with division: both C and Python give a==(a/b)*b+a%b, but >> Python uses floor division and C uses truncating division. >> >> So why does C use truncating division? I'm not sure what the choice >> point was, but at this point two very important reasons would be: >> because that's what the signed integer division instruction gives you >> on basically all processors, and because that's what the C standard >> has specified for years, so people have written code depending on >> that >> behavior. Given these facts, it's IMHO unlikely that C will ever >> change. I would guess C does truncating division because it's easier--one just performs the unsigned division and copies the sign over. - Robert --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-devel@googlegroups.com To unsubscribe from this group, send email to sage-devel-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URLs: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---