Horner's method
recursion
fractional part
Java programming
algorithm design

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.

Practice algorithms

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:

text
d1 / b + d2 / b^2 + ... + dn / b^n

Horner's method rewrites that so you do not keep recomputing powers:

text
(d1 + (d2 + (d3 + ... ) / b) / b) / b

A cleaner recursive form from right to left is:

text
value(i) = (digit(i) + value(i + 1)) / base

with the last digit evaluated as:

text
digit(last) / base

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:

java
1public class FractionalHorner {
2
3    public static double fractionalValue(String digits, int base) {
4        if (digits == null || digits.isEmpty()) {
5            return 0.0;
6        }
7        return fractionalValue(digits, base, 0);
8    }
9
10    private static double fractionalValue(String digits, int base, int index) {
11        int digit = Character.digit(digits.charAt(index), base);
12        if (digit < 0) {
13            throw new IllegalArgumentException("Invalid digit for base " + base);
14        }
15
16        if (index == digits.length() - 1) {
17            return (double) digit / base;
18        }
19
20        return (digit + fractionalValue(digits, base, index + 1)) / base;
21    }
22
23    public static void main(String[] args) {
24        System.out.println(fractionalValue("375", 10)); // 0.375
25        System.out.println(fractionalValue("101", 2));  // 0.625
26    }
27}

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:

text
3 / 10 + 7 / 100 + 5 / 1000

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.

java
1public static double fractionalValueIterative(String digits, int base) {
2    double result = 0.0;
3
4    for (int i = digits.length() - 1; i >= 0; i--) {
5        int digit = Character.digit(digits.charAt(i), base);
6        if (digit < 0) {
7            throw new IllegalArgumentException("Invalid digit for base " + base);
8        }
9        result = (digit + result) / base;
10    }
11
12    return result;
13}

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:

java
System.out.println(fractionalValue("A8", 16));

This evaluates the hexadecimal fraction .A8, which equals:

text
10 / 16 + 8 / 256 = 0.65625

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) / base gives a clean implementation
  • The Java version works naturally with Character.digit and can support different bases
  • An iterative version is often better for production, while recursion is great for clarity
  • If exact numeric behavior matters, consider BigDecimal instead of double

Related reading
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.