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 (lower is better)
23//!
24//! The benchmarks measure the average time to print a 32-bit float and average
25//! time to print a 64-bit float, where the inputs are distributed as uniform random
26//! bit patterns 32 and 64 bits wide.
27//!
28//! The upstream C code, the unsafe direct Rust port, and the safe pretty Rust API
29//! all perform the same, taking around 21 nanoseconds to format a 32-bit float and
30//! 31 nanoseconds to format a 64-bit float.
31//!
32//! There is also a Rust-specific benchmark comparing this implementation to the
33//! standard library which you can run with:
34//!
35//! ```console
36//! $ cargo bench
37//! ```
38//!
39//! The benchmark shows Ryū approximately 2-5x faster than the standard library
40//! across a range of f32 and f64 inputs. Measurements are in nanoseconds per
41//! iteration; smaller is better.
42//!
43//! ## Formatting
44//!
45//! This library tends to produce more human-readable output than the standard
46//! library's to\_string, which never uses scientific notation. Here are two
47//! examples:
48//!
49//! - *ryu:* 1.23e40, *std:* 12300000000000000000000000000000000000000
50//! - *ryu:* 1.23e-40, *std:* 0.000000000000000000000000000000000000000123
51//!
52//! Both libraries print short decimals such as 0.0000123 without scientific
53//! notation.
54
55#![no_std]
56#![allow(
57    clippy::cast_lossless,
58    clippy::cast_possible_truncation,
59    clippy::cast_possible_wrap,
60    clippy::cast_sign_loss,
61    clippy::checked_conversions,
62    clippy::doc_markdown,
63    clippy::expl_impl_clone_on_copy,
64    clippy::if_not_else,
65    clippy::many_single_char_names,
66    clippy::missing_panics_doc,
67    clippy::module_name_repetitions,
68    clippy::must_use_candidate,
69    clippy::needless_doctest_main,
70    clippy::similar_names,
71    clippy::too_many_lines,
72    clippy::unreadable_literal,
73    clippy::unseparated_literal_suffix,
74    clippy::wildcard_imports
75)]
76
77mod buffer;
78mod common;
79mod d2s;
80#[cfg(not(feature = "small"))]
81mod d2s_full_table;
82mod d2s_intrinsics;
83#[cfg(feature = "small")]
84mod d2s_small_table;
85mod digit_table;
86mod f2s;
87mod f2s_intrinsics;
88mod pretty;
89
90pub use crate::buffer::{Buffer, Float, FloatToFixed};
91
92/// Unsafe functions that mirror the API of the C implementation of Ryū.
93pub mod raw {
94    pub use crate::pretty::format64_to_fixed;
95    pub use crate::pretty::{format32, format64};
96}