Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First off - this has nothing to do with base 2 vs. base 10. All of your values are represented using base 10. Base 2 is not used whatsoever in this code.</p> <blockquote> <p>looks like I'm missing some link of understanding that takes place in how calculation was taking care of.</p> </blockquote> <p>Both <code>float</code> and <code>double</code> using a floating point representation for numbers, which is not 100% accurate. When you <em>display</em> the value, there is rounding taking place. Since <code>float</code> has inherently less precision, it's rounded to fewer decimal points, which can make it "appear" more accurate in some cases (even though it's actually less precise).</p> <p>In your case, the reason the "x" group and the "c" group display differently is that you're declaring your x1 and x2 variables like so:</p> <pre><code>double x1 = 0.2F; double x2 = 1.0F; </code></pre> <p>This is, effectively, the same as doing:</p> <pre><code>float temp = 0.2f; double x1 = temp; // Convert from float to double, which has a loss of precision temp = 1f; double x2 = temp; // Convert from float to double, which has a loss of precision </code></pre> <p>As such, <code>x1</code> and <code>x2</code> do not have exactly the same values as <code>c1</code> and <code>c2</code>. This is causing even more loss of precision, which later, when the subtraction occurs, is enough that the printing doesn't round anymore.</p> <p>A good article to read if you want to truly understand this is <a href="http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html">What Every Computer Scientist Should Know About Floating-Point Arithmetic</a>. Most common programming languages use similar representations for floating point numbers, so the issues are fairly universal.</p>
    singulars
    1. This table or related slice is empty.
    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