Class std::Int

Implements Comparable<Int>

Instances of the Int class are integers. Each integer object is either negative, zero or positive. Their range is essentially unlimited: they can represent both very large and very small integers with thousands of digits.

Integer literals and arithmetic operations are the most common ways of constructing integer objects. Besides arithmetic, integers are also used for indexing strings and arrays.

class Int(x[, base as Int])
Construct an object of the Int type. Call the _int() method of the first argument and return a value equal to the result, provided that it is an integer. Of the primitive types, Str and Float objects provide an _int method. Float arguments are converted to integers by truncating any fractions. String arguments may contain optional blank and tab characters before and after the number, and the number must be in base 10 (decimal) by default.

The base argument is only valid for conversions from strings. It specifies the base of the string, overriding the default base 10. Valid values are from 2 to 36, inclusive. Digit values greater than 9 are represented as letters from "a" to "z", such that "a" or "A" is 10, "b" or "B" is 11, etc.

See also: string::IntToStr can be used to convert integers to strings in different bases.

See also: Float objects also define operations that accept integer operands. For example, 1.5 + 2 evaluates to 3.5, as expected.

Operations

Int objects support the following operations (n below refers to an Int value):

n + x (Int + IntInt; Int + FloatFloat)
Perform an addition operation. If x is an integer, the result is also an integer, and if x is a float, the result is a float.
n + x (Int + Addable<Int, T> ⇒ T)
If x is neither an integer or a float, return x + n (switch operands).
n * x (Int * IntInt; Int * FloatFloat)
Perform a multiplication operation. If x is an integer, the result is also an integer, and if x is a float, the result is a float.
n * x (Int + Multipliable<Int, T> ⇒ T)
If x is neither an integer or a float, return x * n (switch operands).
n - x (Int - IntInt; Int - FloatFloat)
Perform a subtraction operation. If x is an integer, the result is also an integer, and if x is a float, the result is a float.
n / x (Int / IntFloat; Int / FloatFloat)
Perform a division operation. The right operand x may be an integer or a float. The result will always be a float.
n div x (Int div IntInt; Int div FloatInt)
Return n divided by x, rounded down to the nearest integer. If x is an integer, the result is an integer, and if x is a float, the result is a float.
n mod x (Int mod IntInt; Int mod FloatFloat)
Perform a modulus operation. The result is chosen so that (n div x) * x + n mod x == n (the equality is only approximate for a floating point operand x). If x is an integer, the result is an integer, and if x is a float, the result is a float.
7 div 3    -- 2
-4 div 3   -- -2
n ** x (Int ** IntInt; Int ** FloatFloat)
Return nx, i.e., n raised to the xth power. If x is an integer, the result is an integer, and if x is a float, the result is a float. The operator follows the following rules in special conditions:
-n (-IntInt)
Return the negation of an integer.
n == x (Int == ObjectBoolean)
Integers can be compared for equality with arbitrary objects. An integer object is equal to an equivalent integer or float value. If x is neither an integer nor a float, the operation is evaluated as x == n instead.

Note: The last property enables user-defined types to be compared with integers usefully.

n < x (Int < IntBoolean; Int < FloatBoolean)
n > x (Int > IntBoolean; Int > FloatBoolean)
Integers can be compared for order with integers and floats. If x is neither an integer nor a float, the operation is evaluated as x > n (for a < operation) or x < n (for a > operation).

Note: The last property allows user-defined types to be compared with integers usefully.

Str(n)
Convert an integer to a string in base 10 (decimal).
Float(n)
Convert an integer to a float.
Hash(n)
Return the hash value of an integer.