Note that there are some explanatory texts on larger screens.

plurals
  1. PODifferent results calculating the average of double variables contained in a vector of structs by altering code formatting?
    primarykey
    data
    text
    <p>I am currently fighting with a very odd behavior of OpenCV/C++. This is what i am doing:</p> <p>Updated 1: Here more informations:</p> <p>I calculate the gradient orientation for a set of pixels and store them in a <code>double</code> field of a self declared <code>struct</code>. The struct elements get stored in a vector.</p> <pre><code>myfunct1() { (...) c.grad_orientation = (sum_ori / highgrads.size()) + M_PI; // +M_PI because i just want to rotate the angle around 180°. (...) my_struct_vector.push_back(c); } </code></pre> <p>I later got several <code>c</code> stored in a vector and want to estimate the average of every single <code>grad_orientation</code>, stored in every struct-element of my vector looking like this:</p> <pre><code>myfunct0() { myfunct1(); //adds element to the my_struct_vector (...) int n = 0; double total_ori = 0.0; for (uint i = 0; i &lt; my_struct_vector.size(); ++i) { total_ori += my_struct_vector[i].grad_orientation; n++; } azimuth = (total_ori / n); cout &lt;&lt; "Average: " &lt;&lt; azimuth * 180/M_PI &lt;&lt; endl; // print out in degrees } </code></pre> <p>Now for the funny part: If i do this under certain circumstances, the <code>cout</code> prints <code>215.963</code>. This is the result i got most often (EXACTLY this result). In some cases, if i add these matrices named above or (yes, really) if i move the double field holding the <code>grad_orientation</code> in the struct one column higher in terms of code i get <code>223.442</code>. So the difference between the two results in the code is <em>only</em> the following:</p> <pre><code>struct my_struct { std::vector&lt;cv::Point&gt; contour_shadow; std::vector&lt;cv::Point&gt; contour_light; cv::RotatedRect rect; cv::Point pf; cv::Point pc; double grad_orientation; // this line moved one column down, beneath "grad_flag" results in a differing result. bool grad_flag; double grad_magnitude; </code></pre> <p>};</p> <p>The printed results do not only depend on the position of the column in the struct declaration but also when i alter different parts of my code.</p> <p>This may has something to do with double precision, but why does it alter if i move columns of code?</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.
    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