ryu

    Pure MoonBit port of Ryu shortest float formatting, Ryu Printf, and experimental decimal parsing

    ryu
    float
    double
    formatting
    parsing
    Download zip
    Author
    Version
    0.1.0
    License
    MIT
    Last updated
    49 minutes ago
    Downloads
    1

    No dependencies

    #Checked Ryu examples

    ///|
    test "shortest formatting preserves source precision" {
    assert_eq(@ryu.d2s(0.3), "3E-1")
    assert_eq(@ryu.f2s(Float::from_double(0.3)), "3E-1")
    assert_eq(
    @ryu.s2d(@ryu.d2s(-0.0)).reinterpret_as_uint64(),
    0x8000000000000000UL,
    )
    }

    ///|
    test "Printf rounding and scientific exponents" {
    assert_eq(@ryu.d2fixed(0.1, precision=20), "0.10000000000000000555")
    assert_eq(@ryu.d2exp(9.99, precision=1), "1.0e+01")
    }

    FormatError

    pub(all) suberror FormatError {
    InvalidPrecision(Int)
    } derive(Eq,
    Debug
    )

    Reports precision values that cannot fit the portable output length contract.

    ParseError

    pub(all) suberror ParseError {
    InputTooShort
    InputTooLong
    MalformedInput
    } derive(Eq,
    Debug
    )

    Preserves the failure statuses of upstream's experimental bounded parser.

    d2exp

    fn d2exp(value : Double, precision~ : Int) -> String raise FormatError

    Formats a binary64 value in scientific notation with an exact fractional width.

    This ports Ryu Printf's %e operation: one digit precedes the decimal point, precision digits follow it, and lowercase e precedes an explicit sign and an exponent padded to at least two digits. Precision zero omits the point. Rounding uses the exact binary value with ties to even and can carry into the exponent. Negative zero, non-finite values, precision validation, input ownership, allocation, and backend guarantees are the same as d2fixed.

    test {
    assert_eq(@ryu.d2exp(9.99, precision=1), "1.0e+01")
    assert_eq(@ryu.d2exp(-0.0, precision=2), "-0.00e+00")
    }

    d2fixed

    fn d2fixed(value : Double, precision~ : Int) -> String raise FormatError

    Formats a binary64 value with exactly precision digits after the decimal point.

    This ports Ryu Printf's %f operation, rounding the exact binary value to nearest with ties to even. Precision zero omits the decimal point. The sign of negative zero and values rounded to zero is preserved. Infinities use Infinity/-Infinity; all NaNs use nan on every backend, following the upstream non-MSVC spelling. Input is unchanged and the result is immutable.

    precision must be in 0..2147483336; otherwise this raises InvalidPrecision before allocation, including for special values. Output storage grows with the requested precision and integer length. Float inputs may be widened exactly to Double for this operation. All supported backends use pure MoonBit.

    test {
    assert_eq(@ryu.d2fixed(2.5, precision=0), "2")
    assert_eq(@ryu.d2fixed(0.1, precision=20), "0.10000000000000000555")
    }

    d2s

    fn d2s(value : Double) -> String

    Converts an IEEE 754 binary64 value to Ryu's shortest round-trip-safe decimal.

    The returned immutable ASCII string uses uppercase E, one digit before the optional decimal point, and an unpadded exponent without a positive sign. Rounding is to nearest with ties to even in the original binary64 precision. Negative zero is -0E0, infinities are Infinity/-Infinity, and every NaN is NaN, regardless of sign or payload. The input is unchanged. This pure MoonBit implementation has identical output on all four supported backends.

    test {
    assert_eq(@ryu.d2s(0.3), "3E-1")
    assert_eq(@ryu.d2s(-0.0), "-0E0")
    }

    f2s

    fn f2s(value : Float) -> String

    Converts an IEEE 754 binary32 value to its shortest round-trip-safe decimal.

    This uses the input's original Float precision; widening to Double before calling d2s can produce different digits. Formatting, ties-to-even rounding, negative zero, infinities, and NaN spellings follow d2s. The immutable ASCII result and input are independent, and output is the same on every backend.

    test {
    assert_eq(@ryu.f2s(Float::from_double(0.3)), "3E-1")
    }

    s2d

    fn s2d(input : StringView) -> Double raise ParseError

    Parses an entire ASCII decimal view using upstream's experimental binary64 parser.

    Up to 17 significant mantissa digits and four significant exponent digits are accepted; initial zeroes do not consume either budget, while all digits after the first nonzero do. ASCII -, one decimal point, and e/E with an optional exponent sign are supported. Initial +, whitespace, non-ASCII characters, NUL, Infinity, and NaN spellings are rejected. Empty input raises InputTooShort; excess digits raise InputTooLong; other rejected syntax raises MalformedInput, in upstream scan order.

    For compatibility this accepts missing mantissa/exponent digits: -, ., and e parse as signed/unsigned zero, and 1e+ parses as one. This is not a general language or JSON number validator. Numeric rounding is to nearest, ties to even; underflow preserves signed zero and overflow yields infinity. Only the actual view is read, with no mutation, and all backends agree.

    test {
    assert_eq(@ryu.s2d("1.25e2"), 125.0)
    assert_eq(@ryu.s2d("-0").reinterpret_as_uint64(), 0x8000000000000000UL)
    }

    s2f

    fn s2f(input : StringView) -> Float raise ParseError

    Parses an entire ASCII decimal view directly to a binary32 Float.

    The mantissa budget is nine significant digits; the exponent budget is four. Grammar, scan-order errors, accepted missing-digit forms, rounding, signed underflow, infinity on overflow, input ownership, and view bounds follow s2d. Direct binary32 rounding avoids a Double-to-Float double-rounding step.

    test {
    assert_eq(@ryu.s2f("0.3").reinterpret_as_uint(), 0x3E99999AU)
    }