Squeak archeology - quo vs. //
Lex Spoon
lex at cc.gatech.edu
Tue Apr 6 20:17:57 UTC 2004
> Today I got very subtly bitten by // truncating towards negative infinity
> rather than zero, e.g.,
>
> 3//2 -> 1
> -3//2 -> -2
>
> whereas:
>
> 3 quo: 2 -> 1
> -3 quo: 2 -> -1
>
> The reason I got bitten is that the C translator generates // as C / which
> has the semantics of #quo:.
I'm pretty sure that in ANSI C the behavior is implementation defined.
So don't count on it doing one way or the other.
The reason other languages round to negative infinity -- not that I'm
saying whether this is a good idea :) -- is to make // and \\ work
nicely together. That is, \\ can now give a proper remainder even with
negative arguments:
self should: [ a = ((a // b) * b + (a \\ b)) ]
The other way to make // and \\ consistent is to give back negative
answers for \\ sometimes. IMHO that would be even more weird than
having // round towards negative infinity, but indeed that's how quo:
and remainder: manage to work so nicely together.
self should: [ a = ((a quo: b) * b + (a remainder: b)) ]
-Lex
More information about the Squeak-dev
mailing list
|