[Curious] Integer division => fraction

Dan Ingalls Dan at SqueakLand.org
Thu Jun 12 01:38:42 UTC 2003


"Andreas Raab" <andreas.raab at gmx.de> wrote...

>Just had an interesting discussion with Dean (over on e-Lang) about integer
>division semantics. He pointed out (and I agree) that most people tend to
>find it it confusing that "3/4" results in a fraction rather than coercing
>to Float by default.
>
>My questions: What do you think about this issue? Are you personally
>surprised if you get a fraction back? Has it bitten you in unexpected
>places? Have you actually ever had the need for coercing to fractions
>instead of floats? Can you think of "typical" applications that would be
>harmed by coercing to floats?

There is of course no assurance that a simple and consistent design will be what "most people expect".  I mean, most people expect C or Java or Basic.  That said, I think there are interesting underlying issues here.

I think I agree that "most people" think about arithmetic as operator-based, and not as object-oriented (ie dependent on who is receiving the message).  This was NOT how we thought about it when we first asked ourselves "Hmm, here we have a world of objects; how should they behave" back in the early 70's.  In those days (for a while) 3 + 0.14 gave you a different result from 0.14 + 3.  This caused a LOT of confusion, and so we introduced coercion.  The current coercion scheme fits pretty well with what "most people expect" in spite of giving full receiver control over semantics.

On the eve of the Smalltalk-80 release, we had a lot of serious discussion about the various arithmetic operations -- integer division, real division, how modulo works with negative numbers, etc., etc.  While I participated, it was others (Peter, Glenn, Ted, Adele, and anyone with traditional math sensitivity) who made the final decisions on these.  The question here is one of definition.  What does "/" mean?  In St-80 it is supposed to mean "exact" division, not "give me the best you can" (except for Floats who can't do anything else ;-).

However, I think that "most people" have come to accept "give me the best you can" for "/" because "most systems" don't offer fractions and so, in their own desire to approximate what "most people expect" they deliver floating point results.

Now, unfortunately IMO, for "most people" acceptance becomes expectation (a human tendency), while the definition of "/" in Smalltalk has not changed, and 355/113 is not pi, even though in a lot of systems you might think so.  That's the purist perspective, which John Sarkela articulated in a lot less words.  I know the tension you feel, and yet I don't think we have to feel ashamed of our position.

Besides simply remembering and advertising that "/" means exact division, there are a couple of other possibilities, of course.  One is to have a preference to print fractions as floats.  Another is to change "/" to mean produce a float.  If anyone opts for this, then I would certainly  agitate for SOME other operator that means exact division.

I would be interested to hear what the MathMorphs folks would have to say about this, or anyone familiar with, eg, MathLab or the good Lisp math libraries.

	- Dan



More information about the Squeak-dev mailing list