@head @module std @title Class std::Int @index Int @implements Comparable @supertypes

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]) @desc 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 @ref{string::IntToStr} can be used to convert integers to strings in different bases. @end @end @see @ref{Float} objects also define operations that accept integer operands. For example, 1.5 + 2 evaluates to 3.5, as expected. @end

Operations

Int objects support the following operations (n below refers to an Int value): @op n + x @optype{Int + Int -> Int; Int + Float -> Float} @desc 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. @end @op n + x @optype{Int + Addable -> T} @desc If x is neither an integer or a float, return x + n (switch operands). @end @op n * x @optype{Int * Int -> Int; Int * Float -> Float} @desc 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. @end @op n * x @optype{Int + Multipliable -> T} @desc If x is neither an integer or a float, return x * n (switch operands). @end @op n - x @optype{Int - Int -> Int; Int - Float -> Float} @desc 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. @end @op n / x @optype{Int / Int -> Float; Int / Float -> Float} @desc Perform a division operation. The right operand x may be an integer or a float. The result will always be a float. @end @op n div x @optype{Int div Int -> Int; Int div Float -> Int} @desc 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. @end @op n mod x @optype{Int mod Int -> Int; Int mod Float -> Float} @desc 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. @example 7 div 3 -- 2 -4 div 3 -- -2 @end @end @op n ** x @optype{Int ** Int -> Int; Int ** Float -> Float} @desc 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:

@end @op -n @optype{-Int -> Int} @desc Return the negation of an integer. @end @op n == x @optype{Int == Object -> Boolean} @desc 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. @end @end @op n < x @optype{Int < Int -> Boolean; Int < Float -> Boolean} @op n > x @optype{Int > Int -> Boolean; Int > Float -> Boolean} @desc 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. @end @end @op Str(n) @desc Convert an integer to a string in base 10 (decimal). @end @op Float(n) @desc Convert an integer to a float. @end @op Hash(n) @desc Return the hash value of an integer. @end