.\" Made by website/plain/build.mjs from website/words/double.md; change that file. .TH SG-DOUBLE 7 2026-11-28 "SG" "SG reference" .SH NAME double \- a fraction of 64 bits (IEEE 754); its arithmetic is the hardware's and never panics .SH SYNOPSIS .TP \fB1.5, 2.998e8, 6.674_30e\-11\fR a fraction written in the program; a double in a var .TP \fBdouble x = 3\fR a whole number where a double goes .TP \fB+ \- * / %\fR the hardware's arithmetic: 1 / 0.0 is Infinity, 0 / 0.0 is NaN .TP \fBa == b, a < b\fR the hardware's comparison: NaN is equal to nothing .TP \fB(double)n, (int)x\fR a long or a nint as a double; a double as an integer, the fraction dropped .TP \fBx.sqrt(), x.pow(y), x.round()\fR math, as methods of the value .TP \fBdouble.Pi, double.NaN, double.Infinity, double.MaxValue\fR constants .TP \fBx.isNaN()\fR the test for NaN, which == never finds .TP \fB{x:F2}\fR printed with two digits after the point .TP \fBdouble.parse(text)\fR the number a text writes, or the place where it stops being one .SH DESCRIPTION A \fBdouble\fR is a floating-point number of 64 bits, as IEEE 754 sets it out: about 16 digits, up to about 1.8e308 (\fBdouble.MaxValue\fR). A number written with a point or an exponent takes the type it is used as, and is a \fBdouble\fR in a \fBvar\fR; \fBfloat small = 0.1\fR makes a [\fBfloat\fR](../docs/learn/book/02-values.md#float-and-double), the type of 32 bits. Digits stand on both sides of the point, and there is no suffix: \fB1.5f\fR is refused. .PP An integer becomes a \fBdouble\fR on its own only where the \fBdouble\fR holds every value of it exactly: \fBint\fR, \fBuint\fR and the smaller ones do. A \fBlong\fR, a \fBulong\fR, a \fBnint\fR or a \fBnuint\fR needs \fB(double)n\fR, which rounds past 2^53, so a sum divided by \fBxs.length()\fR needs it too. A \fBfloat\fR becomes a \fBdouble\fR on its own; the other way is \fB(float)x\fR, which rounds. .PP A \fBdouble\fR never becomes an integer on its own. \fB(int)x\fR drops the fraction, toward zero. A value that does not fit, \fBNaN\fR too, stops the program in the interpreter, in tests and in a debug build; a release build gives the nearest value instead, and 0 for \fBNaN\fR\&. \fBx.tryToInt()\fR gives \fBnull\fR and \fBx.saturatingToInt()\fR the nearest end, in every build. To round first, write \fB(int)x.round()\fR\&. .PP The arithmetic is the hardware's and never panics: a division by zero gives \fBInfinity\fR, or \fBNaN\fR for \fB0 / 0.0\fR, and \fB%\fR keeps the sign of the left side. Each operation is rounded on its own, nothing is fused, so the interpreter and compiled code give the same digits. Numbers written out take the type they go to, so \fBdouble part = 7 / 8\fR is 0.875. A division of two integer variables would drop the fraction before the result became a \fBdouble\fR, so \fBdouble part = done / total\fR is refused: \fB(double)done / total\fR keeps the fraction, and \fB(double)(done / total)\fR drops it on purpose. .PP \fB==\fR and \fB<\fR are the hardware's too: \fBNaN\fR equals nothing, itself included, and is found with \fBisNaN()\fR\&. \fBequals\fR, \fBcompare\fR, \fBsort\fR, \fBcontains\fR and \fB==\fR on a struct or a list use an order in which \fBNaN\fR equals itself and comes first. Since \fB0.1 + 0.2\fR is not \fB0.3\fR, two results are compared with a margin. A \fBdouble\fR is never a pattern of a \fBmatch\fR nor the key of a \fBHashMap\fR\&. .PP A \fBdouble\fR is printed with the fewest digits that read back as the same number, as JavaScript writes it: \fB0.1\fR, \fB3\fR, \fB1e+21\fR, \fB1e\-7\fR, \fBNaN\fR, \fBInfinity\fR, \fB\-0\fR\&. \fBdouble.parse\fR reads all of them back. The methods, from C's \fB\fR, and the constants are in Numbers; in compiled code a \fBdouble\fR is C's \fBdouble\fR\&. .SH EXAMPLES .SS "Written and printed" .RS 4 .nf void main() { var price = 19.99 var light = 2.998e8 double whole = 3 print($"{price} {light} {whole}") print($"{0.1 + 0.2} {1.0 / 3.0} {1e21} {0.0000001}") print($"{price:F1} {1.0 / 3.0:F4}") } .fi .RE .PP .RS 4 .nf 19.99 299800000 3 0.30000000000000004 0.3333333333333333 1e+21 1e-7 20.0 0.3333 .fi .RE .PP \fB0.1\fR and \fB0.2\fR have no exact form in binary, and their sum shows it: the shortest digits that read back are written, not a rounded guess. \fB{price:F1}\fR rounds to one digit after the point. .SS "An average" .RS 4 .nf double average(read List values) { double sum = 0 for v in values { sum += v } return sum / (double)values.length() } void main() { var temperatures = List { 21.5, 23.0, 19.25, 22.75 } print($"{average(read temperatures):F2} degrees") int passed = 7 int total = 8 double dropped = (double)(passed / total) double kept = (double)passed / total print($"{dropped} {kept}") } .fi .RE .PP .RS 4 .nf 21.62 degrees 0 0.875 .fi .RE .PP \fBlength()\fR gives a \fBnint\fR, which needs \fB(double)\fR\&. \fBpassed / total\fR divides two integers and gives 0; written alone where a \fBdouble\fR goes it is refused, so the cast says which is meant. With \fB(double)passed\fR the division is a fractional one, and \fBtotal\fR follows. .SS "Compound interest" .RS 4 .nf void main() { double balance = 1000 double rate = 0.035 for year in 1..4 { balance *= 1 + rate print($"year {year}: {balance:F2}") } var tenYears = 1000 * (1 + rate).pow(10) print($"after ten years: {tenYears:F2}") } .fi .RE .PP .RS 4 .nf year 1: 1035.00 year 2: 1071.22 year 3: 1108.72 after ten years: 1410.60 .fi .RE .PP \fB1 + rate\fR is a \fBdouble\fR, since \fBrate\fR is one, and \fBpow\fR is a method of it. .SS "The distance between two points" .RS 4 .nf struct Point : Copyable { double x double y } double distance(Point a, Point b) { var dx = b.x - a.x var dy = b.y - a.y return (dx * dx + dy * dy).sqrt() } void main() { var home = Point { x = 0, y = 0 } var shop = Point { x = 3, y = 4 } var park = Point { x = -1.5, y = 2.25 } print(distance(home, shop)) print($"{distance(shop, park):F3}") print((shop.x - park.x).hypot(shop.y - park.y)) var angle = (shop.y - home.y).atan2(shop.x - home.x) print($"{angle * 180 / double.Pi:F1} degrees") } .fi .RE .PP .RS 4 .nf 5 4.828 4.828301978956992 53.1 degrees .fi .RE .PP \fBhypot\fR gives the same length without squaring, so it does not overflow on numbers near \fBdouble.MaxValue\fR\&. Angles are in radians. .SS "Infinity and NaN" .RS 4 .nf void main() { double zero = 0 var up = 1 / zero var down = -1 / zero var none = zero / zero print($"{up} {down} {none}") print($"{none == none} {none.isNaN()} {up.isInfinite()} {up > double.MaxValue}") var readings = List { 2.5, none, -1, up } readings.sort() print(readings) print(readings.contains(double.NaN)) } .fi .RE .PP .RS 4 .nf Infinity -Infinity NaN false true true true [NaN, -1, 2.5, Infinity] true .fi .RE .PP Nothing stops the program. \fBnone == none\fR is \fBfalse\fR, as the hardware has it; \fBsort\fR and \fBcontains\fR use the order in which \fBNaN\fR equals itself and comes first. .SS "To an integer and back" .RS 4 .nf void main() { double x = 2.75 print($"{(int)x} {(int)-x} {x.round()} {x.floor()} {(-x).floor()}") var rounded = (int)x.round() print(rounded) long micros = 9007199254740993 double seconds = (double)micros print(seconds) print($"{1e10.tryToInt() ?? -1} {1e10.saturatingToInt()} {double.NaN.tryToLong() ?? -1}") } .fi .RE .PP .RS 4 .nf 2 -2 3 2 -3 3 9007199254740992 -1 2147483647 -1 .fi .RE .PP A cast cuts toward zero; \fBfloor\fR goes down, \fBround\fR to the nearest, a half away from zero. 2^53 + 1 is the first \fBlong\fR a \fBdouble\fR cannot hold, so \fB(double)\fR rounds it. \fBtryToInt\fR and \fBsaturatingToInt\fR never stop the program. .SS "float and double" .RS 4 .nf void main() { float small = 0.1 double wide = small print($"{small} {wide}") float sum = 0 for _ in 0..10 { sum += small } print($"{sum} {(float)wide == small}") print($"{float.MaxValue} {double.MaxValue}") } .fi .RE .PP .RS 4 .nf 0.1 0.10000000149011612 1.0000001 true 3.4028235e+38 1.7976931348623157e+308 .fi .RE .PP A \fBfloat\fR is printed with the digits a \fBfloat\fR needs. As a \fBdouble\fR the same value shows that it was never exactly 0.1. A \fBfloat\fR is computed as a \fBfloat\fR, so ten of them miss 1 by more. .SS "Read from text" .RS 4 .nf void show(read string text) { match double.parse(text) { Ok(v) => print($"{text} -> {v}") Error(e) => print($"{text} -> {e}") } } void main() { show("2.5") show("-1e-3") show(".5") show("NaN") show("1e999") show("12,5") show(" 7") var total = (double.parse("19.99") ?? 0) * 3 print($"{total:F2}") } .fi .RE .PP .RS 4 .nf 2.5 -> 2.5 -1e-3 -> -0.001 \&.5 -> 0.5 NaN -> NaN 1e999 -> Infinity 12,5 -> Invalid(2) 7 -> Invalid(0) 59.97 .fi .RE .PP \fBparse\fR is looser than a number in the program (\fB\&.5\fR) and reads what \fBprint\fR writes (\fBNaN\fR). A number past the largest is \fBInfinity\fR, as in C. \fBInvalid\fR gives the place where the text stops being a number; a space is not skipped, so a program trims the text first. .SH "WHAT STOPS THE PROGRAM" .SS "A value that does not fit the integer" .RS 4 .nf void main() { var sizes = List { 1.5, 3e9 } int total = 0 for s in sizes { total += (int)s } print(total) } .fi .RE .PP .RS 4 .nf panic: value 3000000000 does not fit in int --> example.sg:5:18 5 | total += (int)s | ^^^^^^ hint: `tryToInt()` gives `null` when the value does not fit .fi .RE .PP In a release build \fB(int)3e9\fR is \fB2147483647\fR, the nearest \fBint\fR, and the program goes on. .SS "NaN as an integer" .RS 4 .nf int percent(int done, int total) { return (int)((double)done / total * 100) } void main() { var done = 0 var total = 0 print($"{percent(done, total)}%") } .fi .RE .PP .RS 4 .nf panic: value NaN does not fit in int --> example.sg:2:12 2 | return (int)((double)done / total * 100) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ hint: `tryToInt()` gives `null` when the value does not fit .fi .RE .PP \fB0 / 0.0\fR is \fBNaN\fR, and no integer is \fBNaN\fR\&. A release build gives 0 and prints \fB0%\fR\&. .SH "WHAT THE COMPILER REFUSES" .SS "A division of two integers" .RS 4 .nf void main() { int passed = 7 int total = 8 double part = passed / total print(part) } .fi .RE .PP .RS 4 .nf error: this divides two integers, so the fraction is dropped before the result becomes a `double` --> example.sg:4:19 4 | double part = passed / total | ^^^^^^^^^^^^^^ hint: to keep the fraction: `(double)passed / total`; to drop it on purpose: `(double)(passed / total)` .fi .RE .PP \fB7 / 8\fR written out is 0.875, since both numbers take the type \fBdouble\fR; with variables the division would be an \fBint\fR\&'s, 0, and the result would change with no word. The same holds where the division is passed, returned, or added to a fractional number. .SS "A length in a division" .RS 4 .nf void main() { var prices = List { 2.5, 4.0 } double sum = 0 for p in read prices { sum += p } print(sum / prices.length()) } .fi .RE .PP .RS 4 .nf error: `nint` and `double` do not mix: a `double` does not hold every `nint` exactly --> example.sg:7:17 7 | print(sum / prices.length()) | ^^^^^^^^^^^^^^^ hint: convert it: `(double)prices.length()`, which rounds past 2^53 .fi .RE .SS "A long and a double" .RS 4 .nf void main() { long bytes = 3500000000 double gigabytes = bytes / 1e9 print(gigabytes) } .fi .RE .PP .RS 4 .nf error: `long` and `double` do not mix: a `double` does not hold every `long` exactly --> example.sg:3:24 3 | double gigabytes = bytes / 1e9 | ^^^^^ hint: convert it: `(double)bytes`, which rounds past 2^53 .fi .RE .PP An \fBint\fR mixes with a \fBdouble\fR on its own, since every \fBint\fR has an exact \fBdouble\fR\&. .SS "A double where an integer goes" .RS 4 .nf void main() { double ratio = 0.25 int percent = ratio * 100 print(percent) } .fi .RE .PP .RS 4 .nf error: expected `int`, found `double` --> example.sg:3:19 3 | int percent = ratio * 100 | ^^^^^^^^^^^ hint: convert with `(int)(ratio * 100)`; it drops the fraction, and a value that does not fit panics, or becomes the nearest `int` in a release build .fi .RE .SS "A double where a float goes" .RS 4 .nf void main() { double precise = 2.5 float small = precise print(small) } .fi .RE .PP .RS 4 .nf error: expected `float`, found `double` --> example.sg:3:19 3 | float small = precise | ^^^^^^^ hint: convert with `(float)`, which rounds .fi .RE .SS "A suffix" .RS 4 .nf void main() { var half = 0.5f print(half) } .fi .RE .PP .RS 4 .nf error: a number has no `f` at its end; it takes the type it is used as --> example.sg:2:16 2 | var half = 0.5f | ^^^^ .fi .RE .PP The number takes its type from where it goes: \fBfloat half = 0.5\fR\&. .SS "A number that becomes 0" .RS 4 .nf void main() { double tiny = 1e-400 print(tiny) } .fi .RE .PP .RS 4 .nf error: `1e-400` is too small for double: it would be 0 --> example.sg:2:19 2 | double tiny = 1e-400 | ^^^^^^ .fi .RE .PP \fB1e400\fR, which would be \fBInfinity\fR, is refused the same way. .SS "A fraction as a pattern" .RS 4 .nf string grade(double score) { return match score { 1.0 => "full" _ => "partial" } } void main() { print(grade(1.0)) } .fi .RE .PP .RS 4 .nf error: a fractional number cannot be a pattern two fractional numbers are seldom exactly equal; compare in an `if`: `if x < 0.5 { ... }` .fi .RE .SS "A double as a key" .RS 4 .nf void main() { var counts = HashMap() print(counts.length()) } .fi .RE .PP .RS 4 .nf error: `double` cannot be the key of a `HashMap`: it is not `Hashable` a fractional number is not `Hashable`: `0.0` and `-0.0` are equal and have different bits, and a sum seldom gives the exact key back .fi .RE .SS "sqrt of an integer" .RS 4 .nf void main() { int area = 50 print(area.sqrt()) } .fi .RE .PP .RS 4 .nf error: type `int` has no method `sqrt` --> example.sg:3:16 3 | print(area.sqrt()) | ^^^^ hint: it is a method of `double`: `((double)area).sqrt()` .fi .RE .SS "Math.sqrt" .RS 4 .nf void main() { double side = 2.0 print(Math.sqrt(side * side)) } .fi .RE .PP .RS 4 .nf error: there is no variable or type named `Math` --> example.sg:3:11 3 | print(Math.sqrt(side * side)) | ^^^^ hint: math is a method of the number: `(side * side).sqrt()` .fi .RE .PP There is no \fBMath\fR of C# and Java: \fBsqrt\fR, \fBpow\fR, \fBabs\fR, \fBmin\fR, \fBmax\fR and the others are methods of the number. .SS "Bits of a double" .RS 4 .nf void main() { double x = 6.5 print(x & 1) } .fi .RE .PP .RS 4 .nf error: operator `&` is not defined for `double` and `double` the bit operators work on integers --> example.sg:3:11 3 | print(x & 1) | ^^^^^ hint: `x.toBits()` gives its bits as a `ulong` .fi .RE .PP \fBtoBits()\fR and \fBdouble.fromBits(bits)\fR give the 64 bits as a \fBulong\fR and back, with nothing lost. .SH "SEE ALSO" .BR sg-float (7), .BR sg-int (7), .BR sg-long (7), .BR sg-const (7), .BR sg-string (7) .PP https://sg\-lang.org/reference/double.html