Skip to main content

ryu_js/
lib.rs

1//! ECMAScript compliant pure Rust implementation of Ryū, an algorithm to quickly
2//! convert floating point numbers to decimal strings.
3//!
4//! The PLDI'18 paper [*Ryū: fast float-to-string conversion*][paper] by Ulf
5//! Adams includes a complete correctness proof of the algorithm. The paper is
6//! available under the creative commons CC-BY-SA license.
7//!
8//! This Rust implementation is a line-by-line port of Ulf Adams' implementation
9//! in C, [https://github.com/ulfjack/ryu][upstream].
10//!
11//! [paper]: https://dl.acm.org/citation.cfm?id=3192369
12//! [upstream]: https://github.com/ulfjack/ryu
13//!
14//! # Example
15//!
16//! ```
17//! let mut buffer = ryu_js::Buffer::new();
18//! let printed = buffer.format(1.234);
19//! assert_eq!(printed, "1.234");
20//! ```
21//!
22//! ## Performance
23//!
24//! You can run upstream's benchmarks with:
25//!
26//! ```console
27//! $ git clone https://github.com/ulfjack/ryu c-ryu
28//! $ cd c-ryu
29//! $ bazel run -c opt //ryu/benchmark
30//! ```
31//!
32//! And the same benchmark against our implementation with:
33//!
34//! ```console
35//! $ git clone https://github.com/boa-dev/ryu-js rust-ryu
36//! $ cd rust-ryu
37//! $ cargo run --example upstream_benchmark --release
38//! ```
39//!
40//! The benchmarks measure the average time to print a 32-bit float and average
41//! time to print a 64-bit float, where the inputs are distributed as uniform random
42//! bit patterns 32 and 64 bits wide.
43//!
44//! The upstream C code, the unsafe direct Rust port, and the safe pretty Rust API
45//! all perform the same, taking around 21 nanoseconds to format a 32-bit float and
46//! 31 nanoseconds to format a 64-bit float.
47//!
48//! There is also a Rust-specific benchmark comparing this implementation to the
49//! standard library which you can run with:
50//!
51//! ```console
52//! $ cargo bench
53//! ```
54//!
55//! The benchmark shows Ryū approximately 2-5x faster than the standard library
56//! across a range of f32 and f64 inputs. Measurements are in nanoseconds per
57//! iteration; smaller is better.
58//!
59//! ## Formatting
60//!
61//! This library tends to produce more human-readable output than the standard
62//! library's to\_string, which never uses scientific notation. Here are two
63//! examples:
64//!
65//! - *ryu:* 1.23e40, *std:* 12300000000000000000000000000000000000000
66//! - *ryu:* 1.23e-40, *std:* 0.000000000000000000000000000000000000000123
67//!
68//! Both libraries print short decimals such as 0.0000123 without scientific
69//! notation.
70
71#![no_std]
72#![allow(
73    clippy::cast_lossless,
74    clippy::cast_possible_truncation,
75    clippy::cast_possible_wrap,
76    clippy::cast_sign_loss,
77    clippy::checked_conversions,
78    clippy::doc_markdown,
79    clippy::expl_impl_clone_on_copy,
80    clippy::if_not_else,
81    clippy::many_single_char_names,
82    clippy::missing_panics_doc,
83    clippy::module_name_repetitions,
84    clippy::must_use_candidate,
85    clippy::needless_doctest_main,
86    clippy::similar_names,
87    clippy::too_many_lines,
88    clippy::unreadable_literal,
89    clippy::unseparated_literal_suffix,
90    clippy::wildcard_imports
91)]
92
93mod buffer;
94mod common;
95mod d2s;
96#[cfg(not(feature = "small"))]
97mod d2s_full_table;
98mod d2s_intrinsics;
99#[cfg(feature = "small")]
100mod d2s_small_table;
101mod digit_table;
102mod f2s;
103mod f2s_intrinsics;
104mod pretty;
105#[cfg(test)]
106mod tests;
107
108pub use crate::buffer::{Buffer, Float, FloatToFixed};
109
110/// Unsafe functions that mirror the API of the C implementation of Ryū.
111pub mod raw {
112    pub use crate::pretty::format64_to_fixed;
113    pub use crate::pretty::{format32, format64};
114}