Horner's recursive algorithm for fractional part - Java
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Horner's method is usually introduced for evaluating polynomials, but the same nested structure is also useful for evaluating the fractional digits of a number. In Java, a recursive Horner-style algorithm gives a clean way to convert a fractional digit sequence such as "375" in base 10 into the value 0.375.
The Fractional Horner Idea
Suppose the digits after the decimal point are d1 d2 ... dn in base b. Their value is:
Horner's method rewrites that so you do not keep recomputing powers:
A cleaner recursive form from right to left is:
with the last digit evaluated as:
That turns a sum of fractional powers into a repeated divide-and-add pattern.
Recursive Java Implementation
Here is a simple implementation for a decimal digit string:
For "375" in base 10, the recursion evaluates:
- '
5 / 10 = 0.5' - '
(7 + 0.5) / 10 = 0.75' - '
(3 + 0.75) / 10 = 0.375'
That is Horner's structure applied to the fractional part.
Why This Is Better Than Recomputing Powers
A naive implementation would calculate:
That is fine for a few digits, but Horner's method has two advantages:
- it performs fewer arithmetic operations
- it avoids repeated exponentiation logic
The algorithm is linear in the number of digits and easy to read once the recurrence is understood.
Iterative Version for Comparison
Recursion is elegant, but an iterative version is often easier to debug and avoids call-stack growth.
Both versions implement the same Horner-style evaluation. The iterative one is often the better production choice, while the recursive one is excellent for explaining the mathematics.
Supporting Other Bases
Because the code uses Character.digit, it can handle bases beyond 10 as well:
This evaluates the hexadecimal fraction .A8, which equals:
That makes the approach useful for parsers, numeric-conversion utilities, and educational tools.
Precision Considerations
Using double is fine for many applications, but repeated fractional operations can accumulate floating-point error. If exact decimal behavior matters, use BigDecimal and divide with an explicit scale and rounding mode.
The core Horner structure still applies. Only the numeric type changes.
Common Pitfalls
The biggest pitfall is getting the recursion direction wrong. For the fractional part, the clean recursive formulation works from the last digit back toward the first.
Another pitfall is forgetting base validation. A digit such as 'A' is valid in base 16 but not in base 10.
A third pitfall is assuming double gives exact decimal answers for arbitrary long fractional strings. Floating-point precision has limits.
Finally, do not confuse this with extracting the fractional part of an already parsed floating-point number. This algorithm evaluates a fractional digit sequence directly.
Summary
- Horner's method can evaluate fractional digit sequences efficiently, not just ordinary polynomials
- A recursive relation such as
(digit + nextValue) / basegives a clean implementation - The Java version works naturally with
Character.digitand can support different bases - An iterative version is often better for production, while recursion is great for clarity
- If exact numeric behavior matters, consider
BigDecimalinstead ofdouble
Related reading
- Hot content algorithm / score with time decay
- How a distributed storage system like Raft filter duplicate requests even after client session expiration
- How a marker-based augmented reality algorithm like ARToolkit's one works?
- How and when to create a suffix link in suffix tree?
- Hot swapping in Spring Boot
- How a thread should close itself in Java?
- How are distributed election algorithms implemented in practice (Bully, Ring algorithm)?
- How are nonblocking data structures possible?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.