Package 'zmij'

Title: Round-Trip-Safe Double-Precision Formatting
Description: Converts double-precision floating-point values to decimal strings with the shortest significands needed for round-trip recovery. Uses the 'zmij' algorithm through a vendored 'Rust' implementation. The method is described in Zverovich (2025), "Faster Double-to-String Conversion" <https://vitaut.net/posts/2025/faster-dtoa/>. Provides vectorized formatting, a correctly rounded inverse parser, and explicit handling of missing and non-finite values.
Authors: Nan Xiao [aut, cre, cph] (ORCID: <https://orcid.org/0000-0002-0250-5673>), David Tolnay [ctb, cph] (Author of the vendored Rust zmij crate), Victor Zverovich [ctb, cph] (Author of the zmij algorithm and original C++ implementation)
Maintainer: Nan Xiao <[email protected]>
License: MIT + file LICENSE
Version: 0.1.0
Built: 2026-10-09 10:50:53 UTC
Source: https://github.com/nanxstats/zmij

Help Index


Format Double-Precision Values with zmij

Description

Convert numeric values to decimal strings with the shortest significands that recover the original IEEE 754 binary64 values when parsed using round-to-nearest, ties-to-even rounding.

Usage

format_double(x)

Arguments

x

A numeric vector, matrix, or array.

Details

Integer inputs are converted to double precision before formatting. zmij uses fixed notation for decimal exponents from -5 through 15 and scientific notation outside that range. Other attributes, including classes, are not retained because the returned values are character strings.

Value

A character vector with the same length and shape as x. Names and dimensions are preserved. Missing values remain NA; NaN, positive infinity, and negative infinity become "NaN", "inf", and "-inf", respectively.

References

Zverovich V (2025). "Faster double-to-string conversion." https://vitaut.net/posts/2025/faster-dtoa/.

Tolnay D. "zmij: A double-to-string conversion algorithm based on Schubfach." https://github.com/dtolnay/zmij.

Examples

x <- c(pi, 0.1, -0, .Machine$double.xmin, .Machine$double.xmax)
formatted <- format_double(x)
formatted
identical(parse_double(formatted), x)

format_double(c(NA_real_, NaN, Inf, -Inf))

Parse Double-Precision Strings

Description

Parse decimal strings into IEEE 754 binary64 values using Rust's correctly rounded parser. This is the inverse of format_double() for every finite double.

Usage

parse_double(x)

Arguments

x

A character vector, matrix, or array.

Details

The parser accepts decimal and scientific notation, along with "NaN", "inf", and "-inf". Leading or trailing whitespace and other invalid strings cause an error that identifies the first invalid element.

Using parse_double() avoids platform-specific edge cases in R's built-in decimal parser near the limits of the binary64 range.

Value

A double vector with the same length and shape as x. Names and dimensions are preserved. Missing strings remain NA_real_.

Examples

x <- c(pi, 0.1, -0, .Machine$double.xmin, .Machine$double.xmax)
identical(parse_double(format_double(x)), x)

parse_double(c("1.5", "3e-1", NA_character_, "inf"))