Integers
Integers in Ren'Py and Python are whole numbers of unlimited precision.
Obvious uses are counting things, but they can also be used for state
tracking.
See
Python 3 Numeric types: int
They aren't tied to a hardware specific number of bits which is quite
unusual if you've come from other languages that where an int is a
primitive type. Integers, like everything else in Python, are objects.
There's no equivalent to MAX_INT
or Integer.MAX
to worry about.
Division
While all the usual mathematical operators work as you'd expect with integers we need to talk about division. Integer division works differently in Python 2 and Python 3, and thus between Ren'Py 7.x and Ren'Py 8.x.
Ren'Py 7 / Python 2
Dividing one integer by another using /
gives an integer
result, as does using //
. The result is always rounded
down to negative infinity. If you need a floating point result you need
to convert one of the operands to a float first.
> 3 / 2 1 > 3 // 2 1 > (-3) / 2 -2 > float(3) / 2 1.5
Ren'Py 8 / Python 3
Dividing one integer by another using /
always gives a
floating point result.
Dividing one integer by another using //
gives an integer
result. The result is always rounded
down to negative infinity.
> 3 / 2 1.5 > 3 // 2 1 > (-3) / 2 -1.5 > (-3) // 2 -2
To be compatible across both versions use the//
operator for integer division. For compatible floating point division usefloat(x) / y
.
Modulus
The modulus operator %
works alongside the integer division
operator //
. For positive dividends this is the same as
the remainder:
> 5 // 3 1 > 5 % 3 2
If you need both the result of a division and the modulus then Python
has the divmod
function:
dollars, cents = divmod(costCents, 100)
Bitwise operations
Bitwise operations are only available on integers. These work as though on a two's complement version of the number, with as many bits as needed. In a visual novel these are likely only useful for puzzles.
Operation | Meaning |
---|---|
~x | bitwise inversion of x |
x << n |
shift the bits of x left by n bits
- multiplication by 2n |
x >> n |
shift the bits of x right by n bits
- integer division by 2n |
x & y | bitwise and of x and y |
x | y | bitwise or of x and y |
x ^ y | bitwise exclusive-or of x and y |
Integer Literals
Integer literals can be specified in decimal, binary, octal, or hexadecimal.
See
Integer literals
(Python 2)
Base | Notes | Examples |
---|---|---|
Decimal | Uses the digits 0..9. Non zero numbers cannot begin with zero. Underscores can be used as separators, but are just ignored. |
|
Binary |
Start with 0b or 0B .
Uses the digits 0 and 1.
Underscores can be used as separators, but are just ignored.
|
|
Octal |
Start with 0o or 0O .
Uses the digits 0..7.
Underscores can be used as separators, but are just ignored.
Note: in Python 2 a non-zero number starting with a
zero is treated as octal.
|
|
Hexadecimal |
Start with 0x or 0X .
Uses the digits 0..9 and the letters a..f or A..F.
Underscores can be used as separators, but are just ignored.
|
|
Note: Python 2 allows a postfix of l
or
L
to indicate a value larger than a 32-bit integer,
but this is largely redundant as values outside this range are
automatically treated as being "long".