[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Old Man Yells At Cloud

Steve D'Aprano <steve+python at pearwood.info> writes:
> [...] try something more common:
> 1/2
> Most people aren't expecting integer division, but true division, and silently
> returning the wrong result (0 instead of 0.5) is a silent source of
> bugs.

I'm the sure that expectation depends on their background and previous
programming experience, and since I don't know much about most people
when they first write 1/2 in Python, I must conceded that you may be
right.  But is that really the point?  Was the result of 1/2 determined
by a poll to find out what most people expected?  If so, who were these
people -- the result would depend very largely on the selection?

But there is a stronger claim (which I think you also made) that a
floating point result is the correct one.  However, some people with
little experience of floating point arithmetic (I certainly can't say
most but it must be quite few) will expect


to return a tenth.  For /them/, the floating point result is silently
wrong and a source of bugs.  If I were aiming a language at beginners,
I'd make 1/10 be a rational or a type error.

However, I don't think that is Python's main demographic, so 1/10 giving
something not quite one tenth may well be the correct design choice.

> And 1/2 doesn't have to return an int. Why is this such a big deal?

I'm sure it's not deliberate, but 1/2 is a bad example because it can be
exactly represented in the most common implementations.  To get a more
fruitful exchange of views, a division like 1/3 or 1/10 might be a
better example.