Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-10800

The % operator returns unexpected results for negative numbers for BigIntegers

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 5.0.0-alpha-1
    • None

    Description

      As a follow-up to GROOVY-10786, the behavior of the % operator for BigInteger is a little strange for negative numbers.

      def nums = [-10, -10L, -10f, -10d, -10G, -10.0G]
      assert nums.collect{ it % 3 } == [-1, -1, -1f, -1d, 2G, -1.0G]
      

      In our documentation, we call "%" the remainder operator as does Java, though also like Java, the operator is often informally referred to as the "mod" operator.

      I suggest we change the BigInteger "%" operator to be the same as the other numeric types, i.e. use remainder not mod.
      This has some ramifications which I'll bring up on the mailing list.

      Attachments

        Issue Links

          Activity

            People

              paulk Paul King
              paulk Paul King
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: