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

Problem with casting double to long

XMLWordPrintable

    • x86_64
    • linux

      A DESCRIPTION OF THE PROBLEM :
      For a number greater than or equal to 9_007_199_254_740_993.0, casting to long results in loss of precision

      REGRESSION : Last worked in version 17.0.10

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create a simple java project

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      (long)(9007199254740993.0) == 9007199254740993L
      ACTUAL -
      (long)(9007199254740993.0) == 9007199254740992L

      ---------- BEGIN SOURCE ----------
        public static void main(String[] args) {
          long startVal = 9_007_199_254_740_993L;
          long stopVal = 9_007_199_254_741_100L;

          for (long i = startVal; i < stopVal; i++) {
            long rounded = (long) (i + 0.0);
            if (i != rounded) {
              System.out.println(i + " != " + rounded);
            }
          }
        }
      ---------- END SOURCE ----------

      FREQUENCY : always


            darcy Joe Darcy
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: