[
https://issues.apache.org/jira/browse/LANG-1601?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17183974#comment-17183974
]
Alex Herbert commented on LANG-1601:
------------------------------------
A further thought on the use of MIN_VALUE. Is there a case where any numerator
/ denominator combination can be raised to 2^31 without intermediate overflow?
I think this is perhaps only combinations of 1 / 1:
{noformat}
1 / 1
-1 / 1
1 / -1
-1 / -1
{noformat}
Any other number besides zero would overflow. Zero is already handled.
So perhaps the first step is to test if this works as expected:
{code:java}
Assertions.assertEquals(Fraction.of(1, 1), Fraction.of( 1,
1).pow(Integer.MIN_VALUE));
Assertions.assertEquals(Fraction.of(1, 1), Fraction.of(-1,
1).pow(Integer.MIN_VALUE));
Assertions.assertEquals(Fraction.of(1, 1), Fraction.of( 1,
-1).pow(Integer.MIN_VALUE));
Assertions.assertEquals(Fraction.of(1, 1), Fraction.of(-1,
1).pow(Integer.MIN_VALUE));
{code}
I just tried this and an exception is raised in {{ArithmeticUtils.pow(int,
int)}} for using a negative exponent.
So this is a bug but just because ArithmeticUtils raises an exception. In this
case the exception is an IllegalArgumentException and not the correct
ArithmeticException for intermediate overflow that would be raised using this
fix in pow:
{code:java}
if (exponent == Integer.MIN_VALUE) {
// MIN_VALUE can't be negated
final int temp = exponent / 2;
return new Fraction(ArithmeticUtils.pow(denominator, -temp),
ArithmeticUtils.pow(numerator,
-temp)).pow(2);
}
{code}
This fix also passes the following tests:
{code:java}
Assertions.assertThrows(ArithmeticException.class, () -> Fraction.of(1,
2).pow(Integer.MIN_VALUE));
Assertions.assertThrows(ArithmeticException.class, () -> Fraction.of(2,
1).pow(Integer.MIN_VALUE));
Assertions.assertThrows(ArithmeticException.class,
() -> Fraction.of(Integer.MAX_VALUE - 1,
Integer.MAX_VALUE).pow(Integer.MIN_VALUE));
{code}
> refine performance of Fraction.pow
> ----------------------------------
>
> Key: LANG-1601
> URL: https://issues.apache.org/jira/browse/LANG-1601
> Project: Commons Lang
> Issue Type: Improvement
> Components: lang.math.*
> Reporter: Jin Xu
> Priority: Minor
> Time Spent: 1h 10m
> Remaining Estimate: 0h
>
> https://github.com/apache/commons-lang/pull/611
--
This message was sent by Atlassian Jira
(v8.3.4#803005)