Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The 5.0 just shows that the <em>precise</em> result as Java understands it is closer to 5.0 than it is to any other double. That doesn't mean the precise result of the operation is <em>exactly</em> 5.</p> <p>Now when you ask for the modulus, you're able to down to a much finer level of detail, because the result isn't pinned to having the "5" part.</p> <p>That's not a great explanation, but imagine you had a <em>decimal</em> floating point type with 4 digits of precision. What's the result of 1000 / 99.99 and 1000 % 99.99?</p> <p>Well, the real result starts with 10.001001 - so you have to round that to 10.00. However, the remainder is 0.10, which you can express. So again, it <em>looks</em> like the division gives you a whole number, but it doesn't <em>quite</em>.</p> <p>With that in mind, bear in mind that your literal of 5.6 is <em>actually</em> 5.5999999999999996447286321199499070644378662109375. Now clearly 28.0 (which *can) be represented exactly divided by that number isn't exactly 5.</p> <p>EDIT: Now if you perform the result with <em>decimal</em> floating point arithmetic using <code>BigDecimal</code>, the value really <em>is</em> exactly 5.6, and there are no problems:</p> <pre><code>import java.math.BigDecimal; public class Test { public static void main(String[] args) { BigDecimal x = new BigDecimal("28.0"); BigDecimal y = new BigDecimal("5.6"); BigDecimal div = x.divide(y); BigDecimal rem = x.remainder(y); System.out.println(div); // Prints 5 System.out.println(rem); // Prints 0.0 } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload