You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Christian Semrau (JIRA)" <ji...@apache.org> on 2010/12/07 00:06:14 UTC

[jira] Commented: (LANG-662) org.apache.commons.lang3.math.Fraction does not reduce (Integer.MIN_VALUE, 2^k)

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

Christian Semrau commented on LANG-662:
---------------------------------------

The current implementation:

{code:title=Fraction.java|borderStyle=solid}
    private static int greatestCommonDivisor(int u, int v) {
        //if either op. is abs 0 or 1, return 1:
        if (Math.abs(u) <= 1 || Math.abs(v) <= 1) {
            return 1;
        }
{code} 

The case u==0 || v==0 is already handled at the calling site (). So you might switch to testing for abs==1 only:

{code:title=Fraction.java|borderStyle=solid}
    private static int greatestCommonDivisor(int u, int v) {
    	// the case u==0 || v==0 is handled by the caller.
        // if either op. is abs 1, return 1:
        if (Math.abs(u) == 1 || Math.abs(v) == 1) {
            return 1;
        }
{code} 

Dropping this test altogether does not change correctness, but might influence performance. If you expect many fractions with numerator 1 or denominator 1, keep the test.


> org.apache.commons.lang3.math.Fraction does not reduce (Integer.MIN_VALUE, 2^k)
> -------------------------------------------------------------------------------
>
>                 Key: LANG-662
>                 URL: https://issues.apache.org/jira/browse/LANG-662
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.math.*
>    Affects Versions: 3.0
>            Reporter: Christian Semrau
>            Priority: Minor
>
> The greatestCommonDivisor method in class Fraction does not find the gcd of Integer.MIN_VALUE and 2^k, and this case can be triggered by taking Integer.MIN_VALUE as the numerator. Note that the case of taking Integer.MIN_VALUE as the denominator is handled explicitly in the getReducedFraction factory method.
> {code:title=FractionTest.java|borderStyle=solid}
> 	// additional test cases
> 	public void testReducedFactory_int_int() {
> 		// ...
> 		f = Fraction.getReducedFraction(Integer.MIN_VALUE, 2);
> 		assertEquals(Integer.MIN_VALUE / 2, f.getNumerator());
> 		assertEquals(1, f.getDenominator());
> 	public void testReduce() {
> 		// ...
> 		f = Fraction.getFraction(Integer.MIN_VALUE, 2);
> 		result = f.reduce();
> 		assertEquals(Integer.MIN_VALUE / 2, result.getNumerator());
> 		assertEquals(1, result.getDenominator());
> {code} 

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.