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

Float/Double trunc methods perform rounding for negative values

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.4.5
    • Fix Version/s: 2.4.6
    • Component/s: groovy-jdk
    • Labels:
      None

      Description

      The trunc methods on Float/Double use the Math.floor method for all values, however for negative values this causing rounding. I think for negative values Math.ceil needs to be used.

      Here are some asserts to show the rounding:

      Float f = -123.739f
      Double d = -123.739d
      
      assert -123f == f.trunc() // fails: -124
      assert -123d == d.trunc() // fails: -124
      
      assert​ -123.73f == f.trunc(2)​ // fails: -123.74
      assert -123.73d == d.trunc(2) // fails: -123.73​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
      

        Attachments

          Activity

            People

            • Assignee:
              pascalschumacher Pascal Schumacher
              Reporter:
              jwagenleitner John Wagenleitner
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: