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

class field confused / corrupted

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Blocker
    • Resolution: Fixed
    • 1.0-JSR-5
    • None
    • None
    • None
    • Mustang b82

    Description

      EXPECTED:

      Main - <cn type="rational">2<sep/>1</cn>
      Double - <cn type="rational">2<sep/>1</cn>
      Result - <cn type="rational">2<sep/>1</cn>

      ACTUAL:

      Main - <cn type="rational">2<sep/>1</cn>
      Double - <cn type="rational">null<sep/>null</cn>
      Result - <cn type="rational">null<sep/>null</cn>

      ------------------------

      import java.math.BigInteger
      import java.math.BigDecimal

      class Rational {
      final BigInteger numerator
      final BigInteger denominator

      Rational(BigInteger numerator, BigInteger denominator) {
      def gcd = numerator.gcd(denominator)
      if (gcd > 0G)

      { numerator = numerator.divide(gcd) denominator = denominator.divide(gcd) }

      this.numerator = numerator
      this.denominator = denominator
      println "Main - " + this
      }

      Rational(double number)

      { this(BigDecimal.valueOf(number)) println "Double - " + this }

      Rational(BigDecimal number)

      { this(number.unscaledValue(), BigInteger.TEN.pow(number.scale())) }

      String toString()

      { '<cn type="rational">' + numerator + '<sep/>' + denominator + '</cn>' }

      }

      println "Result - " + new Rational(2)

      Attachments

        Issue Links

          Activity

            People

              blackdrag Jochen Theodorou
              alexlamsl Alex Lam S.L.
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: