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

Arithmetic with negative literals doesn't work as expected

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Won't Fix
    • 1.5.1
    • 1.6-beta-1
    • None
    • None
    • WinXP Pro, Java 6 Update 3, Groovy 1.5.1

    Description

      CODE
      ---------
      def result = (-3).plus(2)
      println 'Expected: -1'
      println "Actual: ${result}"
      println ''

      assert ( (-3).plus(2) == -1 )
      println 'Passed proper check'
      println ''

      result = -3.plus(2)
      println 'Expected: -1'
      println "Actual: ${result}"
      println ''

      assert ( -3.plus(2) == -5 )
      println 'Passed improper check'
      println ''

      assert ( -3.plus(2) == -1 )
      println 'Passed 2nd proper check'

      RESULTS
      --------------
      Expected: -1
      Actual: -1

      Passed proper check

      Expected: -1
      Actual: -5

      Passed improper check

      Caught: java.lang.AssertionError: Expression: (3.plus(2) == -1)
      at math.run(math.groovy:19)
      at math.main(math.groovy)

      =========================

      Groovy's parser detects that a number is negative, even if it is not surrounded by parentheses. However, it does not accurate perform the mathematical calculation necessary. Also, the AssertionError message drops the negative sign when it reports a failed assertion if the literal is not surrounded by parentheses.

      Attachments

        1. math.groovy
          0.5 kB
          John Loizeaux

        Activity

          People

            blackdrag Jochen Theodorou
            loizeaux John Loizeaux
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: