Repository navigation
Write short decimal floats without Ryu's digit-by-digit loop - #1239
Open
quinnj-bot[bot] wants to merge 1 commit into
Open
quinnj-bot[bot] wants to merge 1 commit into
quinnj-bot[bot] wants to merge 1 commit into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1239 +/- ##
==========================================
+ Coverage 96.05% 96.07% +0.01%
==========================================
Files 9 9
Lines 5885 5905 +20
==========================================
+ Hits 5653 5673 +20
Misses 232 232 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Ryu removes one decimal digit per loop step, so a value like 123.45 runs about a dozen steps before it reaches its five digits. Short decimals are common in real tables (prices, measurements, coordinates), and they wrote at a third of the speed of random floats. A decimal of at most 15 significant digits is the only such decimal that rounds to its Float64, so when one exists it is the shortest and Ryu returns it. _writeshortdecimal scales |x| to a 14- or 15-digit integer, rounds it, checks that the decimal rounds back to x, and drops trailing zeros. Other values, and integer-valued floats, use Ryu as before. Output bytes are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
quinnj-bot
Bot
force-pushed
the
claude/write-short-decimals
branch
from
November 19, 2026 20:49
5c9ab16 to
bfc958e
Compare
Contributor
Author
|
An independent review of bfc958e found the output correct. One question remains for the maintainer.
[comment generated via AI] |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Floats with a few decimals, such as prices, measurements, and coordinates, wrote at about a third of the speed of random floats. Ryu finds the shortest digits by removing one decimal digit per loop step, so 123.45 takes about a dozen steps before it reaches its five digits. Ryu's reduction alone costs 74 to 91 cycles for such values, against 39 for a random Float64.
What changed
_writeshortdecimalhandles non-integer values from 1e-9 to 1e15 whose shortest form has at most 15 significant digits. It scales|x|to a 14- or 15-digit integer, rounds it, checks that this decimal rounds back tox(one correctly rounded division by an exact power of ten), and drops trailing zeros._writereducetries it after the existing binary-fraction check. Integer-valued floats and every other value still use Ryu.Why the digits match Ryu
Two different decimals of at most 15 significant digits, both at least 10^E, differ by at least 10^(E-14). A Float64 below 10^(E+1) is closer than 2.3×10^(E-15) to every decimal that rounds to it, which is narrower than that gap. So at most one such decimal rounds to
x. When the check finds one, it is the shortest decimal that rounds tox, which is what Ryu returns. Midpoints between Float64 values need more than 15 digits, so no tie rule comes into play.Scope
floatformat, and the quoted-float path are untouched.Tradeoffs
This is not free for full-precision floats. A column of random Float64 values (17 significant digits) runs 5% more instructions. In the quieter final sweep it measured +2.2% cycles (range +0.8% to +3.7%, 5 rounds, load 10 to 22). In the busier per-PR run it measured -0.2% (range -6% to +2%, 7 rounds, load 24 to 79). On CI's Linux runners (Julia 1.13.4, 4 rounds, medians), the 60-column random-float table measured +2.4% on x86 and +6.4% on arm. Short decimals get 1.6x faster. Whether that trade is worth taking is a call for the maintainer.
Verification
_writereduceequalsBase.Ryu.reduce_shorteston 134 million values: random bit patterns, random and rounded decimals at many scales, every k×10^e for k below 10^6 and |e| ≤ 12 with both neighbors, and powers of ten from 1e-340 to 1e310 with neighbors. This was a local script and is not part of the PR.string(x).🤖 Generated with Claude Code