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

Numeric literals: default BigDecimal for decimals, support suffixes

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.0-beta-4
    • 1.0-beta-5
    • lexer, parser
    • None
    • All

    Description

      This issue is from the recent groovy-dev list discussion concering the use of BigDecimal as the default numeric type for literals with a decimal point. Also, numeric literal suffixes should be supported to specify the type.

      In the discussion, James Strachan said:
      -----------------------------------------------------------------

      • BigDecimal is the default type for any floating point number
        literals. For non-decimals we can choose the best size (Integer, Long,
        BigInteger based on the length of the number)
      • folks can use B (big decimal) postfix to be explict or use D (double)
        or F (float) for more explicit control.
      • to call typesafe methods of lower type (double, float) then you have
        to either use the doubleValue() or floatValue() methods or cast.

      foo(float p)

      { ... }

      x = 1.234
      foo( (float) x )
      foo( x.floatValue() )

      unless

      x = 1.234F
      foo
      ------------------------------------------------------------------

      Concerning:
      >For non-decimals we can choose the best size (Integer, Long,
      >BigInteger based on the length of the number)

      Should we automatically choose the size or use suffixes (or both)?
      B BigInteger (also means BigDecimal for literals with a dot)
      I Integer
      L Long

      Attachments

        1. GroovyDecimalLiteral.patch
          44 kB
          Brian Larson
        2. GroovyDecimalLiteral_2nd.patch
          35 kB
          Brian Larson
        3. math.wiki
          6 kB
          Brian Larson
        4. GroovyDecimalLiteral_3rd.patch
          2 kB
          Brian Larson

        Issue Links

          Activity

            People

              Unassigned Unassigned
              bl7385 Brian Larson
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: