Recovering Rational Numbers from Floating Point: Continued Fractions and Quota Verification
A backend value such as 0.16327272727272726 is often just an unformatted double, but it can be mapped back to a simple rational number. This article explains why the recovery is usually unique for denominators up to about 10^7, shows a Python implementation based on continued fractions and the Stern-Brocot tree, and discusses how multiple observations can be used to infer an internal metering quota basis while avoiding common sampling pitfalls.
A Number That Should Not Have Appeared in a UI
Suppose a usage metric in a service contains this value:
0.16327272727272726
It has 17 decimal digits and a visibly repeating tail. A user-facing interface would normally round or format such a value, keeping only two or three decimal places. When a long precision value appears instead, a common cause is backend serialization that omits the formatting step and writes the internal double directly.
The number itself is not a secret. It is just a floating-point value, and floating-point values often correspond to a simple fraction. If that fraction can be recovered, it may be possible to infer the internal metering unit used by the system. This article uses that value as an example to explain the method and its limits. It does not identify any specific service implementation.
Why This Is Possible: Floating Point and Rational Numbers
An IEEE 754 double has about 15 to 17 significant decimal digits. Suppose p/q is a rational number with a small denominator. When it is converted to a double, the result is the nearest representable double.
If two different fractions have denominators no larger than Q, their difference is at least 1 / Q². When Q = 10^7, that minimum difference is 10^-14, which is much larger than the double precision spacing near 0.16—about 2.8e-17. Therefore, for denominators up to tens of millions, the recovery is usually unique.
Continued Fraction Expansion
Any real number can be expanded as a continued fraction:
x = a0 + 1/(a1 + 1/(a2 + 1/(a3 + ...)))
For rational numbers, the expansion terminates after finitely many steps. For example, 449/2750 has the continued fraction:
[0; 6, 8, 56]
Its truncated expansions produce the convergents:
1/68/49449/2750
The second convergent, 8/49 ≈ 0.1632653, differs from the observed value by about 7e-6, far larger than the floating-point error. It is not the answer. Only the final convergent, 449/2750, is close enough.
The difference between 449/2750 and 0.16327272727272726 is about 1.3e-17, smaller than one double-precision step near that value. So 449/2750 is a reasonable candidate.
Searching With the Stern-Brocot Tree
The Stern-Brocot tree is another way to search for the same fraction. It starts from 0/1 and 1/0. At each step, it takes the mediant of the current interval endpoints:
mediant = (a + c) / (b + d)
where a/b and c/d are the endpoints. Every positive rational appears exactly once in the tree and is in lowest terms. The path from the root to a given fraction corresponds to its continued fraction coefficients.
A runnable implementation is:
import mathfrom fractions import Fractiondef find_rational(x, max_den=10**7, tol=1e-15, max_steps=10**6):"""Find a fraction with denominator <= max_den that is close to x."""whole = math.floor(x)y = x - wholeif y == 0:return Fraction(whole)ln, ld, rn, rd = 0, 1, 1, 0 # left/right bounds: 0/1 and 1/0for _ in range(max_steps):mn, md = ln + rn, ld + rd # mediant numerator/denominatorif md > max_den:return Nonem = mn / mdif abs(m - y) <= tol:return Fraction(whole * md + mn, md)if m < y:ln, ld = mn, mdelse:rn, rd = mn, mdreturn Noneprint(find_rational(0.16327272727272726)) # 449/2750
Two parameter choices are important:
tolmust not be too small. Near0.16, the double spacing is about2.8e-17, and a tolerance around1e-15covers the rounding error.tolmust not be too large. Withmax_den = 10^7, the minimum distance between candidate fractions is10^-14. A tolerance of1e-15gives a search window of2e-15, so usually only one candidate passes. Iftolis1e-12, the window becomes2e-12, many fractions can pass, and the result is no longer unique.
From One Fraction to a Quota Model
A Single Fraction Is Not Enough
The fraction 449/2750 can also be written as 8.98/55, because 2750 = 50 × 55. Here 55 is only one factor of the denominator. From one observation alone, it is impossible to determine whether the system uses 55 as its base.
The real evidence comes from multiple observations. Suppose the same metering rule produces several (used, total) pairs. If each pair can be reconstructed as an unreduced u/T form, and T stays the same, then T is a candidate base.
If only reduced fractions are available, common factors have already been lost. In that case, take the least common multiple of the denominators as a candidate base L. Then check whether each observed value multiplied by L is close to an integer. If many observations satisfy that condition, L is more likely to be the internal base.
Comparing Different Tiers
To compare actual quotas across tiers, first normalize the comparison: use the same time window, the same request class, and the same metering unit. Then, for each tier, compute the ratio of consumed units to the allowance, and record the sample count and dispersion.
Sampling should cover the full range from near zero to near the cap, and should include samples both before and after window resets. This reduces bias from caching and asynchronous statistics. Without enough samples, the conclusion remains a hypothesis rather than a fact.
Common Sampling Pitfalls
- Display-layer recomputation: A frontend may recalculate values from backend data. The result is no longer the original value. Try to obtain the raw backend response.
- Asynchronous statistics delay: Usage is often written asynchronously. Sampling immediately after a request can produce values that are too small.
- Rate limiting and retries: Throttled requests may still be counted, which distorts the ratio.
- Authorization and terms: Before collecting network data, confirm that the data belongs to your own account and review the applicable terms. Do not collect other people’s data, bypass rate limits, or circumvent billing mechanisms. If there is compliance risk, consult legal advice first.
Limits and Checklist
The method applies to rational numbers with denominators up to about 10^7. With larger denominators, different fractions may round to the same floating-point value, so the recovery is no longer unique.
The recovered fraction is the most likely candidate, not a proven answer. It should be validated with a controlled behavioral experiment under an authorized scope, for example by moving usage to a known boundary and observing the returned value.
When reusing this method, check the following:
- Data source: Is it a raw backend response, or a value recomputed by a frontend?
- Display precision: Was the value rounded, and to how many decimal places?
- Tolerance
tol: Is it close to double-precision error, but not so large that multiple candidates pass? - Denominator limit
max_den: Does it satisfy the uniqueness condition? - Observation count: Does the sample cover enough range and time windows?
- Cross-validation: Do different observations identify the same base?
- Behavioral experiment: Was verification completed within authorized scope?
Conclusion
An unrounded floating-point value can reveal the rational number behind it. That recovery depends on clear mathematical methods and careful data handling. For service providers, user-facing values should pass through display-layer formatting, and internal metering units should not be emitted at raw precision. For users, understanding the relationship between floating-point values and rational numbers helps judge data reliability and verify their own usage in a compliant way.
