2 Replies Latest reply: May 29, 2007 10:59 AM by 807606 RSS

    ++ increment of float inacurate with no compiles warnings???

    807606
      Hi,

      I have a simple class that uses the ++ opperator to increment a floting point variable. However the result is not correct and there are no compiler warnings about loss of precision. The class is....
      class Test
      {
          public static void main(String[] args)
          {        
              float x = 1.123f;
              
              x++;
              
              System.out.println(x);
          }
      }
      However the result of X is 2.1230001. X will now fail any if statements such as
      if (x = 2.123)
      {
              // Do Something
       }
      So my question is
      1) Why does this happen
      2) Is it possible to use the ++ operator on a floating point variable with accuracy?

      I'm studying for the SCJP and attention to details like this is everything!!
      Thanks!
      Alan K.