[
https://issues.apache.org/jira/browse/NUMBERS-207?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17935783#comment-17935783
]
Alex Herbert commented on NUMBERS-207:
--------------------------------------
The Lang3 fraction maintains the sign in the numerator. The Numbers version
uses the sign of both the numerator and denominator components to determine the
sign of the fraction. This allows supporting +2^31 using -2^31 / -1. So the
Numbers version can have all representations as positive or negative.
The Lang3 version does not have a signum method. But if you look at the code
for negate you see that Lang3 will error when trying to negate -2^31 / 1. The
Numbers version will return -2^31 / -1.
In all the arithmetic operations this extra support does not impact performance
and it changes the class to reduce runtime errors when using extreme
representations.
Note the codebases have diverged as Fraction moved to Commons Math and then to
Numbers. The Numbers version is more recently modified and IIRC the Numbers
version was tested using all the tests from Lang3 and Math and extra tests
added. Unfortunately the test suite was not comprehensive enough. Thanks for
the bug report.
Q. Are you using compareTo to sort large numbers of fractions? I would be
interested to know if this extra complexity is impacting your runtimes.
> Fraction and BigFraction compareTo incorrect for negative values
> ----------------------------------------------------------------
>
> Key: NUMBERS-207
> URL: https://issues.apache.org/jira/browse/NUMBERS-207
> Project: Commons Numbers
> Issue Type: Bug
> Components: fraction
> Affects Versions: 1.2
> Reporter: Richard Mullender
> Priority: Major
>
> Both `Fraction` and `BigFraction` display incorrect `compareTo` behaviour
> when comparing negative fractions against one another.
> See the below test class:
> {code:java}
> public class FractionTest {
> @Test
> public void testFraction() {
> var fraction1 = org.apache.commons.numbers.fraction.Fraction.of(-10,
> 1);
> var fraction2 = org.apache.commons.numbers.fraction.Fraction.of(-5, 1);
>
> assert fraction1.compareTo(fraction2) < 0;
> }
>
> @Test
> public void testBigFraction() {
> var fraction1 =
> org.apache.commons.numbers.fraction.BigFraction.of(-10, 1);
> var fraction2 = org.apache.commons.numbers.fraction.BigFraction.of(-5,
> 1);
>
> assert fraction1.compareTo(fraction2) < 0;
> }
>
> @Test
> public void testApacheLang3() {
> var fraction1 =
> org.apache.commons.lang3.math.Fraction.getFraction(-10, 1);
> var fraction2 = org.apache.commons.lang3.math.Fraction.getFraction(-5,
> 1);
>
> assert fraction1.compareTo(fraction2) < 0;
> }
> }
> {code}
>
> Out of these three tests, only Lang3's Fraction passes. The other two tests
> (fraction.Fraction and fraction.BigFraction) fail.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)