1# Ryū 2 3[<img alt="github" src="https://img.shields.io/badge/github-dtolnay/ryu-8da0cb?style=for-the-badge&labelColor=555555&logo=github" height="20">](https://github.com/dtolnay/ryu) 4[<img alt="crates.io" src="https://img.shields.io/crates/v/ryu.svg?style=for-the-badge&color=fc8d62&logo=rust" height="20">](https://crates.io/crates/ryu) 5[<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-ryu-66c2a5?style=for-the-badge&labelColor=555555&logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K" height="20">](https://docs.rs/ryu) 6[<img alt="build status" src="https://img.shields.io/github/workflow/status/dtolnay/ryu/CI/master?style=for-the-badge" height="20">](https://github.com/dtolnay/ryu/actions?query=branch%3Amaster) 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.36; 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/abf76d252bc97300354857e64e80d4a2bf664291 23 24```toml 25[dependencies] 26ryu = "1.0" 27``` 28 29<br> 30 31## Example 32 33```rust 34fn main() { 35 let mut buffer = ryu::Buffer::new(); 36 let printed = buffer.format(1.234); 37 assert_eq!(printed, "1.234"); 38} 39``` 40 41<br> 42 43## Performance (lower is better) 44 45 46 47You can run upstream's benchmarks with: 48 49```console 50$ git clone https://github.com/ulfjack/ryu c-ryu 51$ cd c-ryu 52$ bazel run -c opt //ryu/benchmark:ryu_benchmark 53``` 54 55And the same benchmark against our implementation with: 56 57```console 58$ git clone https://github.com/dtolnay/ryu rust-ryu 59$ cd rust-ryu 60$ cargo run --example upstream_benchmark --release 61``` 62 63These benchmarks measure the average time to print a 32-bit float and average 64time to print a 64-bit float, where the inputs are distributed as uniform random 65bit patterns 32 and 64 bits wide. 66 67The upstream C code, the unsafe direct Rust port, and the safe pretty Rust API 68all perform the same, taking around 21 nanoseconds to format a 32-bit float and 6931 nanoseconds to format a 64-bit float. 70 71There is also a Rust-specific benchmark comparing this implementation to the 72standard library which you can run with: 73 74```console 75$ cargo bench 76``` 77 78The benchmark shows Ryū approximately 2-5x faster than the standard library 79across a range of f32 and f64 inputs. Measurements are in nanoseconds per 80iteration; smaller is better. 81 82## Formatting 83 84This library tends to produce more human-readable output than the standard 85library's to\_string, which never uses scientific notation. Here are two 86examples: 87 88- *ryu:* 1.23e40, *std:* 12300000000000000000000000000000000000000 89- *ryu:* 1.23e-40, *std:* 0.000000000000000000000000000000000000000123 90 91Both libraries print short decimals such as 0.0000123 without scientific 92notation. 93 94<br> 95 96#### License 97 98<sup> 99Licensed under either of <a href="LICENSE-APACHE">Apache License, Version 1002.0</a> or <a href="LICENSE-BOOST">Boost Software License 1.0</a> at your 101option. 102</sup> 103 104<br> 105 106<sub> 107Unless you explicitly state otherwise, any contribution intentionally submitted 108for inclusion in this crate by you, as defined in the Apache-2.0 license, shall 109be dual licensed as above, without any additional terms or conditions. 110</sub> 111