man uiua/numbers
Numbers in Uiua: everything is a float
What is a number in Uiua? The language has no integer type to declare and no
int/float split to think about. Counts, indices, booleans and
measurements all look the same, and type returns 0 for every one of them.
This article reads the source of Uiua 0.19.1 (tag 0.19.1 of
uiua-lang/uiua) to see what is stored underneath, and runs experiments to
see where that shows. Each .ua file runs with uiua run --no-format.
The short version
- The docs say numbers “use the IEEE-754 double-precision floating-point format”. Integers are exact up to 2⁵³ and silently rounded above it.
- The interpreter has two real-number storage types,
f64arrays andu8arrays. Number literals and every value bound to a name are stored as bytes when all their elements are whole numbers from 0 to 255. Comparisons return bytes too. Programs can’t see the difference, but sorting can: binding an array to a name made⍆20 times faster in one experiment below. - Indices, counts and grades come out of their primitives as
f64. A primitive that needs an integer converts each element and rejects fractions. &pprints with Rust’s float formatting, which never uses scientific notation. The interactive display uses¯for minus and…for floating-point noise.NaNequalsNaN, sorts after everything, and two special NaN bit patterns mark the empty and deleted slots of hash maps.
One type on the surface
From the Types tutorial at the same tag: “Numbers are decimal numbers with
floating precision. They use the IEEE-754 double-precision floating-point
format.” There is a separate complex type, and type tells the four kinds
apart: 0 for real numbers, 1 for characters, 2 for boxes, 3 for complex
numbers. Everything below is about the 0.
$ cat nums.ua
&p type 5
&p type 1.5
&p type =1 1
&p = ⁿ53 2 +1 ⁿ53 2
&p /× [99999 99999 99999]
&p /× [999999 999999 999999]
$ uiua run --no-format nums.ua
0
0
0
1
999970000299999
999997000002999900
=1 1 is a comparison result, stored as a byte, and type still says 0.
ⁿ53 2 is 2⁵³, and adding 1 to it gives back the same number, so =
returns 1. The product of three five-digit numbers is exact. The product of
three six-digit numbers is 999,997,000,002,999,999, which needs 60 bits; the
nearest double prints as 999997000002999900. Uiua raises no error and gives
no warning. The ⋯ (bits) docs carry the only warning I found: conversions
“performed on large numbers (over 53 bits long) may give incorrect results”.
Two types underneath
The runtime value is an enum in value.rs:
pub enum Value {
/// Byte array used for some boolean operations and for I/O
Byte(Array<u8>),
/// Number array
Num(Array<f64>),
/// Complex number array
Complex(Array<Complex>),
/// Character array
Char(Array<char>),
/// Box array
Box(Array<Boxed>),
// ...
}
Both u8 and f64 implement the ArrayValue trait with the same name,
"number", the same symbol ℝ and the same TYPE_ID of 0, which is why
type can’t tell them apart. A byte hashes as the bits of the equal f64
((*self as f64).to_bits().hash(hasher)), so ◴ (deduplicate) and ⊛
(classify) treat a byte 1 and a float 1 as the same value.
Where do bytes come from? Comparisons, first: the num_num function behind
=, < and the rest returns u8. Reading a file or stream as bytes (&rb,
&frab) gives bytes, and so do a few primitives that build masks, such as
the deprecated ◰ (unique).
The bigger source is a function called try_shrink, documented as “Turn a
number array into a byte array if no information is lost”. It checks that
every element is a whole number from 0 to 255, converts the array to bytes if
so, and sets the BOOLEAN flag if every element is 0 or 1. Two of its
callers matter in every program: Node::new_push, which the compiler uses for
literals, and the interpreter’s BindGlobal, which runs whenever a name is
bound to a value. The assembly that uiua build writes shows the difference,
because bytes serialize as JSON integers and f64 as JSON floats:
$ cat lit.ua
A ← 3
B ← 300
C ← [1 2 3]
D ← =1 [1 0 1]
G ← 2.5
&p [A B G]
$ uiua build lit.ua --output lit.uasm && grep -A5 BINDINGS lit.uasm
BINDINGS
const 3 1
const 300.0 2
const [1,2,3] 3
const [1,0,1] 5
const 2.5 8
3, [1 2 3] and the comparison are bytes; 300 and 2.5 are f64.
Arithmetic does not stay in bytes. add::byte_byte is
pub fn byte_byte(a: u8, b: u8) -> f64 {
f64::from(a) + f64::from(b)
}
so there is no 8-bit overflow to worry about. Arrays flagged BOOLEAN get
separate bool_bool versions of +, × and a few others that return bytes.
The storage type decides some algorithms. ⍆ (sort) on a list of bytes is a
counting sort over 256 buckets, while a list of f64 goes to rayon’s
parallel par_sort_unstable_by. That makes binding visible in timings. Here
a million random bytes are sorted as read, then converted to f64 without
being bound, then converted and bound to a name (⍜now measures the time of
the function inside it, and Ms turns seconds into milliseconds):
$ cat shrink.ua
Ms ← ÷100 ⁅ ×1e5
R ← &frab "rand.bin"
F ← -0.5 +0.5 R
&p $"bytes from &frab _ ms" Ms ⍜now(◌⍆) R
&p $"f64, never bound _ ms" Ms ⍜now(◌⍆) -0.5 +0.5 R
&p $"f64, bound to F _ ms" Ms ⍜now(◌⍆) F
$ head -c 1000000 /dev/urandom > rand.bin
$ uiua run --no-format shrink.ua
bytes from &frab 0.42 ms
f64, never bound 7.63 ms
f64, bound to F 0.39 ms
All three sort the same million values. F went through two float
operations, and binding it turned it back into bytes. Two more runs gave
0.36 to 0.37 ms for the bytes and 7.59 and 8.61 ms for the unbound floats;
the bound version took 0.37 ms once and 2.81 ms once.
This is a much smaller set than BQN’s CBQN, which packs numbers into bits or
1-, 2- or 4-byte integers depending on their range. In Uiua a list of whole
numbers from 0 to 255 is 8 bytes per element unless it was written as a
literal, bound to a name, or produced by a comparison or an I/O primitive.
Anything above 255 is always f64.
Indices and counts are floats
Every primitive that produces positions or counts builds an f64 array:
⍏(rise) and⍖(fall) build their result as a list off64indices,(0..self.row_count()).map(|i| i as f64), and sort that.⊛(classify) computesusizeclasses and converts them withiter.into_iter().map(|i| i as f64).collect().⊕⧻(group by length) and°⊚(un where) fill anEcoVec<f64>with the counts.
Bind one of those results to a name and, if its values are small enough,
try_shrink turns it into bytes after all.
When an index comes back in, say as the left argument of ⊏ (select), it is
checked element by element. A fraction is an error; a large integer is cast
with Rust’s as, which saturates:
$ uiua run --no-format idx.ua
Error: Index must be an array of integers, but 1.5 is not an integer
at idx.ua:1:1
1 | ⊏ 1.5 [10 20 30]
─
$ uiua eval '⊏ 1e20 [1 2 3]'
Error: Index 9223372036854775807 is out of bounds of length 3
1e20 became isize::MAX, 9223372036854775807, before the bounds check. Other
primitives that need natural numbers, such as °⊚, go through
as_number_list, which checks for NaN, infinity, fractions and range, and
names the problem in its error message.
Printing
There are two printers. &p (print) formats a scalar number with Rust’s
f64::to_string, which writes the shortest decimal that round-trips and
never switches to exponent notation. The interactive display, which shows what
is left on the stack when a program ends, has its own formatter in
grid_fmt.rs:
$ cat print.ua
&p ⁿ63 2
&p 1e22
&p + 0.1 0.2
&p ⋕ "¯5"
+ 0.1 0.2
¯5
[3 NaN 1 ∞ ¯∞]
$ uiua run --no-format print.ua
9223372036854776000
10000000000000000000000
0.30000000000000004
-5
0.300…04
¯5
[3 NaN 1 ∞ ¯∞]
&p writes -5 with an ASCII minus and the full 17 significant digits of
0.1 + 0.2. The display writes ¯5, Uiua’s own minus sign, and shortens the
floating-point noise to 0.300…04. The changelog for 0.9.0 (2024-02-25) added
that: “Numbers that seem to have a floating-point epsilon rounding error will
be output with the epsilon noted”. The display also recognizes multiples of π
and τ and prints them by name.
Parsing
⋕ (parse) on a string ends in Rust’s str::parse::<f64>(), after some
preparation in parse_uiua_num: surrounding spaces are trimmed, ¯ and a
backtick become -, the names π, τ, η and ∞ (or pi, tau, eta,
inf) become their values, and one / divides:
$ cat parse.ua
&p ⋕ "1/3"
&p ⋕ "¯5"
&p ⋕ "2π"
&p ⋕ " 42 "
&p ⋕ "1e3"
$ uiua run --no-format parse.ua
0.3333333333333333
-5
6.283185307179586
42
1000
It always returns an f64. A string such as "12345678901234567890" parses
to the nearest double and prints as 12345678901234567000.
NaN and the map sentinels
Comparison in Uiua goes through array_cmp, which for f64 is
partial_cmp, with a fallback for NaN: two NaNs compare equal and a NaN is
greater than any number. Hashing for ◴ and ⊛ maps every NaN to one bit
pattern and −0 to 0:
$ cat nan.ua
&p = NaN NaN
&p ⍆ [3 NaN 1 ∞ ¯∞]
&p ◴ [0 ¯0 NaN NaN]
$ uiua run --no-format nan.ua
1
[-inf 1 3 inf NaN]
[0 NaN]
That = NaN NaN is 1 makes NaN usable as a value: it can be deduplicated,
used as a map key (get NaN map [1 NaN] [5 6] returns 6). It is also why maps can use
NaN for their own bookkeeping. A Uiua map is an ordinary array of values
with the keys attached as metadata, in an open-addressing table with linear
probing and a load factor of 0.75. Its empty and deleted key slots hold two
specific NaN bit patterns:
// A NaN value used as empty, not the standard NaN.
pub const EMPTY_NAN: f64 = must_cast(0x7ff8_0000_0000_0000u64 | 0x0000_0000_0000_0001);
// A NaN value used as a tombstone, not the standard NaN.
pub const TOMBSTONE_NAN: f64 = must_cast(0x7ff8_0000_0000_0000u64 | 0x0000_0000_0000_0002);
The hash function keeps those two bit patterns apart from the single NaN it
maps every other NaN to.
¤ (fix) shows the raw table, where the display prints the two sentinels as
∅ and ⊥:
$ cat map.ua
M ← map [57 55 76] [6 1 4]
¤ M
¤ remove 55 M
$ uiua run --no-format map.ua
╭─
╷ [∅ 55 57 76] → [6 1 4]
╯
╭─
╷ [∅ ⊥ 57 76] → [6 4]
╯
Three keys sit in a table of four slots, and removing 55 leaves a tombstone where it was, while the values array simply loses its 1. Maps keyed by characters use two private-use code points the same way, U+2FFFF and U+2FFFE, since 0.18.0.
Curious facts
typereturns 0 for both storage types, and the binary encoder doesn’t keep the distinction either:binarypicks the narrowest integer or float width that holds the values, so a list off64counts below 256 is written one byte per element.ⁿ63 2prints as9223372036854776000. The last three digits are the shortest decimal that rounds to the same double, not the real value of 2⁶³, which ends in808.- A fractional index is an error, but an integer index of 1e20 is accepted and saturated to 9223372036854775807 before the bounds check fails.
⋕ "2π"is 6.283185307179586: constant names are part of the number syntax for parsing.&pprints infinity asinfinside an array ([-inf 1 3 inf NaN]); the stack display prints∞and¯∞.
History
- 0.8.0 (2024-01-31): experimental hash map functions arrive; “the internal byte array type is now used in more places”.
- 0.9.0 (2024-02-25): numbers with apparent epsilon errors are displayed with the epsilon noted.
- 0.10.0 (2024-04-04):
map,insert,has,getandremoveare no longer experimental. - 0.16.0 (2025-05-18):
⍆on lists of bytes uses counting sort. - 0.18.0 (2026-02-05): map empty and tombstone characters become U+2FFFF
and U+2FFFE;
maptakes an optional capacity. - 0.19.1 (2026-09-03): the version read here.
Sources
All at tag 0.19.1 of https://github.com/uiua-lang/uiua:
try_shrink: src/value.rs#L1342; called from src/tree.rs#L338 and src/run.rs#L576Value: src/value.rs#L29;&pformatting: src/value.rs#L561;usizetof64: src/value.rs#L1572; integer checks: src/value.rs#L629ArrayValueforf64andu8, hashing, counting sort: src/array.rs#L1057, #L1108;array_cmp: #L1282- Comparisons and byte arithmetic: src/algorithm/pervade.rs#L1360, #L1470
- Select’s index conversion: src/algorithm/dyadic/structure.rs#L88
- Rise and fall: src/algorithm/monadic/sort.rs#L57; un where: src/algorithm/monadic/mod.rs#L2007; parse: #L224, #L632
- Display formatting: src/grid_fmt.rs#L132
- Maps and the NaN sentinels: src/algorithm/map.rs#L886
- Binary encoding widths: src/algorithm/encode.rs#L603
- Types tutorial: site/text/tutorial/Types.md
⋯(bits) docs: parser/src/defs.rs#L1108- Changelog: changelog.md
grep -rl /lib/uiua/numbers/
ls uiua/
- draft How Uiua runs: stack, arrays and special-cased idiomsWhat the Uiua 0.19.1 interpreter does with a line of glyphs: a tree of nodes, a peephole optimizer that rewrites known idioms, fast paths inside primitives, and one idiom from the docs that misses its fast path.