Last updated: 2026-11-15
Numbers in PatLang 2.0: Integers, Intervals and Integers Modulo n
For new readers
PatLang keeps exact arithmetic wherever it can. Integers never overflow: one that leaves the 64-bit range becomes a bigint, and a division that does not come out even gives a rational. PatLang 2.0 fixes where each of those boundaries lies, so both engines agree on every result, and adds two classes: intervals, for computing with ranges of values, and integers modulo n, for clock arithmetic, fixed-width machine words and cryptography.mod n wraps like a clock face
The rules here are sections 1.3, 1.11, 1.12 and 1.14 of the values-and-memory specification. The numeric tower itself, with its floats, bigints, rationals and complex numbers, is older than v2 and is described in PatLang's numeric tower; this page covers what v2 settled and added.
The integer rule FoundationalKnowledge that endures for decades โ core principles
An integer is an int exactly when it fits in a signed 64-bit word, from -263 to 263ย โย 1. Any other integer is a bigint. Every operation applies the rule to its result, in both directions: a sum that leaves the range is a bigint, and a bigint result back inside it is an int. Division of two integers that divides evenly is an int, and otherwise a rational.no silent overflow: it widens to bigint
let big = 9223372036854775807
print(type_of(big))
print(type_of(big + 1))
print(type_of((big + 1) - 1))
print(type_of(10 / 2))
print(10 / 4)
print(type_of(10 / 4))
int
bigint
int
int
5/2
rational
The rule does not depend on how a value is stored. v1 kept integers in 48 bits inside a tagged word, and its interpreter and native code disagreed about where an int stopped; v2's two-word values give an int all 64 bits, and the class is decided by the value alone.
Numbers and bits as functions Applied / MethodologicalKnowledge with a 5โ10 year half-life โ stable practice
powof an exact base to a whole power is exact, and otherwise a float.sin,cos,tan,asin,acos,atan,atan2,logandexpgive floats.ceil,truncandroundare exact for exact numbers, androundtakes a half away from zero.absof a complex number is its modulus.to_fixedrounds the float's exact value, a tie to even.max(a, b)andmin(a, b)give the larger and the smaller of two real numbers, keeping its kind.bnotof an int is its bitwise complement, sobnot nis-1ย -ย n. On anything else it stops the program, asband,bor,bxor,shlandshrdo.
print(pow(2, 10))
print(pow(2, 0.5))
print(round(5 / 2))
print(bnot 0)
print(max(3, 7 / 2))
1024
1.4142135623730951
3
-1
7/2
Intervals Applied / MethodologicalKnowledge with a 5โ10 year half-life โ stable practice
interval_make(lo, hi) gives an interval whose ends are any real numbers with loย <=ย hi; any other call stops the program at its require. interval_lo and interval_hi read the ends. An interval prints as [lo,hi], and stays an interval when its ends are equal.
| Operation | Result |
|---|---|
[a,b] + [c,d] | [a+c, b+d] |
[a,b] - [c,d] | [a-d, b-c] |
[a,b] * [c,d] | From the least to the greatest of the four products |
-[a,b] | [-b, -a] |
| A real operand | Counts as the interval from itself to itself |
Division, %, a complex operand | Stop the program |
== | Both ends equal; a real number equals the interval from itself to itself |
< and its relatives | Do not apply |
let a = interval_make(1, 2)
print(a + interval_make(3, 4))
print(interval_make(0 - 2, 3) * interval_make(4, 5))
print(a + 5)
print(interval_make(1 / 2, 3 / 2) + 1)
print(type_of(interval_hi(a * 9223372036854775807)))
[4,6]
[-10,15]
[6,7]
[3/2,5/2]
bigint
Each end follows the rules of its own numbers, so no rounding is added: rational ends stay rational, and an end that leaves the int range becomes a bigint. That makes v2's intervals exact enclosures over exact numbers, which suits the symbolic maths and interval arithmetic libraries; with float ends, the ends carry ordinary float rounding and are not widened outward.exact intervals avoid the drift that plagues float math
Integers modulo n Applied / MethodologicalKnowledge with a 5โ10 year half-life โ stable practice
mod_make(x, n) gives a modint: the integer x reduced modulo n, with n at least 2. mod_value(m) gives the residue, from 0 to nย โย 1, and mod_modulus(m) gives n. A modint prints as 3 mod 7, and its modulus is fixed when it is made. No operation changes it.
let t = mod_make(9, 12)
print(t)
print(t + 5)
print(type_of(t))
let x = mod_make(37, 101)
print(pow(x, 0 - 1))
print(x * pow(x, 0 - 1))
print(mod_make(5, 101) / x)
print(mod_value(t) + 1)
print(mod_make(t, 5))
9 mod 12
2 mod 12
modint
71 mod 101
1 mod 101
52 mod 101
10
4 mod 5
+,-,*and unary-give a modint with the same modulus. An integer operand counts as the modint it reduces to.a / bisatimes the inverse ofb. Abwith no inverse, one sharing a factor with n, stops the program.pow(m, k)for an integerkismmultiplied by itselfktimes; a negativektakes the inverse's power, andpow(m, 0)is 1 modulo n.- Two modints are equal when their moduli and residues are, and an integer equals a modint when it reduces to it.
mod_make(m, n)of a modintmgives its residue reduced modulo the new n. That is how a value moves between moduli: a byte into a field, or a field element's low byte out of it.- Two modints with different moduli, an operand of another class,
<and its relatives, and%all stop the program, with exit code 70.
Machine words
When n is 2k, for k from 1 to 64, a modint is a k-bit word. band, bor and bxor act on the words; bnot complements all k bits; shl and shr shift by a count from 0 to k, losing the bits shifted out and shifting zeros in; and rotl(m, s) and rotr(m, s) rotate. For any other n, these stop the program.
print(mod_make(4294967295, 4294967296) + 1)
print(rotl(mod_make(1, 4294967296), 31))
print(mod_make(240, 256) bxor 255)
0 mod 4294967296
2147483648 mod 4294967296
15 mod 256
Words of this kind are what hash functions are written in. The specification's scenarios include a SHA-256 written on modint words modulo 232, checked against the four example digests of FIPS 180-41, and the runtime library has SHA-512 on words modulo 264.
A modint also promises something no other class does: its operations take time that depends on its modulus alone, never on its residue. That property, and the compiler check that tests it, have a page of their own: Constant-time arithmetic and the --constant-time check.prevents timing side-channels
See also
PatLang's numeric tower covers the older classes and how they promote. Bitwise operators and bitfields covers the bit operators on ints. Values, objects and operator events covers operators as methods, which is how the number classes implement + and its relatives.
National Institute of Standards and Technology. (2015). Secure Hash Standard (SHS) (FIPS PUB 180-4). U.S. Department of Commerce. https://doi.org/10.6028/NIST.FIPS.180-4 โฉ
Graduate Attributes
Modelled on this page
- Subject Specialist: Technical distinctions between integer types, intervals and modular arithmetic are defined with precise mathematical boundaries.
- Digitally Fluent: The implementation of constant-time arithmetic is linked to cryptographic security requirements for hash functions.
- Critical Thinker: Design choices regarding exact arithmetic are justified by referencing specific specification sections and external standards.
University of Reading Graduate Attributes framework.