"W. Kent Starr" wrote:
>
>...
>
> Sorry, but I don't see the 'problem' here.
You are certainly entitled to that opinion and many Python people agree
with you. So I don't want to start a flamewar here as we have recently
had in comp.lang.python. I'll just explain Guido's reasoning and you can
determine whether you "buy it" or not.
> Both Ruby (1.63) and Python (1.52)
> exhibit the following behaviour:
>
> 5 / 2 == 2
> 5.0 / 2 == 2.5
>
> So where's the 'problem'? Is this not 'correct' behavior? Is it not right to
> 'assume' that integer objects will do 'integer arithmetic' whilst floating
> point objects do 'floating point' arithmetic?
The way Guido puts it, integers are not really so much of a type as half
a type. After all you can use integers almost anywhere you can use
floats without worrying about the difference. I would guess that this is
also true in Ruby. There are two problems:
The first is that the rules are just non-intuitive to people schooled in
traditional algebra. Once you get to high school you are probably taught
that "remainder division" is something for children. "Real" division is
the "real thing." And then when you get to university you are taught
about "floor division" which is really the same as "remainder" division.
But university professors usually use explicit notation for floor
division so using the float division operator is confusing even for this
crowd. Go ask your average high school or university graduate what 5/2
is without giving them some special context and see what they say.
The second issue is that it is extremely easy to write a module and test
it on float inputs and then have some user of the module accidentally
pass it integer inputs. Integers and floats are not distinct elsewhere
in the language so it isn't surprising that the end-user would expect
them to work with the module. We are trained both in the real world and
in the overall behaviour of our programming languages to think of
integers as a special kind of float -- a subtype.
"The substitutability principle stands for the ability for an object of
a type t to be substituted for another of a super type t 0 , without the
behavior of the whole system being affected."
Integers are *almost* always substitutable for floats. But not always --
because of division.
In statically typed languages the problem does not arise because an
integer passed into a function expecting a float will always,
automatically convert the integer to a float. So substitutability is
always maintained.
--
Take a recipe. Leave a recipe.
Python Cookbook! http://www.ActiveState.com/pythoncookbook