尹茂椿萱 created LANG-1834:
--------------------------

             Summary: 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: 尹茂椿萱


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