SmallInteger
Integer values represented as tagged pointers for efficient arithmetic.
SmallInteger is the primary numeric type in Maggie, storing exact integers in the 48-bit range. Arithmetic operators (+, -, *, /, \\, //), comparisons (<, >, <=, >=, =), and iteration (timesRepeat:, to:do:, to:by:do:) are implemented as primitives for performance. When arithmetic overflows SmallInteger range, the result is automatically promoted to BigInteger. The methods below provide higher-level numeric operations built on those primitives.
3 + 4 >>> 7
10 - 3 >>> 7
6 * 7 >>> 42
10 / 3 >>> 3
10 \\ 3 >>> 1
"Smalltalk-style iteration"
| sum <Integer> |
sum := 0.
1 to: 100 do: [:i | sum := sum + i].
sum
Instance Methods
primitives
Return the product of the receiver and the argument.
Return the sum of the receiver and the argument.
Return the difference of the receiver and the argument.
Return the integer quotient of the receiver divided by the argument (truncated toward zero).
Return the integer quotient of the receiver divided by the argument (truncated toward negative infinity).
Return true if the receiver is less than the argument.
Return true if the receiver is less than or equal to the argument.
Return true if the receiver equals the argument.
Return true if the receiver is greater than the argument.
Return true if the receiver is greater than or equal to the argument.
Return the remainder of the receiver divided by the argument.
Return the bitwise AND of the receiver and the argument.
Return the bitwise OR of the receiver and the argument.
Shift the receiver left by count bits (negative count shifts right).
Return the bitwise XOR of the receiver and the argument.
Evaluate block the receiver number of times.
Evaluate block with each integer from the receiver to stop, stepping by step.
Evaluate block with each integer from the receiver to stop, inclusive.
uncategorized
Return the absolute value of the receiver.
-5 abs >>> 5
5 abs >>> 5
0 abs >>> 0
Return true if the receiver is between min and max, inclusive.
5 between: 1 and: 10 >>> true
1 between: 1 and: 10 >>> true
10 between: 1 and: 10 >>> true
0 between: 1 and: 10 >>> false
Return true if the receiver is divisible by 2.
4 even >>> true
7 even >>> false
0 even >>> true
Return the factorial of the receiver (n!). The receiver must be non-negative.
0 factorial >>> 1
1 factorial >>> 1
5 factorial >>> 120
10 factorial >>> 3628800
"Build a table of factorials"
1 to: 8 do: [:n |
(n printString, '! = ', n factorial printString) print
]
Return the greatest common divisor of the receiver and the argument using Euclid's algorithm.
12 gcd: 8 >>> 4
7 gcd: 5 >>> 1
100 gcd: 75 >>> 25
0 gcd: 5 >>> 5
-12 gcd: 8 >>> 4
Return true if the receiver is zero.
0 isZero >>> true
1 isZero >>> false
-1 isZero >>> false
Return the least common multiple of the receiver and the argument.
4 lcm: 6 >>> 12
3 lcm: 5 >>> 15
7 lcm: 7 >>> 7
Return the larger of the receiver and the argument.
3 max: 5 >>> 5
7 max: 2 >>> 7
4 max: 4 >>> 4
Return the smaller of the receiver and the argument.
3 min: 5 >>> 3
7 min: 2 >>> 2
4 min: 4 >>> 4
Return the negation of the receiver.
5 negated >>> -5
-3 negated >>> 3
0 negated >>> 0
Return true if the receiver is less than zero.
-3 negative >>> true
5 negative >>> false
0 negative >>> false
Return true if the receiver is not divisible by 2.
7 odd >>> true
4 odd >>> false
0 odd >>> false
Return true if the receiver is greater than zero.
5 positive >>> true
-3 positive >>> false
0 positive >>> false
Return the decimal string representation of the receiver.
42 printString >>> '42'
-7 printString >>> '-7'
0 printString >>> '0'
Return -1, 0, or 1 indicating the sign of the receiver.
-42 sign >>> -1
0 sign >>> 0
17 sign >>> 1