You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "尹茂椿萱 (Jira)" <ji...@apache.org> on 2026/09/19 00:20:00 UTC
[jira] [Closed] (LANG-1834) raction.getReducedFraction incorrectly throws for reducible Integer.MIN_VALUE numerator
[ https://issues.apache.org/jira/browse/LANG-1834?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
尹茂椿萱 closed LANG-1834.
----------------------
> 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
> Fix For: 3.21.0
>
>
> {{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)