Python from Wise Guy's Viewpoint

Marcin 'Qrczak' Kowalczyk qrczak at knm.org.pl
Mon Oct 27 15:13:45 EST 2003


On Mon, 27 Oct 2003 13:40:24 -0500, Matthew Danish wrote:

> Something that annoys me about many statically-typed languages is the
> insistence that arithmetic operations should return the same type as the
> operands.  2 / 4 is 1/2, not 0.  Arithmetically, 1 * 1.0 is
> well-defined, so why can I not write this in an SML program?

Confusing integer division with rational division is not a consequence
of static typing, except that with static typing it's not as dangerous as
with dynamic typing (because a function declared as taking floating point
arguments and performing / on them will do the same even if you pass
integers to it, which in most languages will be automatically converted).

But in Haskell / is defined only on types in class Fractional, which
doesn't include integers. Integer division is `div` and `quot` (they
differ for negative numbers). In Pascal 2/4 is 0.5, in SML / is only
for floats, in both languages integer division is spelled div. Most
languages don't support rational numbers, so / on integers can only
return a floating point number or be an error.

Yes, many languages inherited the confusion of divisions from C. This
includes some dynamically typed languages, e.g. Python - but it's planned
to be fixed, and Ruby.

Mixed-type arithmetic is a different story. I'm talking only about 1/2
being equal to 0 in some languages - this doesn't coincide with static
typing.

-- 
   __("<         Marcin Kowalczyk
   \__/       qrczak at knm.org.pl
    ^^     http://qrnik.knm.org.pl/~qrczak/





More information about the Python-list mailing list