.\" Made by website/plain/build.mjs from website/words/float.md; change that file. .TH SG-FLOAT 7 2026-11-28 "SG" "SG reference" .SH NAME float \- a fraction of 32 bits (IEEE 754), about 7 digits; half the size of a double, for large arrays and for C .SH SYNOPSIS .TP \fBfloat x = 0.1\fR a fraction written in the program, a float where the place says so; there is no suffix .TP \fBfloat n = 3\fR a whole number where a float goes, if a float holds it exactly .TP \fB(float)d, (float)n\fR a double, an int or a long as a float, rounded .TP \fB+ \- * / %\fR the hardware's arithmetic in 32 bits: it never panics, and past the largest gives Infinity .TP \fBfloat.MaxValue, float.NaN, float.Infinity, float.Pi\fR constants, as a float holds them .TP \fBx.toBits(), float.fromBits(bits)\fR the 32 bits as a uint, and back .TP \fB{x:F2}\fR printed with two digits after the point .TP \fBfloat.parse(text)\fR the number a text writes, as a Result .SH DESCRIPTION A \fBfloat\fR is a floating-point number of 32 bits, as IEEE 754 sets it out: about 7 digits, up to about 3.4e38 (\fBfloat.MaxValue\fR). It takes half the room of a [\fBdouble\fR](double.md), which is what a list of a million values, a file format or a function of C may ask for. Where none of those asks, a \fBdouble\fR is the better choice. .PP A number with a point or an exponent takes the type it is used as, so \fBfloat x = 0.1\fR is a \fBfloat\fR, and in a \fBvar\fR it is a \fBdouble\fR\&. No letter at its end makes one a \fBfloat\fR: \fB0.5f\fR is refused. A whole number goes where a \fBfloat\fR goes only if a \fBfloat\fR holds it exactly, which every whole number up to 2^24 (16 777 216) is. \fB(float)d\fR rounds a \fBdouble\fR to the nearest \fBfloat\fR, a value past the largest to \fBInfinity\fR, and never stops the program. .PP A \fBfloat\fR becomes a \fBdouble\fR on its own, exactly; the other way is \fB(float)x\fR\&. \fBsbyte\fR, \fBbyte\fR, \fBshort\fR and \fBushort\fR become a \fBfloat\fR on their own; an \fBint\fR, a \fBuint\fR, a \fBlong\fR and the others need \fB(float)n\fR, which rounds past 2^24. A \fBfloat\fR beside a \fBdouble\fR makes the operation a \fBdouble\fR\&'s; a number written beside a \fBfloat\fR is a \fBfloat\fR\&. A \fBfloat\fR is computed as a \fBfloat\fR: each operation is rounded to 32 bits on its own, in the interpreter and in compiled code alike, so both give the same digits. .PP As for a \fBdouble\fR, the arithmetic and \fB==\fR are the hardware's: a division by zero gives \fBInfinity\fR or \fBNaN\fR, and \fBNaN\fR equals nothing. \fB(int)x\fR drops the fraction and stops the program for a value that does not fit, \fBNaN\fR included, in the interpreter, in tests and in a debug build; a release build gives the nearest value, and 0 for \fBNaN\fR\&. .PP A \fBfloat\fR is printed with the fewest digits that read back as the same \fBfloat\fR: \fB0.1\fR, \fB3.4028235e+38\fR\&. \fB{x:F2}\fR rounds the exact value. A function of C takes and gives a \fBfloat\fR as it is, but after the \fB\&...\fR of a function like \fBprintf\fR, where C would make it a \fBdouble\fR without a word, a \fBfloat\fR is refused, and the hint is \fB(double)x\fR\&. The methods are in Numbers; in compiled code a \fBfloat\fR is C's \fBfloat\fR\&. .SH EXAMPLES .SS "Written and printed" .RS 4 .nf void main() { float small = 0.1 var wide = 0.1 float rounded = (float)wide float count = 3 print($"{small} {wide} {rounded} {count}") float third = 1.0 / 3.0 print($"{third} {(double)third}") print($"{float.MaxValue} {float.Pi} {float.E}") print($"{third:F3} {float.Pi:F10}") } .fi .RE .PP .RS 4 .nf 0.1 0.1 0.1 3 0.33333334 0.3333333432674408 3.4028235e+38 3.1415927 2.7182817 0.333 3.1415927410 .fi .RE .PP \fBwide\fR is a \fBdouble\fR, since nothing says otherwise. A \fBfloat\fR is printed with the digits a \fBfloat\fR needs, so \fBthird\fR shows eight; as a \fBdouble\fR the same value shows what it holds. \fBF10\fR writes more digits than the \fBfloat\fR has, and they come from its exact value, not from π. .SS "Seven digits" .RS 4 .nf void main() { float fsum = 0 double dsum = 0 for _ in 0..1_000_000 { fsum += 0.1 dsum += 0.1 } print($"{fsum} {dsum}") float count = 16_777_216 count += 1 print(count) print($"{(float)16_777_217.0} {(float)16_777_218.0}") float price = 19.99 print($"{price} {(double)price} {price:F2}") } .fi .RE .PP .RS 4 .nf 100958.34 100000.00000133288 16777216 16777216 16777218 19.99 19.989999771118164 19.99 .fi .RE .PP A million additions of 0.1 in a \fBfloat\fR miss by almost 1 %, since each sum is rounded to 7 digits; a \fBdouble\fR misses in the eleventh. Past 2^24 a \fBfloat\fR has no room for the ones: 16 777 216 plus 1 is 16 777 216 again. .SS "A float meets a double and an integer" .RS 4 .nf void main() { float f = 0.1 double d = 0.1 print($"{f == d} {f == (float)d} {f + d}") var scaled = f * 2.5 float back = scaled print(back) short width = 640 float wide = width int height = 480 var ratio = (float)height / wide print($"{wide * 0.5} {ratio}") } .fi .RE .PP .RS 4 .nf false true 0.20000000149011612 0.25 320 0.75 .fi .RE .PP \fBf == d\fR makes \fBf\fR a \fBdouble\fR first, and the \fBdouble\fR nearest 0.1 is not the \fBfloat\fR nearest it; \fB(float)d\fR compares as \fBfloat\fRs. \fBf * 2.5\fR stays a \fBfloat\fR, since the number takes the type of \fBf\fR\&. A \fBshort\fR becomes a \fBfloat\fR on its own; an \fBint\fR is written \fB(float)height\fR\&. .SS "Infinity and NaN" .RS 4 .nf void main() { float top = float.MaxValue print($"{top * 2} {-top * 2}") float big = 1e30 print($"{big * big} {(double)big * big}") float zero = 0 var none = zero / zero print($"{none} {none == none} {none.isNaN()}") print($"{(float)1e300} {(float)1e-300}") var values = List { 2.5, none, -1, float.Infinity } values.sort() print(values) } .fi .RE .PP .RS 4 .nf Infinity -Infinity Infinity 1.0000000300949327e+60 NaN false true Infinity 0 [NaN, -1, 2.5, Infinity] .fi .RE .PP Nothing stops the program. \fBbig * big\fR passes the largest \fBfloat\fR and is \fBInfinity\fR; done as \fBdouble\fRs the same product fits. \fB(float)\fR of a \fBdouble\fR too large or too small gives \fBInfinity\fR or 0. \fBsort\fR puts \fBNaN\fR first. .SS "The bits of a float" .RS 4 .nf void main() { float one = 1 print($"{one.toBits():x8} {(-one).toBits():x8} {float.fromBits(0x40490fdb)}") float zero = 0 var minus = -zero print($"{minus} {minus == zero} {minus.toBits():x8}") var smallest = float.fromBits(1) print($"{smallest} {smallest > 0} {float.fromBits(0x7f7fffff)}") var tagged = float.fromBits(0x7fc0_0042) print($"{tagged.isNaN()} {tagged.toBits() & 0xff:x2}") print($"{one.nextUp() - one} {one.nextUp().toBits() - one.toBits()}") } .fi .RE .PP .RS 4 .nf 3f800000 bf800000 3.1415927 -0 true 80000000 1e-45 true 3.4028235e+38 true 42 1.1920929e-7 1 .fi .RE .PP \fBtoBits()\fR gives the 32 bits as a \fBuint\fR and \fBfloat.fromBits\fR reads them back; every pattern is a \fBfloat\fR\&. \fB\-0\fR equals 0 and has a bit of its own. The smallest \fBfloat\fR above 0 is about 1.4e-45, printed with the one digit that reads back. A \fBNaN\fR keeps the bits it is given. The step after 1 is one bit, about 1.2e-7. .SS "A float to C" .RS 4 .nf extern "m" { float sqrtf(float x) float hypotf(float x, float y) } extern { int printf(read CString format, ...) } void main() { float side = 2 float root = unsafe sqrtf(side) print($"{root} {root == side.sqrt()} {unsafe hypotf(3, 4)}") var format = CString.from("%.9f\en") ?? panic("a zero byte inside") _ = unsafe printf(read format, (double)root) } .fi .RE .PP .RS 4 .nf 1.4142135 true 5 1.414213538 .fi .RE .PP \fBsqrtf\fR of C's \fB\fR takes and gives a \fBfloat\fR; \fBsqrt\fR on a \fBfloat\fR gives the same value. After the \fB\&...\fR of \fBprintf\fR the value is written \fB(double)root\fR, which is what C reads there. .SS "Read from text" .RS 4 .nf void show(read string text) { match float.parse(text) { Ok(v) => print($"{text} -> {v}") Error(e) => print($"{text} -> {e}") } } void main() { show("0.1") show("3.4028235e38") show("1e39") show("16777217") show("1e-50") show("1,5") } .fi .RE .PP .RS 4 .nf 0.1 -> 0.1 3.4028235e38 -> 3.4028235e+38 1e39 -> Infinity 16777217 -> 16777216 1e-50 -> 0 1,5 -> Invalid(1) .fi .RE .PP The value is the nearest \fBfloat\fR: past the largest it is \fBInfinity\fR, below the smallest 0. A whole number past 2^24 is rounded too, as a number written in the program is not. .SH "WHAT STOPS THE PROGRAM" .SS "A float that does not fit the integer" .RS 4 .nf int pixels(float size) { return (int)size } void main() { print(pixels(640.5)) print(pixels(3e9)) } .fi .RE .PP .RS 4 .nf 640 panic: value 3000000000 does not fit in int --> example.sg:2:12 2 | return (int)size | ^^^^^^^^^ hint: `tryToInt()` gives `null` when the value does not fit .fi .RE .PP In a release build \fB(int)3e9\fR is 2147483647, the nearest \fBint\fR; \fBsize.saturatingToInt()\fR gives it in every build. .SS "NaN as an integer" .RS 4 .nf int percent(float done, float total) { return (int)(done / total * 100) } void main() { print(percent(0, 0)) } .fi .RE .PP .RS 4 .nf panic: value NaN does not fit in int --> example.sg:2:12 2 | return (int)(done / total * 100) | ^^^^^^^^^^^^^^^^^^^^^^^^^ hint: `tryToInt()` gives `null` when the value does not fit .fi .RE .PP A release build gives 0. .SH "WHAT THE COMPILER REFUSES" .SS "A double where a float goes" .RS 4 .nf float half(float x) { return x / 2 } void main() { double size = 3.5 print(half(size)) } .fi .RE .PP .RS 4 .nf error: expected `float`, found `double` --> example.sg:7:16 7 | print(half(size)) | ^^^^ hint: convert with `(float)`, which rounds .fi .RE .SS "An int and a float" .RS 4 .nf void main() { float scale = 0.5 int width = 640 print(width * scale) } .fi .RE .PP .RS 4 .nf error: `int` and `float` do not mix: a `float` does not hold every `int` exactly --> example.sg:4:11 4 | print(width * scale) | ^^^^^ hint: convert it: `(float)width`, which rounds past 2^24 .fi .RE .SS "A whole number a float does not hold" .RS 4 .nf void main() { float count = 16_777_217 print(count) } .fi .RE .PP .RS 4 .nf error: `16_777_217` is not held exactly by a `float` a `float` holds every whole number up to 2^24 --> example.sg:2:19 2 | float count = 16_777_217 | ^^^^^^^^^^ hint: written with a point it is rounded to the nearest, 16777216: `16_777_217.0` .fi .RE .PP \fB16_777_217.0\fR, a fraction, rounds to the nearest \fBfloat\fR, as does \fB(float)16_777_217\fR\&. .SS "A number past the largest" .RS 4 .nf void main() { float big = 1e39 print(big) } .fi .RE .PP .RS 4 .nf error: `1e39` does not fit in float --> example.sg:2:17 2 | float big = 1e39 | ^^^^ .fi .RE .PP A number that would be 0 in a \fBfloat\fR, as \fB1e\-46\fR, is refused too; \fBdouble\fR holds both. .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 | ^^^^ hint: `float half = 0.5` .fi .RE .SS "A float after ..." .RS 4 .nf extern { int printf(read CString format, ...) } void main() { var format = CString.from("%f\en") ?? panic("a zero byte inside") float x = 1.5 _ = unsafe printf(read format, x) } .fi .RE .PP .RS 4 .nf error: a `float` does not go to C after `...` C reads it there as a `double`, made wider without a word --> example.sg:8:36 8 | _ = unsafe printf(read format, x) | ^ hint: `(double)x` .fi .RE .SS "Bits of a float" .RS 4 .nf void main() { float x = 1.5 print(x & 1) } .fi .RE .PP .RS 4 .nf error: operator `&` is not defined for `float` and `float` the bit operators work on integers --> example.sg:3:11 3 | print(x & 1) | ^^^^^ hint: `x.toBits()` gives its bits as a `uint` .fi .RE .SH "SEE ALSO" .BR sg-double (7), .BR sg-int (7), .BR sg-short (7), .BR sg-extern (7) .PP https://sg\-lang.org/reference/float.html