The REALLY bad thing about Python lists ..

Russell Wallace rwallace at esatclear.ie
Wed May 24 23:36:15 CEST 2000


Tim Peters wrote:
> 
> [Tim]
> > ISO/ANSI C doesn't fully define what happens when (roughly)
> > integer division or % are handed negative numbers.

> [Russell Wallace]
> > Don't blame the C committee, blame the CPU manufacturers.  C doesn't
> > define the result because CPUs aren't consistent about what result they
> > give.
> 
> That's exactly what makes it critical for languages to define it:  a prime
> charter of most languages is to aid portability, not expose accidents of the
> hardware du jour.

That's one way to look at it, and for a high-level language like Python
it's a reasonable one.  For a systems programming language like C -
whose main design criterion is that language operations should map
directly to machine operations - it's another.  Consider that mandating
the behavior of negative number division will often mean that a simple
arithmetic operation on ints will compile into a section of code with
multiple branch instructions *even in the overwhelmingly most common
case where negative operands will never occur*.

> > (Under what circumstances would you want to use % on negative numbers?
> > I can't think of any.)
> 
> Anytime something "wraps around".  Think circles, like clocks or, well,
> circles <wink>.  If the big hand is pointing at 10 now, where was it
> pointing 45 minutes ago?  At (10-45) % 60 == 25 in Python, undefined in the
> current C, and the systematically unhelpful -35 in C9X.

See, I'd never write the (10-45) % 60 in any language, even if the
language defined the result.  Why?  Because even if the compiler
understands it unambiguously, humans mightn't - different humans
*disagree* on what the logical thing for negative integer division to
return is.  I'd much rather explicitly add 60 and know where I stand.

In fact, if a language is going to mandate anything, my vote would be to
mandate that it is an error for div or mod to have either operand
negative, just as it is an error for the second operand to be zero.

> saving-a-nanosecond-doesn't-help-sell-a-wrong-answer-ly y'rs  - tim

but-if-there's-no-consensus-on-what-the-right-answer-is-then-it-should-be-regarded-as-an-error-ly
y'rs

-- 
"To summarize the summary of the summary: people are a problem."
Russell Wallace
mailto:rwallace at esatclear.ie
http://www.esatclear.ie/~rwallace



More information about the Python-list mailing list