--- a/LargeInteger.st Fri Sep 02 16:44:53 2016 +0200
+++ b/LargeInteger.st Fri Sep 02 16:45:06 2016 +0200
@@ -499,14 +499,15 @@
The result is truncated toward negative infinity
and will be negative, if the operands signs differ.
The following is always true:
- (receiver // aNumber) * aNumber + (receiver \\ aNumber) = receiver
+ (receiver // aNumber) * aNumber + (receiver \\ aNumber) = receiver
Be careful with negative results: 9 // 4 -> 2, while -9 // 4 -> -3.
- Especially surprising:
- -1 // 10 -> -1 (because -(1/10) is truncated towards next smaller integer, which is -1.
- -10 // 3 -> -4 (because -(10/3) is truncated towards next smaller integer, which is -4.
-
- See #quo: which returns -2 in the above case and #rem: which is the corresponding remainder."
+ Especially surprising (because of truncation toward negative infinity):
+ -1 // 10 -> -1 (because -(1/10) is truncated towards next smaller integer, which is -1.
+ -10 // 3 -> -4 (because -(10/3) is truncated towards next smaller integer, which is -4.
+
+ See #quo: which truncates toward zero and returns -2 in the above case
+ and #rem: which is the corresponding remainder."
|nrClass divMod quo|
@@ -517,19 +518,19 @@
Use a special method for this case ...
"
((nrClass == SmallInteger) or:[ nrClass == self class]) ifFalse:[
- ^ self retry:#// coercing:aNumber
+ ^ self retry:#// coercing:aNumber
].
divMod := self absDivMod:aNumber.
quo := divMod at:1.
(sign == aNumber sign) ifFalse:[
- "/ adjust for truncation if negative and there is a remainder ...
- "/ be careful: there is one special case to care for here:
- "/ if quo is maxInt+1, the negation can be represented as a smallInt.
- quo := quo setSign:-1.
- (divMod at:2) == 0 ifFalse:[
- ^ quo - 1
- ].
+ "/ adjust for truncation if negative and there is a remainder ...
+ "/ be careful: there is one special case to care for here:
+ "/ if quo is maxInt+1, the negation can be represented as a smallInt.
+ quo := quo setSign:-1.
+ (divMod at:2) == 0 ifFalse:[
+ ^ quo - 1
+ ].
"/ quo digitLength == SmallInteger maxBytes ifTrue:[
"/ ^ quo compressed
"/ ].
@@ -798,19 +799,22 @@
truncates toward negative infinity).
The result's sign is negative if the receiver has a sign different from the arg's sign.
The following is always true:
- (receiver quo: aNumber) * aNumber + (receiver rem: aNumber) = receiver
- "
+ (receiver quo: aNumber) * aNumber + (receiver rem: aNumber) = receiver
+ For positive results, this is the same as #//,
+ for negative results, the remainder is ignored.
+ I.e.: '9 // 4 = 2' and '-9 // 4 = -3'
+ in contrast: '9 quo: 4 = 2' and '-9 quo: 4 = -2'"
|nrClass quo |
nrClass := aNumber class.
((nrClass == SmallInteger) or:[ nrClass == self class] ) ifFalse:[
- ^ self retry:#quo: coercing:aNumber
+ ^ self retry:#quo: coercing:aNumber
].
quo := (self absDivMod:aNumber) at:1.
(sign == aNumber sign) ifTrue:[
- ^ quo
+ ^ quo
].
^ quo setSign:-1