.\" Made by website/plain/build.mjs from website/words/int.md; change that file. .TH SG-INT 7 2026-11-28 "SG" "SG reference" .SH NAME int \- an integer of 32 bits, with a sign; a whole number is one when nothing else is asked for .SH SYNOPSIS .TP \fBint\fR an integer of 32 bits, from -2 147 483 648 to 2 147 483 647 .TP \fB42, 0xff, 0b1010, 1000000\fR a number written in the program: an int where nothing else is asked for .TP \fBint.MinValue, int.MaxValue\fR the two ends .TP \fBa + b, a \- b, a * b, a / b, a % b\fR arithmetic in int; / cuts toward zero .TP \fB(int)x\fR a value of another number type, as an int .TP \fBx.tryAdd(y), x.wrappingAdd(y), x.saturatingAdd(y)\fR arithmetic that says what an overflow does .TP \fBx.tryToInt(), x.wrappingToInt(), x.saturatingToInt()\fR a conversion to int that never panics .TP \fBint.parse(text), int.parseHex(text)\fR the number a text writes, as a Result .TP \fB$"{n:x8}", $"{n:D3}", $"{n,6}"\fR an int written in hex, with zeros in front, in a width .SH DESCRIPTION An \fBint\fR is a signed integer of 32 bits, on every machine. It is the usual type for a count, a code or a small sum. A number written in the program takes the type of the place it goes to (\fBbyte b = 200\fR, \fBlong total = 0\fR); where no place says a type, as in \fBvar n = 5\fR, it is an \fBint\fR if it fits and a \fBlong\fR if it does not. .PP An operation on two \fBint\fRs gives an \fBint\fR\&. An \fBint\fR becomes a wider type on its own where that type holds every one of its values: \fBlong\fR, \fBnint\fR and \fBdouble\fR\&. Every other way is written as a cast, \fB(byte)n\fR or \fB(uint)n\fR, and so is the way back, \fB(int)total\fR\&. A number never becomes an \fBint\fR on its own from a \fBlong\fR, a \fBdouble\fR or a length. .PP Going past an end is an overflow. In the interpreter, in tests and in a debug build it stops the program, with the values and the methods that do not stop it; a release build (\fBsg build \-\-release\fR) wraps around instead, as the machine does. A cast whose value does not fit follows the same rule. A division by zero, and \fBint.MinValue / \-1\fR, stop the program in every build. Where the program wants one behaviour in every build, a method says it: \fBtryAdd\fR gives \fBnull\fR, \fBwrappingAdd\fR wraps, \fBsaturatingAdd\fR stops at the end; the same for \fBSubtract\fR and \fBMultiply\fR, and \fBtryDivide\fR\&. .PP Lengths and positions are \fBnint\fR, not \fBint\fR: \fBlist.length()\fR gives an \fBnint\fR\&. An \fBint\fR is an index as it is, since it widens; a length becomes an \fBint\fR with \fB(int)\fR\&. .PP The methods are in Numbers. .SH EXAMPLES .SS "Numbers as written" .RS 4 .nf void main() { var count = 42 var mask = 0xff var flags = 0b1010 var million = 1_000_000 var big = 3_000_000_000 print($"{count} {mask} {flags} {million} {big}") print($"{int.MinValue} {int.MaxValue}") } .fi .RE .PP .RS 4 .nf 42 255 10 1000000 3000000000 -2147483648 2147483647 .fi .RE .PP The first four are \fBint\fRs. \fBbig\fR does not fit in one, so it is a \fBlong\fR\&. .SS "Division and remainder" .RS 4 .nf void main() { int items = 17 int perBox = 5 print($"{items / perBox} boxes, {items % perBox} left") print($"{-17 / 5} {-17 % 5}") double half = (double)(items / 2) double exact = items / 2.0 print($"{half} {exact}") } .fi .RE .PP .RS 4 .nf 3 boxes, 2 left -3 -2 8 8.5 .fi .RE .PP \fB/\fR drops the fraction toward zero, and \fB%\fR takes the sign of the left side. \fBitems / 2\fR is divided as two \fBint\fRs; where a \fBdouble\fR goes, the cast around it says the fraction is dropped on purpose (without it the line is refused). With \fB2.0\fR the division is a \fBdouble\fR\&'s. .SS "Wider on its own, narrower with a cast" .RS 4 .nf long square(long x) { return x * x } void main() { int side = 100000 print(square(side)) var scores = List { 70, 85, 92 } int total = 0 for s in read scores { total += s } int count = (int)scores.length() print($"{total / count} {scores[count - 1]}") print($"{(int)7.9} {(int)-7.9} {(int)'A'} {(byte)count}") } .fi .RE .PP .RS 4 .nf 10000000000 82 92 7 -7 65 3 .fi .RE .PP \fBside\fR becomes a \fBlong\fR on its way into \fBsquare\fR, so the product does not overflow. \fBlength()\fR gives an \fBnint\fR, which becomes an \fBint\fR with a cast; \fBcount \- 1\fR is an index as it is. A cast from a fraction drops what is after the point. .SS "One behaviour in every build" .RS 4 .nf int? total(read List parts) { int sum = 0 for p in parts { sum = sum.tryAdd(p) ?? return null } return sum } void main() { int top = int.MaxValue print($"{top.wrappingAdd(1)} {top.saturatingAdd(1)} {top.tryAdd(1) ?? 0}") print($"{(-5).saturatingMultiply(top)} {10.tryDivide(0) ?? -1}") var small = List { 1, 2, 3 } var large = List { top, 1 } print(total(read small) ?? -1) print(total(read large) ?? -1) } .fi .RE .PP .RS 4 .nf -2147483648 2147483647 0 -2147483648 -1 6 -1 .fi .RE .PP \fBtryAdd\fR gives \fBnull\fR where \fB+\fR would overflow, and \fB?? return null\fR hands that on. These methods do the same in the interpreter, in a debug build and in a release build. .SS "Into an int, without a panic" .RS 4 .nf void main() { long small = 1234 long big = 5_000_000_000 print($"{small.tryToInt() ?? -1} {big.tryToInt() ?? -1}") print($"{big.wrappingToInt()} {big.saturatingToInt()}") double huge = 1e12 print($"{huge.tryToInt() ?? -1} {huge.saturatingToInt()}") } .fi .RE .PP .RS 4 .nf 1234 -1 705032704 2147483647 -1 2147483647 .fi .RE .PP \fBtryToInt\fR gives \fBnull\fR where \fB(int)\fR would not fit, \fBwrappingToInt\fR keeps the low 32 bits, and \fBsaturatingToInt\fR gives the nearest end. .SS "Read from text" .RS 4 .nf void main() { var line = "12, 7, x9, 30, 99999999999" int sum = 0 for field in line.split(',') { var text = field.trim() match int.parse(text) { Ok(n) => sum += n Error(Invalid(at)) => print($"{text}: not a number from byte {at}") Error(OutOfRange) => print($"{text}: too large for an int") } } print(sum) print(int.parseHex("7fffffff") ?? 0) } .fi .RE .PP .RS 4 .nf x9: not a number from byte 0 99999999999: too large for an int 49 2147483647 .fi .RE .PP \fBparse\fR reads a sign and digits and nothing else, so the spaces are trimmed first. \fBparseHex\fR reads hex digits without \fB0x\fR\&. .SS "Written into text" .RS 4 .nf void main() { int code = 48879 int day = 7 print($"{code:x} {code:X8} {code:b}") print($"{day:D3} [{day,4}] [{day,-4}]") print($"{-1:x}") print(code.toString().length()) } .fi .RE .PP .RS 4 .nf beef 0000BEEF 1011111011101111 007 [ 7] [7 ] ffffffff 5 .fi .RE .PP \fBx\fR and \fBX\fR write hex, \fBb\fR binary and \fBD\fR decimal, each with at least so many digits. A width after a comma pads with spaces, in front or, when it is negative, after. A negative number in hex is written as its 32 bits. .SS "A release build wraps" The file wrap/main.sg: .PP .RS 4 .nf int add(int a, int b) { return a + b } void main() { print(add(int.MaxValue, 1)) } .fi .RE .PP .RS 4 .nf $ cd wrap $ sg main.sg panic: overflow: 2147483647 + 1 does not fit in int --> main.sg:2:12 2 | return a + b | ^^^^^ hint: `tryAdd` gives `null` instead, and `wrappingAdd` wraps around $ sg build --release main.sg -o add $ ./add -2147483648 .fi .RE .PP A release build leaves the check out of every \fB+\fR, \fB\-\fR and \fB*\fR, and the number wraps around. The wrapping is defined, two's complement, and an overflow cannot make memory unsafe, since every index is still checked. Where an overflow is a real case, \fBtryAdd\fR says so in every build. .SH "WHAT STOPS THE PROGRAM" .SS "An overflow" .RS 4 .nf int area(int width, int height) { return width * height } void main() { print(area(70000, 70000)) } .fi .RE .PP .RS 4 .nf panic: overflow: 70000 * 70000 does not fit in int --> example.sg:2:12 2 | return width * height | ^^^^^^^^^^^^^^ hint: `tryMultiply` gives `null` instead, and `wrappingMultiply` wraps around .fi .RE .PP In a release build the product wraps. A \fBlong\fR holds it: \fB(long)width * height\fR\&. .SS "A division by zero" .RS 4 .nf int perHour(int count, int hours) { return count / hours } void main() { print(perHour(120, 0)) } .fi .RE .PP .RS 4 .nf panic: division by zero: 120 / 0 --> example.sg:2:12 2 | return count / hours | ^^^^^^^^^^^^^ .fi .RE .PP This stops the program in every build, release too; \fBtryDivide\fR gives \fBnull\fR instead. \fB%\fR by zero stops it the same way. .SS "A cast that does not fit" .RS 4 .nf int toInt(long value) { return (int)value } void main() { print(toInt(5_000_000_000)) } .fi .RE .PP .RS 4 .nf panic: value 5000000000 does not fit in int --> example.sg:2:12 2 | return (int)value | ^^^^^^^^^^ hint: `tryToInt()` gives `null` when the value does not fit, and `wrappingToInt()` cuts it to size .fi .RE .PP In a release build the cast keeps the low 32 bits, as \fBwrappingToInt()\fR does in every build. .SH "WHAT THE COMPILER REFUSES" .SS "A number too large" .RS 4 .nf void main() { int population = 8_100_000_000 print(population) } .fi .RE .PP .RS 4 .nf error: `8_100_000_000` does not fit in int --> example.sg:2:22 2 | int population = 8_100_000_000 | ^^^^^^^^^^^^^ hint: a `long` holds it: `long population = 8_100_000_000` .fi .RE .PP \fBlong population\fR holds it. .SS "A long where an int goes" .RS 4 .nf void main() { long total = 10 int count = total print(count) } .fi .RE .PP .RS 4 .nf error: expected `int`, found `long` --> example.sg:3:17 3 | int count = total | ^^^^^ hint: convert with `(int)total`; a value that does not fit panics, or is cut to size in a release build .fi .RE .SS "A length as an int" .RS 4 .nf void main() { var names = List { "Ada", "Grace" } int count = names.length() print(count) } .fi .RE .PP .RS 4 .nf error: expected `int`, found `nint` --> example.sg:3:17 3 | int count = names.length() | ^^^^^^^^^^^^^^ hint: convert with `(int)names.length()`; a value that does not fit panics, or is cut to size in a release build .fi .RE .PP \fBvar count = names.length()\fR keeps it an \fBnint\fR\&. .SS "An int and a uint" .RS 4 .nf void main() { int a = 5 uint b = 7 print(a + b) } .fi .RE .PP .RS 4 .nf error: `int` and `uint` do not mix: neither holds every value of the other --> example.sg:4:11 4 | print(a + b) | ^^^^^ hint: `long` holds both: `(long)b` .fi .RE .SS "A number as a condition" .RS 4 .nf void main() { int count = 3 if count { print(count) } } .fi .RE .PP .RS 4 .nf error: expected `bool`, found `int` --> example.sg:3:8 3 | if count { | ^^^^^ hint: a number is not a condition; compare it: `count != 0` .fi .RE .SS "A shift past 31" .RS 4 .nf void main() { int bit = 1 << 32 print(bit) } .fi .RE .PP .RS 4 .nf error: a shift by 32 is out of range: `int` has 32 bits --> example.sg:2:20 2 | int bit = 1 << 32 | ^^ hint: the count goes from 0 to 31 .fi .RE .PP \fBlong bit = 1 << 32\fR shifts a \fBlong\fR\&. .SS "A conversion that cannot fail" .RS 4 .nf void main() { int n = 5 print(n.tryToLong() ?? 0) } .fi .RE .PP .RS 4 .nf error: every `int` fits in `long`: there is nothing to convert --> example.sg:3:13 3 | print(n.tryToLong() ?? 0) | ^^^^^^^^^ hint: it becomes one on its own: `long x = value` .fi .RE .SH "SEE ALSO" .BR sg-long (7), .BR sg-nint (7), .BR sg-uint (7), .BR sg-byte (7), .BR sg-double (7) .PP https://sg\-lang.org/reference/int.html