Implements the floating point algorithms described in https://research.swtch.com/fp.
I recommend reading the above for an insight into the technical details of how it works. History of this implementation can be found at https://codeberg.org/tiehuis/zig-fp with some more in-depth measurements. I refer to this new implementation as fp going forward.
I have extended the algorithms above to support arbitrary float types in addition to the provided f64 variant. This is also written such that it should be api-compatible, if I've missed anything let me know.
Performance
All build invocations below can run from the zig-fp repo
Generally, fp is ~2x faster than ryu for formatting, and 10-20% faster than eisel-lemire. I've also compared to a zig implementation of zmij which is generally considered the fastest formatting implementation and found it to be generally about the same speed. As a note, the reason I prefer fp to zmij is that the primitives I find more adaptable and I much prefer the same primitives and their usage for both formatting and parsing.
Like the previous implementation, fp is implemented with two backing engines. That is, every float equal or smaller than 64-bits uses the 64-bit engine, and all greater will be backed by a 128-bit engine.
Tests were run on AMD Ryzen 5 5600X 6-Core Processor.
Shortest (f64)
$ zig build perf -Doptimize=ReleaseFast -- --seed=0 --zmij=true
# mode=scientific precision=null seed=0x0 iters=10000 accumulator=0x1f357339e4ee5c3073c sample=null
fp: format: 14.49ns, parse: 16.50ns
std: format: 28.64ns, parse: 19.09ns
zmij: format: 16.51ns
Fixed Precision (f64)
$ zig build perf -Doptimize=ReleaseFast -- --seed=0 --precision=10
# mode=scientific precision=10 seed=0x0 iters=10000 accumulator=0x1f357339e4ee6365fc6 sample=null
fp: format: 14.34ns, parse: 14.02ns
std: format: 31.76ns, parse: 16.93ns
Large Floats
Floats larger than f64 now also support a faster parse variant. std would always parse these via the slow big int fallback which was a serious performance regression.
$ zig build perf -Doptimize=ReleaseFast -- --seed=0 --type=f128 --iters=1 --samples=1000
# mode=scientific precision=null seed=0x0 iters=1 accumulator=0x7ce6713de64edddac058d620af60ea10 sample=null
fp: format: 149.48ns, parse: 120.47ns
std: format: 220.73ns, parse: 1117267.61ns
Binary Size
This is summarized below, for different release types. The f64 engine uses compact tables only for small optimization modes. The f128 engine always uses compact tables.
f64 engine
| small | fast | |
|---|---|---|
| fp | 10.6KiB |
23.2KiB |
| std | 30.4KiB |
45.4KiB |
f128 engine
| small | fast | |
|---|---|---|
| fp | 20.9KiB |
24.4KiB |
| std | 30.8KiB |
43.1KiB |
Notable New Features
- Full precision output is now supported.
std.debug.format("{d.50}\n", .@as(f64, 123.1));
# std: 123.10000000000000000000000000000000000000000000000000
# fp: 123.09999999999999431565811391919851303100585937500000
This uses a new representation of the fallback big integer library. Note that I looked at other techniques such as table-driven approaches like floff. I found though that generally the performance gain was marginal and I preferred the simpler approach here. The table-based approach works well for f64 but is still quite costly for f128 even with super compact tables. The big-int approach as well I could re-use the implementation for fallback parsing for a dual-purpose.
- Case is now supported as an option when formatting. This currently only applies to special values (e.g. inf, nan). Could extend to exponent specifiers.
Testing
See https://codeberg.org/tiehuis/fp-testcases for floating point test-cases I've compiled. Programs exist in zig-fp which can run against this sample data. Instructions are in the readme.
- fuzz-tested against reference uscale c implementation
- exhaustively tested f16, f32 against std
- fuzz-tested f64, f80, f128 against std
std has been originally fuzz-tested against glibc.
#23942 is still an issue. Simple follow-up to use exact rounding for whole values.
Fixes #20619.
Fixes #22779.
Fixes #37103.
Fixes #37379.
Fixes #36208.
Fixes #37789.
Implements the floating point algorithms described in https://research.swtch.com/fp. I recommend reading the above for an insight into the technical details of how it works. History of this implementation can be found at https://codeberg.org/tiehuis/zig-fp with some more in-depth measurements. I refer to this new implementation as **fp** going forward. I have extended the algorithms above to support arbitrary float types in addition to the provided f64 variant. This is also written such that it should be api-compatible, if I've missed anything let me know. ## Performance > All build invocations below can run from the zig-fp repo Generally, **fp is ~2x faster than ryu for formatting**, and **10-20% faster than eisel-lemire**. I've also compared to a zig implementation of [zmij](https://github.com/vitaut/zmij) which is generally considered the fastest formatting implementation and found it to be generally about the same speed. As a note, the reason I prefer fp to zmij is that the primitives I find more adaptable and I much prefer the same primitives and their usage for both formatting and parsing. Like the previous implementation, fp is implemented with two backing engines. That is, every float equal or smaller than 64-bits uses the 64-bit engine, and all greater will be backed by a 128-bit engine. Tests were run on `AMD Ryzen 5 5600X 6-Core Processor`. ### Shortest (f64) ``` $ zig build perf -Doptimize=ReleaseFast -- --seed=0 --zmij=true # mode=scientific precision=null seed=0x0 iters=10000 accumulator=0x1f357339e4ee5c3073c sample=null fp: format: 14.49ns, parse: 16.50ns std: format: 28.64ns, parse: 19.09ns zmij: format: 16.51ns ``` ### Fixed Precision (f64) ``` $ zig build perf -Doptimize=ReleaseFast -- --seed=0 --precision=10 # mode=scientific precision=10 seed=0x0 iters=10000 accumulator=0x1f357339e4ee6365fc6 sample=null fp: format: 14.34ns, parse: 14.02ns std: format: 31.76ns, parse: 16.93ns ``` ### Large Floats Floats larger than f64 now also support a faster parse variant. std would always parse these via the slow big int fallback which was a serious performance regression. ``` $ zig build perf -Doptimize=ReleaseFast -- --seed=0 --type=f128 --iters=1 --samples=1000 # mode=scientific precision=null seed=0x0 iters=1 accumulator=0x7ce6713de64edddac058d620af60ea10 sample=null fp: format: 149.48ns, parse: 120.47ns std: format: 220.73ns, parse: 1117267.61ns ``` ### Binary Size This is summarized below, for different release types. The f64 engine uses compact tables only for small optimization modes. The f128 engine always uses compact tables. #### f64 engine | | small | fast | | ------ | ----- | ------- | | **fp** | `10.6KiB` | `23.2KiB` | | **std** | `30.4KiB` | `45.4KiB` | ### f128 engine | | small | fast | | ------ | ----- | ------- | | **fp** | `20.9KiB` | `24.4KiB` | | **std** | `30.8KiB` | `43.1KiB` | ## Notable New Features - Full precision output is now supported. ```zig std.debug.format("{d.50}\n", .@as(f64, 123.1)); # std: 123.10000000000000000000000000000000000000000000000000 # fp: 123.09999999999999431565811391919851303100585937500000 ``` This uses a new representation of the fallback big integer library. Note that I looked at other techniques such as table-driven approaches like [floff](https://github.com/jk-jeon/floff). I found though that generally the performance gain was marginal and I preferred the simpler approach here. The table-based approach works well for f64 but is still quite costly for f128 even with super compact tables. The big-int approach as well I could re-use the implementation for fallback parsing for a dual-purpose. - Case is now supported as an option when formatting. This currently only applies to special values (e.g. inf, nan). Could extend to exponent specifiers. ## Testing See https://codeberg.org/tiehuis/fp-testcases for floating point test-cases I've compiled. Programs exist in zig-fp which can run against this sample data. Instructions are in the readme. - fuzz-tested against reference uscale c implementation - exhaustively tested f16, f32 against std - fuzz-tested f64, f80, f128 against std std has been originally fuzz-tested against glibc. --- [#23942](https://github.com/ziglang/zig/issues/23942) is still an issue. Simple follow-up to use exact rounding for whole values. Fixes [#20619](https://github.com/ziglang/zig/issues/20619). Fixes [#22779](https://github.com/ziglang/zig/issues/22779). Fixes #37103. Fixes #37379. Fixes #36208. Fixes #37789.