Class int
A built-in type for integers.
Remarks
Signed 64-bit integer type. This means that it can take values from -2^63
to 2^63 - 1
, i.e. from -9223372036854775808
to 9223372036854775807
. When it exceeds these bounds, it will wrap around.
ints can be automatically converted to floats when necessary, for example when passing them as arguments in functions. The float will be as close to the original integer as possible.
Likewise, floats can be automatically converted into ints. This will truncate the float, discarding anything after the floating-point.
Note: In a boolean context, an int will evaluate to false
if it equals 0
, and to true
otherwise.
var x: int = 1 # x is 1
x = 4.2 # x is 4, because 4.2 gets truncated
var max_int = 9223372036854775807 # Biggest value an int can store
max_int += 1 # max_int is -9223372036854775808, because it wrapped around
You can use the 0b
literal for binary representation, the 0x
literal for hexadecimal representation, and the _
symbol to separate long numbers and improve readability.
Constructors
int
Constructs an int set to 0
.
int int
int(int)
Constructs an int as a copy of the given int.
int int(int from)
Parameters
from
int
int(String)
int int(String from)
Parameters
from
String
int(bool)
Constructs a new int from a bool. true
is converted to 1
and false
is converted to 0
.
int int(bool from)
Parameters
from
bool
int(float)
Constructs a new int from a float. This will truncate the float, discarding anything after the floating point.
int int(float from)
Parameters
from
float
Operators
!= (float)
Returns true
if the int is not equivalent to the float.
bool != (float right)
Parameters
right
float
!= (int)
Returns true
if the ints are not equal.
bool != (int right)
Parameters
right
int
% (int)
Returns the remainder after dividing two ints. Uses truncated division, which returns a negative number if the dividend is negative. If this is not desired, consider using @GlobalScope.posmod.
print(6 % 2) # Prints 0
print(11 % 4) # Prints 3
print(-5 % 3) # Prints -2
int % (int right)
Parameters
right
int
& (int)
Performs the bitwise AND
operation.
print(0b1100 & 0b1010) # Prints 8 (binary 1000)
This is useful for retrieving binary flags from a variable.
var flags = 0b101
# Check if the first or second bit are enabled.
if flags & 0b011:
do_stuff() # This line will run.
int & (int right)
Parameters
right
int
* (Color)
Multiplies each component of the Color by the int.
Color * (Color right)
Parameters
right
Color
* (Quaternion)
Multiplies each component of the Quaternion by the int. This operation is not meaningful on its own, but it can be used as a part of a larger expression.
Quaternion * (Quaternion right)
Parameters
right
Quaternion
* (Vector2)
Multiplies each component of the Vector2 by the int.
print(2 * Vector2(1, 4)) # Prints (2, 8)
Vector2 * (Vector2 right)
Parameters
right
Vector2
* (Vector2i)
Multiplies each component of the Vector2i by the int.
Vector2i * (Vector2i right)
Parameters
right
Vector2i
* (Vector3)
Multiplies each component of the Vector3 by the int.
Vector3 * (Vector3 right)
Parameters
right
Vector3
* (Vector3i)
Multiplies each component of the Vector3i by the int.
Vector3i * (Vector3i right)
Parameters
right
Vector3i
* (Vector4)
Multiplies each component of the Vector4 by the int.
Vector4 * (Vector4 right)
Parameters
right
Vector4
* (Vector4i)
Multiplies each component of the Vector4i by the int.
Vector4i * (Vector4i right)
Parameters
right
Vector4i
* (float)
float * (float right)
Parameters
right
float
* (int)
Multiplies the two ints.
int * (int right)
Parameters
right
int
** (float)
Raises an int to a power of a float. The result is a float.
print(2 ** 0.5) # Prints 1.4142135623731
float ** (float right)
Parameters
right
float
** (int)
Raises the left int to a power of the right int.
print(3 ** 4) # Prints 81
int ** (int right)
Parameters
right
int
+ (float)
float + (float right)
Parameters
right
float
+ (int)
Adds the two ints.
int + (int right)
Parameters
right
int
- (float)
float - (float right)
Parameters
right
float
- (int)
Subtracts the two ints.
int - (int right)
Parameters
right
int
/ (float)
float / (float right)
Parameters
right
float
/ (int)
Divides the two ints. The result is an int. This will truncate the float, discarding anything after the floating point.
print(6 / 2) # Prints 3
print(5 / 3) # Prints 1
int / (int right)
Parameters
right
int
< (float)
Returns true
if the int is less than the float.
bool < (float right)
Parameters
right
float
< (int)
Returns true
if the left int is less than the right int.
bool < (int right)
Parameters
right
int
<< (int)
Performs the bitwise shift left operation. Effectively the same as multiplying by a power of 2.
print(0b1010 << 1) # Prints 20 (binary 10100)
print(0b1010 << 3) # Prints 80 (binary 1010000)
int << (int right)
Parameters
right
int
<= (float)
Returns true
if the int is less than or equal to the float.
bool <= (float right)
Parameters
right
float
<= (int)
Returns true
if the left int is less than or equal to the right int.
bool <= (int right)
Parameters
right
int
== (float)
Returns true
if the int is equal to the float.
bool == (float right)
Parameters
right
float
== (int)
Returns true
if the two ints are equal.
bool == (int right)
Parameters
right
int
> (float)
Returns true
if the int is greater than the float.
bool > (float right)
Parameters
right
float
> (int)
Returns true
if the left int is greater than the right int.
bool > (int right)
Parameters
right
int
>= (float)
Returns true
if the int is greater than or equal to the float.
bool >= (float right)
Parameters
right
float
>= (int)
Returns true
if the left int is greater than or equal to the right int.
bool >= (int right)
Parameters
right
int
>> (int)
Performs the bitwise shift right operation. Effectively the same as dividing by a power of 2.
print(0b1010 >> 1) # Prints 5 (binary 101)
print(0b1010 >> 2) # Prints 2 (binary 10)
int >> (int right)
Parameters
right
int
^ (int)
Performs the bitwise XOR
operation.
print(0b1100 ^ 0b1010) # Prints 6 (binary 110)
int ^ (int right)
Parameters
right
int
unary+
Returns the same value as if the +
was not there. Unary +
does nothing, but sometimes it can make your code more readable.
int unary+
unary-
Returns the negated value of the int. If positive, turns the number negative. If negative, turns the number positive. If zero, does nothing.
int unary-
| (int)
Performs the bitwise OR
operation.
print(0b1100 | 0b1010) # Prints 14 (binary 1110)
This is useful for storing binary flags in a variable.
var flags = 0
flags |= 0b101 # Turn the first and third bits on.
int | (int right)
Parameters
right
int
~
Performs the bitwise NOT
operation on the int. Due to 2's complement, it's effectively equal to -(int + 1)
.
print(~4) # Prints -5
print(~(-7)) # Prints 6
int ~