Uploaded image for project: 'Commons Numbers'
  1. Commons Numbers
  2. NUMBERS-132

ArithmeticUtils.gcd(int, int) can be simplified by performing the gcd algorithm on negative numbers

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 1.0
    • 1.0-beta1
    • core
    • None

    Description

      The method ArithmeticUtils.gcd(int, int) currently handles the special case of the non-negatable Integer.MIN_VALUE by converting the arguments to longs if one of them is Integer.MIN_VALUE and performing two iterations of the regular euclidean algorithm before handing the resulting values over to a helper method that performs the binary gcd algorithm.

      However, the tactic used by gcd(long, long) is much more elegant: It just converts positive arguments to their negative counterparts, thereby avoiding the risk of overflow completely without having to make exceptions for special cases and resorting to other data types.

      The method gcd(int, int) would likely be much more compact if it also were to apply this technique.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              Schamschi Heinrich Bohne
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 0.5h
                  0.5h