Note that there are some explanatory texts on larger screens.

plurals
  1. POC# double precision issue, how to detect and handle in a safe way
    primarykey
    data
    text
    <p>I was looking for satisfactory and safe workaround to my double precision issue specified to this problem:</p> <p><img src="https://i.stack.imgur.com/SUPn4.png" alt="enter image description here"></p> <p>This program tries to find how many small circle can fit into a large circle. It fills the large circle and then culls those that intersect the large circumference. using this formula:</p> <pre><code>distance_small_pos_from_center + small_radius &lt; big_radius </code></pre> <p>All calculations were in double, except for screen output on WinForms which takes int for coords.</p> <p><img src="https://i.stack.imgur.com/OtsgM.png" alt="enter image description here"></p> <p>The above image shows the result of the culling. You can see that it is not symmetric when it should really be because the constraint is that there must be one small circle exactly in the center. I step through the code and find that this is because some calculations yield, for example, </p> <pre><code>99.9999999 &lt; 100 </code></pre> <p>This answer <a href="https://stackoverflow.com/questions/6875007/c-double-precision">C++ double precision and rounding off</a> says we should use all the precision available, but in this case, I had to do a <code>Math.Round(distance_small_pos_from_center + small_radius, 3)</code> using 3 arbitarily.</p> <p>The result of the culling differs very much without Math.Round. In retrospect, this is one kind of bug that is hard to detect <strong>if I had not drawn it out</strong>. Maybe I did something wrong, or didn't understand doubles as much as I thought I had.</p> <p>So, anyone has solutions or tips to avoid this kind of problem?</p>
    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.
 

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