Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is <em>integer truncation</em> .. you are dividing two integers => the result will be another integer with the fractional part thrown away. (So for instance in the case when the real result of an integer division would be 3.9, truncation would make it 3 (ie. it doesn't round)).</p> <p>In your case, if you change this to:</p> <pre><code>x2 = 360 / 4095.0; /* one of the operands now has a decimal point */ </code></pre> <p>you'll get</p> <pre><code>0.087912, 0.087912 </code></pre> <p>as output.</p> <p>I.e., as soon as <em>one or both</em> of the operands of the division operator <code>/</code> are float/doubles, the result will be too (i.e., it will be "promoted" to float/double). So I could have changed <code>x2</code> to</p> <pre><code>x2 = 360.0 / 4095.0; </code></pre> <p>or</p> <pre><code>x2 = 360.0 / 4095; </code></pre> <p>and would have gotten the same result as above.</p> <p>As mentioned by @chris just using a <code>.</code> is sufficient too.</p> <p>Re your question above about precision:</p> <p>You are already working with long doubles .. I don't think internally you can change anything unless you use some special library, but you can certainly display more digits. E.g., </p> <pre><code> printf("%Lf, %.20Lf \n",x1, x2); </code></pre> <p>will yield </p> <pre><code> 0.087912, 0.08791208791208791895 </code></pre> <p>Finally, as @edA-qa mort-ora-y reminds us, you can also <em>cast</em> values to certain types, but it matters <em>when</em> you do it. A simple example (note that the values end up as <code>float</code> <em>after</em> the assignment in any case since <code>v</code> is a <code>float</code>):</p> <pre><code>float v = 0; /* values shown are BEFORE assignment */ v = (5 / 2); /* value is 2 due to integer truncation before assignment */ v = (float) (5 / 2); /* 2.0 as integer division occurs 1st, then cast to float */ v = (float) 5 / 2; /* 2.5 since 5 becomes 5.0 through casting first. */ </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