Nan results when iterating using sin and cos functions
<p>I'm compiling this program using Code::Blocks 10.05 however normally I will get about 10 iterations done before it starts producing Nan in every single output. I was wondering if this is a problem caused by using the cos and sin functions and if there was a decent work around to avoid this?</p>

<p>I have to produce a lot of iterates because I am working on a project for University so it has to be accurate too. I looked up a few articles about how to avoid using sin and cos though I need to follow a few formulas rigorously otherwise the results I produce may be inaccurate so I'm not sure whether to compromise.</p>

<pre><code>    struct Particle // Need to define what qualities our particle has
{
   double dPosition;
   double dAngle;

};

Particle Subject;

void M1(double &amp;x, double &amp;y) //Defines movement if particle doesn't touch inner   boundary
{
    x = x + 2*y;
}

double d = 0.25; //This can and will be changed when I need to find a distance between
                // the two cricles at a later stage


void M2(double &amp;x,double &amp;y, double d) //Defines movement of a particle if it impacts the inner boundary
{
    double z = asin(-(sin(y)+d*cos(x + y))/0.35);
    double y1 = y;
    y = asin(-0.35*sin(z) + d*cos(x + y + 2*z));
    x = y + y1 + x + 2*z;
}

int main()
{
    cout &lt;&lt; "Please tell me where you want this particle to start positions-wise? (Between 0 and 2PI" &lt;&lt; endl;
    cin &gt;&gt; Subject.dPosition;
    cout &lt;&lt; "Please tell me the angle that you would like it to make with the normal? (Between 0 and PI/2)" &lt;&lt; endl;
    cin &gt;&gt; Subject.dAngle;
    cout &lt;&lt; "How far would you like the distances of the two middle circles to be?" &lt;&lt; endl;
    double d;
    cin &gt;&gt; d;

    // These two functions are to understand where the experiment begins from.
    // I may add a function to change where the circle starts however I will use radius = 0.35 throughout

    cout &lt;&lt; "So position is: " &lt;&lt; Subject.dPosition &lt;&lt; endl;
    cout &lt;&lt; "And angle with the normal is: " &lt;&lt; Subject.dAngle &lt;&lt;endl;

    int n=0;
    while (n &lt;= 100) //This is used to iterate the process and create an array of Particle data points
    {               // in order to use this data to build up Poincare diagrams.

    {
        while (Subject.dPosition &gt; 2*M_PI)
            Subject.dPosition = Subject.dPosition - 2*M_PI;
    }
    {
        if (0.35 &gt;= abs(0.35*cos(Subject.dPosition + Subject.dAngle)+sin(Subject.dAngle))) //This is the condition of hitting the inner boundary
            M2(Subject.dPosition, Subject.dAngle, d); //Inner boundary collision
        else
            M1(Subject.dPosition, Subject.dAngle); // Outer boundary collision
    };
    cout &lt;&lt; "So position is: " &lt;&lt; Subject.dPosition &lt;&lt; endl;
    cout &lt;&lt; "And angle with the normal is: " &lt;&lt; Subject.dAngle &lt;&lt;endl;
    n++;
}
    return 0;
}
</code></pre>
