.\" Made by website/plain/build.mjs from website/words/long.md; change that file. .TH SG-LONG 7 2026-11-28 "SG" "SG reference" .SH NAME long \- an integer of 64 bits, with a sign; for sums, sizes of files and time in milliseconds .SH SYNOPSIS .TP \fBlong\fR an integer of 64 bits, from -9 223 372 036 854 775 808 to 9 223 372 036 854 775 807 .TP \fB3000000_000\fR a number written in the program that does not fit in an int .TP \fBlong total = 0\fR a long from a number that would be an int alone .TP \fBlong.MinValue, long.MaxValue\fR the two ends .TP \fB(long)n * m\fR an int made a long first, so the arithmetic is done in 64 bits .TP \fB(int)x, x.tryToInt(), x.saturatingToInt()\fR a long back to an int .TP \fB(double)x, (nint)x\fR a long as a fraction, rounded, or as an index .TP \fBlong.parse(text)\fR the number a text writes, as a Result .SH DESCRIPTION A \fBlong\fR is a signed integer of 64 bits, on every machine. It is the type for what can pass two billion: a sum of many values, the size of a file in bytes, a time in milliseconds or nanoseconds. \fBFile.info\fR gives a size as a \fBlong\fR, \fBDateTime\fR gives its milliseconds as one, and \fBDuration\fR is made from one. .PP A number written in the program is a \fBlong\fR where it goes into a \fBlong\fR, or where it does not fit in an \fBint\fR: \fBvar big = 3_000_000_000\fR is a \fBlong\fR, and \fBvar small = 5\fR an \fBint\fR\&. To have a \fBlong\fR from a small number, the type is written: \fBlong total = 0\fR\&. .PP An \fBint\fR becomes a \fBlong\fR on its own, in an assignment, as an argument and next to a \fBlong\fR in arithmetic: \fBcount * size\fR is a \fBlong\fR when either is. Two \fBint\fRs still give an \fBint\fR, and the \fBlong\fR comes only after: \fBlong bytes = megabytes * 1048576\fR multiplies in 32 bits and can overflow. \fB(long)megabytes * 1048576\fR makes the \fBlong\fR first. .PP Nothing becomes smaller on its own. A \fBlong\fR goes to an \fBint\fR with \fB(int)\fR, which stops the program when the value does not fit (a release build keeps the low 32 bits instead), or with \fBtryToInt()\fR, \fBsaturatingToInt()\fR or \fBwrappingToInt()\fR, which do the same in every build. A \fBlong\fR does not mix with a \fBulong\fR, since neither holds every value of the other, and goes to a \fBdouble\fR only with \fB(double)\fR, since a \fBdouble\fR holds whole numbers exactly only up to 2^53. An index is an \fBnint\fR, so a \fBlong\fR used as one is written \fB(nint)x\fR\&. .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; \fBtryAdd\fR, \fBwrappingAdd\fR and \fBsaturatingAdd\fR say one behaviour for every build. The methods are in Numbers. .SH EXAMPLES .SS "When a number is a long" .RS 4 .nf void main() { var small = 5 var big = 3_000_000_000 long total = 0 total += small total += big print($"{total} {big * 4}") print($"{long.MinValue} {long.MaxValue}") } .fi .RE .PP .RS 4 .nf 3000000005 12000000000 -9223372036854775808 9223372036854775807 .fi .RE .PP \fBsmall\fR is an \fBint\fR and widens as it is added. \fBbig\fR is a \fBlong\fR because it does not fit in an \fBint\fR, so \fBbig * 4\fR is computed in 64 bits. .SS "An int meets a long" .RS 4 .nf long square(long x) { return x * x } void main() { int side = 100_000 print(square(side)) int count = 3 long size = 4_000_000_000 var total = count * size print(total) long sum = 0 for i in 1..100_001 { sum += (long)i * i } print(sum) } .fi .RE .PP .RS 4 .nf 10000000000 12000000000 333338333350000 .fi .RE .PP \fBside\fR becomes a \fBlong\fR on its way into \fBsquare\fR\&. \fBcount * size\fR is a \fBlong\fR because \fBsize\fR is one. In the loop \fBi\fR is an \fBint\fR, and \fB(long)i\fR makes the square a \fBlong\fR before it is taken: \fBi * i\fR alone would overflow from 46 341 on. .SS "The type reaches the numbers, not the variables" .RS 4 .nf void main() { long month = 30 * 24 * 60 * 60 * 1000 int days = 30 long same = (long)days * 24 * 60 * 60 * 1000 print($"{month} {same}") } .fi .RE .PP .RS 4 .nf 2592000000 2592000000 .fi .RE .PP In \fBmonth\fR every number is written in the program, so each takes the type of the place, \fBlong\fR\&. With \fBdays\fR, an \fBint\fR, the product would be computed in \fBint\fR and overflow; \fB(long)days\fR makes every step a \fBlong\fR\&. .SS "Sizes of files" .RS 4 .nf string human(long bytes) { if bytes < 1024 { return $"{bytes} B".toString() } if bytes < 1 << 20 { return $"{(double)bytes / 1024:F1} KB".toString() } if bytes < 1 << 30 { return $"{(double)bytes / (1 << 20):F1} MB".toString() } return $"{(double)bytes / (1 << 30):F2} GB".toString() } void main() { var sizes = List { 512, 48_213, 7_340_032, 5_368_709_120 } long total = 0 for size in read sizes { total += size print(human(size)) } print($"{total} bytes, {human(total)}") } .fi .RE .PP .RS 4 .nf 512 B 47.1 KB 7.0 MB 5.00 GB 5376097877 bytes, 5.01 GB .fi .RE .PP A file of 5 GB does not fit in an \fBint\fR\&. \fB(double)bytes\fR is written out, since a \fBlong\fR becomes a \fBdouble\fR only with a cast. In \fBbytes < 1 << 30\fR the shift takes the type of \fBbytes\fR, so \fB1 << 40\fR would be a \fBlong\fR too. .SS "Time in milliseconds" .RS 4 .nf Result main() { var start = try DateTime.parse("2026-11-27T09:15:00Z") var end = try DateTime.parse("2026-11-27T11:47:30.250Z") long ms = end.unixMilliseconds() - start.unixMilliseconds() long seconds = ms / 1000 print($"{ms} ms") print($"{seconds / 3600}:{seconds / 60 % 60:D2}:{seconds % 60:D2}.{ms % 1000:D3}") print(Duration.fromMilliseconds(ms)) print(start.unixMilliseconds()) return Ok() } .fi .RE .PP .RS 4 .nf 9150250 ms 2:32:30.250 9150.25s 1795770900000 .fi .RE .PP \fBunixMilliseconds()\fR counts from 1970 and is past two billion, so it is a \fBlong\fR; so is the difference. \fB/\fR and \fB%\fR split it into hours, minutes, seconds and milliseconds. .SS "Back to an int" .RS 4 .nf void main() { long size = 4_000_000_000 long kilobytes = size / 1000 print((int)kilobytes) print(size.tryToInt() ?? -1) print(size.saturatingToInt()) print(size.wrappingToInt()) var names = List { "Ada", "Grace", "Linus" } long last = 2 print(names[(nint)last]) } .fi .RE .PP .RS 4 .nf 4000000 -1 2147483647 -294967296 Linus .fi .RE .PP \fB(int)\fR is for a value that fits; \fBtryToInt\fR gives \fBnull\fR for one that does not, \fBsaturatingToInt\fR the nearest end and \fBwrappingToInt\fR the low 32 bits. An index is an \fBnint\fR, so a \fBlong\fR becomes one with \fB(nint)\fR\&. .SS "Read from text" .RS 4 .nf void main() { print(long.parse("9007199254740993") ?? 0) print(long.parse("-9223372036854775808") ?? 0) match long.parse("9223372036854775808") { Ok(n) => print(n) Error(e) => print(e) } print(int.parse("3000000000") is Error(OutOfRange)) } .fi .RE .PP .RS 4 .nf 9007199254740993 -9223372036854775808 OutOfRange true .fi .RE .PP A number too large for an \fBint\fR is read with \fBlong.parse\fR; \fBOutOfRange\fR says it does not fit in a \fBlong\fR either. .SH "WHAT STOPS THE PROGRAM" .SS "An int product on its way into a long" .RS 4 .nf long bytesOf(int megabytes) { return megabytes * 1024 * 1024 } void main() { print(bytesOf(4096)) } .fi .RE .PP .RS 4 .nf panic: overflow: 4194304 * 1024 does not fit in int --> example.sg:2:12 2 | return megabytes * 1024 * 1024 | ^^^^^^^^^^^^^^^^^^^^^^^ hint: the result goes into a `long`; compute in `long` from the start: `(long)megabytes * 1024 * 1024` .fi .RE .PP \fBmegabytes\fR is an \fBint\fR, so the product is one, and becomes a \fBlong\fR only when it is returned. \fB(long)megabytes * 1024 * 1024\fR is computed in 64 bits. .SS "An overflow" .RS 4 .nf long next(long n) { return n + 1 } void main() { print(next(long.MaxValue)) } .fi .RE .PP .RS 4 .nf panic: overflow: 9223372036854775807 + 1 does not fit in long --> example.sg:2:12 2 | return n + 1 | ^^^^^ hint: `tryAdd` gives `null` instead, and `wrappingAdd` wraps around .fi .RE .PP In a release build it wraps to \fBlong.MinValue\fR\&. .SS "A cast to int that does not fit" .RS 4 .nf int toInt(long value) { return (int)value } void main() { print(toInt(4_000_000_000)) } .fi .RE .PP .RS 4 .nf panic: value 4000000000 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 .SH "WHAT THE COMPILER REFUSES" .SS "A long where an int goes" .RS 4 .nf int half(int n) { return n / 2 } void main() { long size = 2048 print(half(size)) } .fi .RE .PP .RS 4 .nf error: expected `int`, found `long` --> example.sg:7:16 7 | print(half(size)) | ^^^^ hint: convert with `(int)size`; a value that does not fit panics, or is cut to size in a release build .fi .RE .SS "A long and a ulong" .RS 4 .nf void main() { long a = 1 ulong b = 2 print(a + b) } .fi .RE .PP .RS 4 .nf error: `long` and `ulong` do not mix: neither holds every value of the other --> example.sg:4:11 4 | print(a + b) | ^^^^^ hint: convert one side to the type of the other: `(long)b`; a value that does not fit panics, or is cut to size in a release build .fi .RE .SS "A long where a double goes" .RS 4 .nf void main() { long total = 10 double average = total / 4 print(average) } .fi .RE .PP .RS 4 .nf error: expected `double`, found `long` --> example.sg:3:22 3 | double average = total / 4 | ^^^^^^^^^ hint: a `double` holds whole numbers exactly up to 2^53, and `long` goes past that; convert with `(double)`, which rounds .fi .RE .PP \fB(double)total / 4\fR divides as fractions and gives \fB2.5\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 .SS "A number past every long" .RS 4 .nf void main() { print(18_446_744_073_709_551_615) } .fi .RE .PP .RS 4 .nf error: `18_446_744_073_709_551_615` does not fit in long --> example.sg:2:11 2 | print(18_446_744_073_709_551_615) | ^^^^^^^^^^^^^^^^^^^^^^^^^^ hint: write the type where it goes: `ulong value = 18_446_744_073_709_551_615` .fi .RE .PP A number is never unsigned on its own; a \fBulong\fR is written. .SH "SEE ALSO" .BR sg-int (7), .BR sg-ulong (7), .BR sg-nint (7), .BR sg-double (7) .PP https://sg\-lang.org/reference/long.html