title: Why does using modulo on non-integer values lose floating-point precision?
body: <p>I am wondering why I am losing precision when using this code :</p>
  
  <pre><code>double x = 12.0456;    // or float : same result
  System.out.println(x); // outputs 12.0456 obviously
  x %= 1;                // should now be equal to 0.0456 right?
  System.out.println(x); // outputs 0.04560000000000031 or 0.045599937 when using float
  </code></pre>
  
  <p>12.0456 modulo 1 should equal 0.0456 right?
  But it shows a slightly different value, why do I keep losing precision? I mean the code should substract exactly 1 until the value is less than 1.</p>
  
  <p>However, I found out a way to get the correct value : </p>
  
  <pre><code>double x = 12.0456;
  System.out.println(x);
  x %= 1;
  System.out.println((float)x); //outputs 0.0456 exactly
  </code></pre>
  
  <p>This way works perfectly, but do you guys have a better solution?</p>
  
  <p><strong>I don't care which floating point type I should use, I just want to find a clean way to get the correct value!</strong> I don't like having to convert the value to a double and then to a float.</p>