Method
Search the legal pairs.
Compare the exact errors.
Ratiofit minimizes the absolute difference between the chosen fraction and your entered finite decimal. It keeps the two integer caps separate and uses no floating-point rounding to choose a result.
The objective
Write the target as r and the caps as P and Q. A legal pair has 1 ≤ p ≤ P and 1 ≤ q ≤ Q. The quantity being minimized is |p/q − r|. A ratio above the target can beat one below it, or the other way around; only the absolute difference matters until there is an exact tie.
absolute error = |p/q − r|
legal terms: 1 ≤ p ≤ P, 1 ≤ q ≤ Q
This is not relative percentage error, closeness after rounding, a fixed p+q total or a physical tolerance test. Changing the error metric would be a different question.
Why two candidates per denominator are enough
Fix one positive denominator q. Minimizing |p/q − r| is equivalent to minimizing |p − rq|, because division by the positive q preserves comparisons. On the integer number line, a closest p can only be floor(rq) or floor(rq)+1. If either lies outside the allowed interval 1 through P, move it to the nearest endpoint of that interval.
The calculator considers those clamped candidates for every q from 1 through Q, removing a duplicate when both candidates reach the same endpoint. Every legal pair has one of those denominators, and every denominator's best numerator is covered, so the retained minimum is a global minimum over the finite allowed set. This is our direct finite-search argument, not a claim that a denominator-only library function supplies the numerator cap.
Exact input and exact comparisons
The script parses your decimal as A/S, with S = 10¹² and A a positive integer. Shorter decimals are padded, not rounded. For a candidate p/q, its signed error is (pS − Aq)/(Sq). If E = |pS − Aq|, two absolute errors can be compared by E₁q₂ and E₂q₁ because the common positive S cancels.
The implementation uses integer arithmetic for parsing, candidate selection and ties. It reduces the selected fraction for display; equivalent pairs can always reduce within the original caps. At most two candidate checks are made per denominator, so Q = 10000 means at most 20000 checks. It retains one current best candidate rather than building a P-by-Q grid. Runtime still depends on your browser and device.
How ties are resolved
When absolute errors are exactly equal, choose the lower rational value. If values are equal, choose the smaller denominator, then the smaller numerator. This deterministic product rule is visible in the calculator and exports. It is not necessarily the tie rule used by a different approximation API.
For example, at r = 0.75 with caps P = 1, Q = 2, the two allowed fractions 1/2 and 1 are equally far away. This tool chooses 1/2 even though its denominator is larger. See the nearby-decimal example to show how a tiny real change can resolve that tie.
The attainable interval
The smallest allowed ratio is 1/Q and the largest is P/1. A positive target outside that interval is still accepted within the input bounds. A target below the interval selects its nearest lower boundary ratio, 1/Q; a target above it selects P/1. The target itself is never silently clamped. The result explains when the nearest achievable ratio is a boundary.
Input and interpretation limits
- Target: greater than 0, no greater than 1000000, with up to twelve fractional digits. Enter 0.5 rather than .5. A decimal point must have digits on both sides.
- Each cap: an integer from 1 to 10000. Leading zeros and outside whitespace are accepted; signs, exponents, separators, units, colon ratios and fraction expressions are rejected.
- Inputs use at most 64 characters. Blank, malformed or out-of-range values are rejected without silent clamping. Positive terms only: 0/q and negative ratios are not candidates.
- An approximate decimal of an irrational number is treated as that entered decimal, never as the symbolic constant. The tool has no irrational-symbol parser.
- No dimensions, fabrication, gear suitability, strength, investment or financial calculation is performed. A small numerical error is not evidence that a physical design will work.
Display rounding and exact-match status
The achieved decimal is rounded to twelve places. The signed and absolute error decimals use eighteen places and are accompanied by exact fractions. Under the allowed bounds a nonzero error has magnitude at least 1/(10¹² × 10000), so this error display cannot turn a permitted nonzero error into zero. Exact-match status is based only on a zero exact numerator.
Two values can look equal when rounded in one display yet still have a nonzero exact difference. The default example deliberately demonstrates that distinction.
Calculate, edit and export
Press Calculate ratio to run the search after editing. An edit immediately clears stale results and disables exports. Reset example restores the three default values and recalculates. Copy and optional text/JSON downloads contain only the current valid model, including the tie rule and exact error. JSON integer and fraction quantities use strings to preserve exact values.
Primary reference context
The official Python fractions documentation explains rational arithmetic and nearest approximation with a denominator limit. It also distinguishes constructing a fraction from a decimal string from constructing one from a binary float. The official CPython fractions implementation supplies context for its own denominator-limited method.
Ratiofit uses the separate finite search and explicit tie policy described here. Its independent numerator cap, bounds, examples and interface are product choices. Return to the calculator or explore the worked examples.