.\" Made by website/plain/build.mjs from website/words/nint.md; change that file. .TH SG-NINT 7 2026-11-28 "SG" "SG reference" .SH NAME nint \- an integer with a sign, as wide as an address; every length, index and position is one .SH SYNOPSIS .TP \fBnint\fR a signed integer as wide as an address: 64 bits on today's computers .TP \fBxs.length(), text.length()\fR a length, as an nint .TP \fBxs[i], xs[a..b], xs[^k]\fR an index and the ends of a range: an nint, or an integer that becomes one on its own (byte, short, int) .TP \fBfor i in 0..xs.length()\fR every place of a list; i is an nint .TP \fBtext.indexOf(x), xs.indexOf(v), xs.binarySearch(v)\fR a place, as an nint? .TP \fBnint.MinValue, nint.MaxValue\fR the two ends, the machine's .TP \fB(nint)x\fR a long, a uint, a ulong or a nuint as an index .TP \fB(int)n, n.tryToInt(), (double)n\fR a length as an int, or as a fraction .TP \fBq \- p\fR the count of items between two raw pointers .TP \fBMemory.sizeOf(), Memory.alignOf()\fR the bytes a type takes, and its alignment .SH DESCRIPTION An \fBnint\fR is a signed integer as wide as an address on the machine: 64 bits on today's computers, from -9 223 372 036 854 775 808 to 9 223 372 036 854 775 807. It is the type of every length and every place: \fBlength()\fR of a list, an array, a slice or a text gives an \fBnint\fR, and so do \fBindexOf\fR and the other searches. A list is not held to two billion items, as it would be if its places were \fBint\fRs. .PP An index takes an \fBnint\fR, or a type that becomes one on its own: \fBbyte\fR, \fBsbyte\fR, \fBshort\fR, \fBushort\fR and \fBint\fR\&. So \fBxs[i]\fR with an \fBint i\fR is written as it is. A \fBlong\fR, a \fBuint\fR, a \fBulong\fR and a \fBnuint\fR are written \fB(nint)x\fR: a \fBuint\fR does not fit in the \fBnint\fR of a 32-bit machine, and the unsigned types have values no \fBnint\fR holds. The same holds for the ends of a range and for the count after \fB^\fR\&. .PP An \fBnint\fR becomes a \fBlong\fR on its own, and next to an \fBint\fR in arithmetic the result is an \fBnint\fR: \fBtotal / xs.length()\fR is an \fBnint\fR\&. Nothing goes back on its own. A length becomes an \fBint\fR with \fB(int)\fR, which stops the program when it does not fit, or with \fBtryToInt()\fR and \fBsaturatingToInt()\fR; it becomes a \fBdouble\fR with \fB(double)\fR, since a \fBdouble\fR holds whole numbers exactly only up to 2^53. An \fBnint\fR and a \fBnuint\fR do not mix. .PP An \fBnint\fR has a sign, so a length minus one is -1 for an empty list, not a huge number, and a loop that counts down stops at \fBi >= 0\fR\&. A negative index stops the program, as one past the end does. .PP Overflow follows the rule of every integer: a panic in the interpreter, in tests and in a debug build, wrapping in a release build, and \fBtryAdd\fR, \fBwrappingAdd\fR and \fBsaturatingAdd\fR for one behaviour in every build. An \fBnint\fR has no \fBtoLittleEndian\fR or \fBfromBigEndian\fR: its size depends on the machine, and a number kept in bytes has a size of its own, so it goes through a \fBlong\fR\&. .PP In raw memory, \fBq \- p\fR counts the items of one type between two pointers as an \fBnint\fR, and \fBMemory.sizeOf()\fR gives the bytes of a type as one. \fB(nint)p\fR is an address as a number with a sign; [\fBnuint\fR](nuint.md) is the usual one. .SH EXAMPLES .SS "Lengths and places" .RS 4 .nf void main() { var scores = List { 70, 85, 92, 64 } var count = scores.length() print(count) for i in 0..count { print($"{i}: {scores[i]}") } int first = 0 byte last = 3 print(scores[first] + scores[last]) print(scores[^1]) } .fi .RE .PP .RS 4 .nf 4 0: 70 1: 85 2: 92 3: 64 134 64 .fi .RE .PP \fBcount\fR is an \fBnint\fR, and so is \fBi\fR, which runs from 0 to 3. An \fBint\fR and a \fBbyte\fR are indexes as they are, since both become an \fBnint\fR on their own. .SS "Places in a text are bytes" .RS 4 .nf void main() { var text = "naïve café" print(text.length()) var at = text.indexOf("café") ?? -1 print(at) print(text[at..text.length()]) print(text.indexOf('z') ?? -1) } .fi .RE .PP .RS 4 .nf 12 7 café -1 .fi .RE .PP \fBï\fR and \fBé\fR take two bytes each, so the text has 12 bytes, and \fBcafé\fR starts at byte 7. \fBindexOf\fR gives an \fBnint?\fR, \fBnull\fR when nothing is found. .SS "Counting down" .RS 4 .nf void main() { var xs = List { 5, 8, 13 } var i = xs.length() - 1 while i >= 0 { print(xs[i]) i -= 1 } var empty = List() print(empty.length() - 1) } .fi .RE .PP .RS 4 .nf 13 8 5 -1 .fi .RE .PP An \fBnint\fR has a sign, so \fBi\fR goes down to -1 and the loop ends there. A length minus one of an empty list is -1. .SS "Into an int and a double" .RS 4 .nf void main() { var temperatures = List { 18, 21, 25, 19 } int total = 0 for t in read temperatures { total += t } var whole = total / temperatures.length() double exact = total / (double)temperatures.length() int count = (int)temperatures.length() print($"{whole} {exact} {count}") long day = 2 print(temperatures[(nint)day]) print(temperatures.length().tryToInt() ?? -1) } .fi .RE .PP .RS 4 .nf 20 20.75 4 25 4 .fi .RE .PP \fBtotal / temperatures.length()\fR divides an \fBint\fR by an \fBnint\fR and gives an \fBnint\fR, \fB20\fR\&. For a fraction the length is written \fB(double)\fR, and the \fBint\fR then becomes a \fBdouble\fR on its own. \fB(int)\fR makes the length an \fBint\fR, and a \fBlong\fR is an index only with \fB(nint)\fR\&. \fBtryToInt()\fR gives \fBnull\fR for a length past \fBint.MaxValue\fR, which a cast would stop the program on. .SS "The ends, and overflow" .RS 4 .nf void main() { print(nint.MinValue) print(nint.MaxValue) nint big = nint.MaxValue print(big.tryAdd(1) ?? -1) print(big.wrappingAdd(1)) print(big.saturatingAdd(1)) long wide = big print(wide) } .fi .RE .PP .RS 4 .nf -9223372036854775808 9223372036854775807 -1 -9223372036854775808 9223372036854775807 9223372036854775807 .fi .RE .PP \fBnint.MaxValue\fR is the machine's. An \fBnint\fR becomes a \fBlong\fR on its own. .SS "Counting in raw memory" .RS 4 .nf struct Point { int x int y } void main() { var points = Memory.allocate(5) var fourth = unsafe (points + 3) print(fourth - points) print(points - fourth) var bytes = (fourth - points) * Memory.sizeOf() print(bytes) print((nint)fourth - (nint)points) unsafe Memory.free(points) } .fi .RE .PP .RS 4 .nf 3 -3 24 24 .fi .RE .PP \fBfourth \- points\fR counts \fBPoint\fRs, three of them, and has a sign. \fBMemory.sizeOf()\fR is an \fBnint\fR too, 8 bytes, so the product is one. \fB(nint)p\fR reads the address as a number, and two of them differ by the bytes. .SH "WHAT STOPS THE PROGRAM" .SS "An index below zero" .RS 4 .nf int last(read List xs) { return xs[xs.length() - 1] } void main() { var empty = List() print(last(read empty)) } .fi .RE .PP .RS 4 .nf panic: index -1 is out of range: the length is 0 --> example.sg:2:12 2 | return xs[xs.length() - 1] | ^^^^^^^^^^^^^^^^^^^ hint: `.get(i)` reads with `null` instead; before a write, compare the index with `.length()` .fi .RE .PP \fBxs.get(xs.length() \- 1)\fR, or \fBxs[^1]\fR behind a test of the length, says what an empty list gives. .SS "A cast that does not fit" .RS 4 .nf nint place(ulong at) { return (nint)at } void main() { print(place(ulong.MaxValue)) } .fi .RE .PP .RS 4 .nf panic: value 18446744073709551615 does not fit in nint --> example.sg:2:12 2 | return (nint)at | ^^^^^^^^ hint: `tryToNint()` gives `null` when the value does not fit, and `wrappingToNint()` cuts it to size .fi .RE .SH "WHAT THE COMPILER REFUSES" .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 "A long as an index" .RS 4 .nf void main() { long at = 1 var names = List { "Ada", "Grace" } print(names[at]) } .fi .RE .PP .RS 4 .nf error: expected `nint`, found `long` --> example.sg:4:17 4 | print(names[at]) | ^^ hint: convert with `(nint)at`; a value that does not fit panics, or is cut to size in a release build .fi .RE .PP A \fBuint\fR, a \fBulong\fR and a \fBnuint\fR are refused the same way. .SS "An nint and a nuint" .RS 4 .nf void main() { var xs = List { 1, 2, 3 } nuint extra = 2 print(xs.length() + extra) } .fi .RE .PP .RS 4 .nf error: `nint` and `nuint` do not mix: neither holds every value of the other --> example.sg:4:11 4 | print(xs.length() + extra) | ^^^^^^^^^^^^^^^^^^^ hint: convert one side to the type of the other: `(nint)extra`; a value that does not fit panics, or is cut to size in a release build .fi .RE .SS "A length in a fraction" .RS 4 .nf void main() { var xs = List { 1, 2, 3 } double part = 1.0 / xs.length() print(part) } .fi .RE .PP .RS 4 .nf error: `nint` and `double` do not mix: a `double` does not hold every `nint` exactly --> example.sg:3:25 3 | double part = 1.0 / xs.length() | ^^^^^^^^^^^ hint: convert it: `(double)xs.length()`, which rounds past 2^53 .fi .RE .SS "An nint written as bytes" .RS 4 .nf void main() { nint size = 4096 print(size.toLittleEndian()) } .fi .RE .PP .RS 4 .nf error: `nint` has the size of an address, which depends on the machine; a number kept in bytes has a size of its own --> example.sg:3:16 3 | print(size.toLittleEndian()) | ^^^^^^^^^^^^^^ hint: make it a `long` first: `((long)size).toLittleEndian()` gives 8 bytes .fi .RE .SH "SEE ALSO" .BR sg-int (7), .BR sg-long (7), .BR sg-nuint (7), .BR sg-list (7), .BR sg-string (7), .BR sg-raw (7), .BR sg-range (7), .BR sg-index-from-end (7) .PP https://sg\-lang.org/reference/nint.html