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

Modulo operations with floating point numbers are not always correct

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      The math problem 2.4%0.2 (2.4 modulo 0.2) return 0.2, while the problem 3.2%0.2 returns 0.
      This is true for many cases of floating point modulo and should have all kinds of very negative effects in all sorts of software.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      System.out.println((2.4%0.2)+""); //return 0.2
      System.out.println((3.2%0.2)+""); //returns 0


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      2.4%0.2 and 3.2%0.2 should always return 0
      ACTUAL -
      result is 0.2, which is clearly not the case

      ---------- BEGIN SOURCE ----------
      System.out.println((2.4%0.2)+""); //return 0.2
      System.out.println((3.2%0.2)+""); //returns 0
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      BigDecimal can be used.

      FREQUENCY : always


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

              Created:
              Updated:
              Resolved: