• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

benches/H03-May-2022-5948

examples/H03-May-2022-8974

src/H03-May-2022-2,4421,859

tests/H03-May-2022-614438

.cargo-checksum.jsonH A D03-May-202289 11

.cargo_vcs_info.jsonH A D01-Jan-197074 65

.gitignoreH A D25-Apr-201930 43

.travis.ymlH A D11-Jun-2019350 1917

Cargo.lockH A D01-Jan-19706.1 KiB145127

Cargo.tomlH A D01-Jan-19701 KiB3632

Cargo.toml.orig-cargoH A D14-Oct-2019769 2823

LICENSE-APACHEH A D25-Apr-201911.1 KiB202169

LICENSE-BOOSTH A D25-Apr-20191.3 KiB2420

README.mdH A D11-Jun-20193.7 KiB11584

build.rsH A D14-Oct-20191.9 KiB6743

README.md

1# Ryū
2
3[![Build Status](https://api.travis-ci.org/dtolnay/ryu.svg?branch=master)](https://travis-ci.org/dtolnay/ryu)
4[![Latest Version](https://img.shields.io/crates/v/ryu.svg)](https://crates.io/crates/ryu)
5[![Rust Documentation](https://img.shields.io/badge/api-rustdoc-blue.svg)](https://docs.rs/ryu)
6[![Rustc Version 1.15+](https://img.shields.io/badge/rustc-1.15+-lightgray.svg)](https://blog.rust-lang.org/2017/02/02/Rust-1.15.html)
7
8Pure Rust implementation of Ryū, an algorithm to quickly convert floating point
9numbers to decimal strings.
10
11The PLDI'18 paper [*Ryū: fast float-to-string conversion*][paper] by Ulf Adams
12includes a complete correctness proof of the algorithm. The paper is available
13under the creative commons CC-BY-SA license.
14
15This Rust implementation is a line-by-line port of Ulf Adams' implementation in
16C, [https://github.com/ulfjack/ryu][upstream].
17
18*Requirements: this crate supports any compiler version back to rustc 1.15; it
19uses nothing from the Rust standard library so is usable from no_std crates.*
20
21[paper]: https://dl.acm.org/citation.cfm?id=3192369
22[upstream]: https://github.com/ulfjack/ryu/tree/688f43b62276b400728baad54afc32c3ab9c1a95
23
24```toml
25[dependencies]
26ryu = "1.0"
27```
28
29## Example
30
31```rust
32fn main() {
33    let mut buffer = ryu::Buffer::new();
34    let printed = buffer.format(1.234);
35    assert_eq!(printed, "1.234");
36}
37```
38
39## Performance
40
41You can run upstream's benchmarks with:
42
43```console
44$ git clone https://github.com/ulfjack/ryu c-ryu
45$ cd c-ryu
46$ bazel run -c opt //ryu/benchmark
47```
48
49And the same benchmark against our implementation with:
50
51```console
52$ git clone https://github.com/dtolnay/ryu rust-ryu
53$ cd rust-ryu
54$ cargo run --example upstream_benchmark --release
55```
56
57These benchmarks measure the average time to print a 32-bit float and average
58time to print a 64-bit float, where the inputs are distributed as uniform random
59bit patterns 32 and 64 bits wide.
60
61The upstream C code, the unsafe direct Rust port, and the safe pretty Rust API
62all perform the same, taking around 21 nanoseconds to format a 32-bit float and
6331 nanoseconds to format a 64-bit float.
64
65There is also a Rust-specific benchmark comparing this implementation to the
66standard library which you can run with:
67
68```console
69$ cargo bench
70```
71
72The benchmark shows Ryu approximately 4-10x faster than the standard library
73across a range of f32 and f64 inputs. Measurements are in nanoseconds per
74iteration; smaller is better.
75
76| type=f32 | 0.0  | 0.1234 | 2.718281828459045 | f32::MAX |
77|:--------:|:----:|:------:|:-----------------:|:--------:|
78| RYU      | 3ns  | 28ns   | 23ns              | 22ns     |
79| STD      | 40ns | 106ns  | 128ns             | 110ns    |
80
81| type=f64 | 0.0  | 0.1234 | 2.718281828459045 | f64::MAX |
82|:--------:|:----:|:------:|:-----------------:|:--------:|
83| RYU      | 3ns  | 50ns   | 35ns              | 32ns     |
84| STD      | 39ns | 105ns  | 128ns             | 202ns    |
85
86## Formatting
87
88This library tends to produce more human-readable output than the standard
89library's to\_string, which never uses scientific notation. Here are two
90examples:
91
92- *ryu:* 1.23e40, *std:* 12300000000000000000000000000000000000000
93- *ryu:* 1.23e-40, *std:* 0.000000000000000000000000000000000000000123
94
95Both libraries print short decimals such as 0.0000123 without scientific
96notation.
97
98<br>
99
100#### License
101
102<sup>
103Licensed under either of <a href="LICENSE-APACHE">Apache License, Version
1042.0</a> or <a href="LICENSE-BOOST">Boost Software License 1.0</a> at your
105option.
106</sup>
107
108<br>
109
110<sub>
111Unless you explicitly state otherwise, any contribution intentionally submitted
112for inclusion in this crate by you, as defined in the Apache-2.0 license, shall
113be dual licensed as above, without any additional terms or conditions.
114</sub>
115