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

Makarand Hinge commented on LANG-1834:
--------------------------------------

[~ggregory] 

I was able to reproduce this issue with 
{{{}Fraction.getReducedFraction(Integer.MIN_VALUE, -2){}}}, which currently 
throws {{{}ArithmeticException: overflow: can't negate{}}}.

I plan to work on a fix for this. The approach will be to handle the reducible 
{{Integer.MIN_VALUE}} numerator case before sign normalization, so that the 
fraction can be reduced safely before attempting to negate the numerator. I’ll 
also preserve the existing overflow checks for cases where 
{{Integer.MIN_VALUE}} genuinely cannot be represented after negation.

I’ll add a regression test for this case and verify the existing test suite to 
make sure the change does not affect other {{Fraction}} behavior.

> raction.getReducedFraction incorrectly throws for reducible Integer.MIN_VALUE 
> numerator
> ---------------------------------------------------------------------------------------
>
>                 Key: LANG-1834
>                 URL: https://issues.apache.org/jira/browse/LANG-1834
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.*, lang.math.*
>    Affects Versions: 3.20.0
>         Environment: * Java: 17
>  * Apache Commons Lang
>  * Test framework: JUnit 5
>            Reporter: 尹茂椿萱
>            Priority: Major
>
> {{Fraction.getReducedFraction(Integer.MIN_VALUE, -2)}} throws an 
> {{{}ArithmeticException{}}}:
>  
> {{ArithmeticException: overflow: can't negate}}
> However, the fraction can be safely reduced to {{{}1073741824/1{}}}, since 
> both values are representable as {{{}int{}}}.
> The implementation already contains a historical special case for 
> {{Integer.MIN_VALUE}} as the denominator:
>  
> {{if (denominator == Integer.MIN_VALUE && (numerator & 1) == 0) \{
>     numerator /= 2;
>     denominator /= 2;
> }}}
> This modification handles some {{Integer.MIN_VALUE}} cases, but the handling 
> is incomplete. It does not cover the case where {{Integer.MIN_VALUE}} is the 
> numerator and the fraction can be reduced before sign normalization.
> For example:
>  
> {{Fraction.getReducedFraction(Integer.MIN_VALUE, -2);}}
> should return a fraction with numerator {{1073741824}} and denominator 
> {{{}1{}}}, but currently throws an exception.
> *Suggested fix:* reduce the fraction before negating 
> {{{}Integer.MIN_VALUE{}}}, or otherwise handle reducible 
> {{Integer.MIN_VALUE}} numerators before sign normalization.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to