Fraction
Joe Darcy
joe.darcy at oracle.com
Thu Jan 5 23:46:09 UTC 2012
Hello,
On 1/1/2012 8:54 AM, Tom Eugelink wrote:
>
> Hello guys,
>
> I was wondering if it was of any interest to OpenJFX to have a
> calculation class that does not round. I've got "Fraction" laying
> around, which does math using two BigIntegers, so it never even
> rounds. The API is roughly equivalent to BigDecimal, so you have
> methods like add, divide, etc.
>
> Any interest in adding such a class to Java?
>
The BigDecimal class can already preform exact computations. To get
exact behavior, use the version of add, subtract, etc. that takes a
single BigDecimal argument or the version that takes a BigDecimal
argument and a MathContext argument and pass a MathContext object with a
precision of 0. Of the four basic arithmetic operations, the most
interesting operation is divide since it can result in infinite inputs
(fractions that are non-terminating in decimal) from finite inputs while
the other operations cannot.
In terms of its representation, BigDecimal class uses a floating-point
style scaled representation so that very large or very small numbers
that are low-precision don't use a lot of storage.
When doing general computations, there is a need to round result
periodically to avoid unbounded growth in the sizes of the numbers being
operated on. BigDecimal supports various rounding operations, including
rounding to a given number of places after the decimal point and
rounding to a given number of total digits. These styles of rounding
are relatively easy to understand, but still quite vexing for numerical
analysis.
The rounding options I'm familiar with for rational packages are fixed
slash vs floating-slash, that is, given constraints on the sizes of the
numerator and denominator, return the nearest fraction to the exact
result. AFAIK, such system are less studied if not more difficult to
work with than traditional rounding.
In short, while there would be some use cases, without additional
justification I don't see the need to add a rational number package to
the JDK.
Cheers,
-Joe
More information about the core-libs-dev
mailing list