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

Re: Division in Smalltalk



Well, it looks like we've finally come to a decision about the
division and remainder (modulus) operators in Smalltalk-80, so here it
is for everyone to start getting used to it.

/	means "correct division".  If you divide two integers and the
remainder wouldn't be zero, / returns a Fraction (rational
representation in lowest terms).  If you divide two exact numbers
(integers or Fractions), the result loses no information: it is a
Fraction if (and only if) the quotient isn't integral. If you do a
division involving an inexact number (Float), the result is always a
Float.

//	means "division with truncation towards minus infinity".  The
result is always an integer, regardless of what you started with.  a
// b is equivalent to (a / b) floor.

\\	means "remainder with truncation towards minus infinity".  a
\\ b is equivalent to a - ((a // b) * b).  This operation is sometimes
called "modulus": it is, for example, an appropriate operation for
reducing hash values modulo a table size.

quo:	means "division with truncation towards zero".  The result is
always an integer.  a quo: b is equivalent to (a / b) truncate.

rem:	means "remainder with truncation towards zero", i.e. a rem: b is
equivalent to a - ((a quo: b) * b).

This arrangement was worked out in a long and often frustrating
process that involved input from a lot of people.  Steve Putz is
busily implementing it at this very moment.