function s = mySin(x)

s = 0; sgn = 1;  % "sgn" is a sign variable that changes sign with each iteration
                 % this avoids the silly inefficient calculation of (-1)^n

for n=1:2:999    % Note that the step-size is two, so we loop over odd "n" only
    
    an = x^n / factorial(n);
    s = s + sgn * an;
    sgn = -sgn;
    
    if ( abs(an) < 1e-16 ) 
        return;
    elseif ( abs(an) == Inf ) 
        fprintf('Term #%d is too large: terminating\n', fix(n/2));
        return;
    elseif ( isnan(an) )
        fprintf('NaN encountered at term #%d: terminating\n', fix(n/2));
        return;
    end
    
end
        
