ArithmeticException Non-terminating decimal expansion; no exact representable decimal result
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When working with computer programming and numerical computations, a common exception that often perplexes developers is the ArithmeticException: "Non-terminating decimal expansion; no exact representable decimal result". This error is prevalent in environments like Java, particularly when performing division operations with floating point or decimal data types.
Understanding the Exception
The essence of this exception revolves around the limitation of how floating-point numbers (like float and double in Java) or decimals are represented and stored in computer memory. Most computing systems use binary floating-point formats, which can only approximately represent many decimal fractions. This approximate representation stems from the fact that computers use a base-2 number system, while the human-readable numbers we use daily are in base-10.
A non-terminating decimal expansion in a digital computer occurs when the result of a division can't be exactly represented with a finite number of binary digits. For example, the fraction in decimal base is 0.3333..., extending infinitely. In a binary system, similarly, some fractions don't have an exact representation and can lead to an infinite series of digits after the decimal point, which is not practically storable in memory.
Example in Java
Here’s how this might typically manifest in Java:
The above code will throw an ArithmeticException because dividing 1 by 3 results in a non-terminating decimal that can’t be fully represented. To handle this, Java's BigDecimal class provides an overload of divide that allows specifying the scale (the number of digits to the right of the decimal point) and the rounding mode:
This code would not throw an exception; instead, it would give a result rounded to 10 decimal places.
How Computer Systems Cope
To cope with these limitations, programming languages and computer systems implement specific strategies:
1. Fixed Precision:
Systems limit the number of digits that can be stored following the decimal point. This is the typical method used by SQL databases and languages with fixed decimal types.
2. Rounding:
Most systems use some form of rounding to handle non-terminating decimals, such as rounding to the nearest representable number or truncating to the set precision.
3. Exceptions:
As seen in Java’s BigDecimal, directly throwing an exception when encountering a non-terminating decimal forces programmers to handle this case explicitly - either by catching the exception or by specifying the scale and rounding mode explicitly.
Practical Side and Implications
Handling such exceptions is crucial in financial applications where precision and accuracy of decimal calculations are vital. Mismanaging non-terminating expansions can lead to significant monetary discrepancies.
Summary Table
| Strategy | Description |
| Fixed Precision | Limits storage to a predefined number of decimal places. |
| Rounding | Approximates the result by rounding to the nearest possible value. |
| Exceptions | Forces explicit handling by throwing an error during runtime. |
Conclusion
The ArithmeticException for non-terminating decimal expansions underscores a significant challenge in computer arithmetic: representing decimal numbers accurately within the binary system fundamentally used by digital computers. This is a crucial aspect for developers, especially those working with financial software, numerical computations, or any application where precision is paramount. By understanding how to manage such exceptions, one can ensure more robust, accurate, and effective software solutions.

