[ 
https://issues.apache.org/jira/browse/NUMBERS-150?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17184622#comment-17184622
 ] 

Alex Herbert commented on NUMBERS-150:
--------------------------------------

Fraction.pow(int) requires negation of the input exponent when it is negative. 
This is not possible for Integer.MIN_VALUE since the negation has no effect.
{code:java}
assert Integer.MIN_VALUE == -Integer.MIN_VALUE;
{code}

The method should detect Integer.MIN_VALUE and compute the power correctly.

This will only effect a fraction with an absolute value of 1. All other 
non-zero fractions should generate an ArithmeticException due to overflow:

{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));

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}



> Fraction.pow to correctly handle Integer.MIN_VALUE as the argument
> ------------------------------------------------------------------
>
>                 Key: NUMBERS-150
>                 URL: https://issues.apache.org/jira/browse/NUMBERS-150
>             Project: Commons Numbers
>          Issue Type: Bug
>          Components: fraction
>            Reporter: Jin Xu
>            Priority: Major
>          Time Spent: 10m
>  Remaining Estimate: 0h
>
> a bug was discovered by [NUMBERS-149].
> here is the fix pr.
> https://github.com/apache/commons-numbers/pull/83



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to