SmallInteger

Inherits from: Object

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.

Test
3 + 4 >>> 7
10 - 3 >>> 7
6 * 7 >>> 42
10 / 3 >>> 3
10 \\ 3 >>> 1
Example
"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.

bitAnd:

Return the bitwise AND of the receiver and the argument.

bitOr:

Return the bitwise OR of the receiver and the argument.

bitShift:

Shift the receiver left by count bits (negative count shifts right).

bitXor:

Return the bitwise XOR of the receiver and the argument.

primPrintString
timesRepeat:

Evaluate block the receiver number of times.

to:by:do:

Evaluate block with each integer from the receiver to stop, stepping by step.

to:do:

Evaluate block with each integer from the receiver to stop, inclusive.

uncategorized

abs

Return the absolute value of the receiver.

Test
-5 abs >>> 5
5 abs >>> 5
0 abs >>> 0
between:and:

Return true if the receiver is between min and max, inclusive.

Test
5 between: 1 and: 10 >>> true
1 between: 1 and: 10 >>> true
10 between: 1 and: 10 >>> true
0 between: 1 and: 10 >>> false
even

Return true if the receiver is divisible by 2.

Test
4 even >>> true
7 even >>> false
0 even >>> true
factorial

Return the factorial of the receiver (n!). The receiver must be non-negative.

Test
0 factorial >>> 1
1 factorial >>> 1
5 factorial >>> 120
10 factorial >>> 3628800
Example
"Build a table of factorials"
1 to: 8 do: [:n |
    (n printString, '! = ', n factorial printString) print
]
gcd:

Return the greatest common divisor of the receiver and the argument using Euclid's algorithm.

Test
12 gcd: 8 >>> 4
7 gcd: 5 >>> 1
100 gcd: 75 >>> 25
0 gcd: 5 >>> 5
-12 gcd: 8 >>> 4
isZero

Return true if the receiver is zero.

Test
0 isZero >>> true
1 isZero >>> false
-1 isZero >>> false
lcm:

Return the least common multiple of the receiver and the argument.

Test
4 lcm: 6 >>> 12
3 lcm: 5 >>> 15
7 lcm: 7 >>> 7
max:

Return the larger of the receiver and the argument.

Test
3 max: 5 >>> 5
7 max: 2 >>> 7
4 max: 4 >>> 4
min:

Return the smaller of the receiver and the argument.

Test
3 min: 5 >>> 3
7 min: 2 >>> 2
4 min: 4 >>> 4
negated

Return the negation of the receiver.

Test
5 negated >>> -5
-3 negated >>> 3
0 negated >>> 0
negative

Return true if the receiver is less than zero.

Test
-3 negative >>> true
5 negative >>> false
0 negative >>> false
odd

Return true if the receiver is not divisible by 2.

Test
7 odd >>> true
4 odd >>> false
0 odd >>> false
positive

Return true if the receiver is greater than zero.

Test
5 positive >>> true
-3 positive >>> false
0 positive >>> false
printString

Return the decimal string representation of the receiver.

Test
42 printString >>> '42'
-7 printString >>> '-7'
0 printString >>> '0'
sign

Return -1, 0, or 1 indicating the sign of the receiver.

Test
-42 sign >>> -1
0 sign >>> 0
17 sign >>> 1