Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8203471

HALF_DOWN Rounding Mode gives incorrect results

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Not an Issue
    • Affects Version/s: 8u161
    • Fix Version/s: None
    • Component/s: core-libs
    • Subcomponent:
    • CPU:
      x86
    • OS:
      windows_7

      Description

      ADDITIONAL SYSTEM INFORMATION :
      java version "1.8.0_101"
      Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
      Java HotSpot(TM) Client VM (build 25.101-b13, mixed mode)

      OS Details:
      Windows 7 Ultimate
      32 bit OS
      RAM: 4 GB

      A DESCRIPTION OF THE PROBLEM :
      Following is the description of HALF_DOWN Rounding mode mentioned here (https://docs.oracle.com/javase/7/docs/api/java/math/RoundingMode.html#HALF_DOWN):
      Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round down. Behaves as for RoundingMode.UP if the discarded fraction is > 0.5; otherwise, behaves as for RoundingMode.DOWN.

      In actual, when the neighbors are equidistant, then ROUND DOWN doesn't happen




      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the code mentioned in the section 'Source code for an executable test case' to reproduce the issue.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Expected (The definition says, in case the neighbors are equidistant, then ROUND DOWN i.e towards 0)
      HALF_DOWN(37.45) = 37.4
      ACTUAL -
      Actual:
      HALF_DOWN(37.45) = 37.5

      ---------- BEGIN SOURCE ----------
      import java.io.BufferedReader;
      import java.io.InputStreamReader;
      import java.math.RoundingMode;
      import java.text.DecimalFormat;

      public class RoundingNumbers
      {
      static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      public static void main(String[] args)
      {
      double d[]={37.44, 37.45, 37.46, -37.44, -37.45, -37.46};
      DecimalFormat df = new DecimalFormat("#.#");
      df.setRoundingMode(RoundingMode.HALF_DOWN);

      for(int i=0; i<d.length; i++)
      System.out.println("HALF_DOWN("+d[i]+")="+df.format(d[i]));

      }
      }

      ----

      Here is the output:

      HALF_DOWN(37.44)=37.4
      HALF_DOWN(37.45)=37.5
      HALF_DOWN(37.46)=37.5
      HALF_DOWN(-37.44)=-37.4
      HALF_DOWN(-37.45)=-37.5
      HALF_DOWN(-37.46)=-37.5
      ---------- END SOURCE ----------

      FREQUENCY : always


        Attachments

          Activity

            People

            • Assignee:
              psonal Pallavi Sonal (Inactive)
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: