Cálculo Manual de Seno/Coseno/Tangente/Secante/Cosecante

Cálculo Manual de Seno/Coseno/Tangente/Secante/Cosecante

El siguiente código está escrito en JavaScript:

  
function factorial(n)
{
 var ret=1;

 for(var s=1;s<n;s++)
  ret*=(s+1);

 return ret;
}





function pow(n,power)
{
 var ret;

 if(power==0)return 1;
 if(power%1!=0)return NaN;
 if(power<0)
 {
  if(n==0)return Infinity;

  ret=1;
  for(var j=power;j<0;j++)
  ret/=n;
 }
  else
  {
   ret=n;
   for(var j=0;j<power-1;j++)
   ret*=n;
 }

 return ret;
}







function sin(theta, num_iterations)
{
 var ret=theta;

 var tmp=0;
 var sign=-1;
 for(var s=0,exp_and_fact=3;s<num_iterations;s++,exp_and_fact+=2)
 {
   ret+=(pow(theta,exp_and_fact)/factorial(exp_and_fact))*sign;
   sign*=-1;
 }
  return ret;
}






function cos(theta, num_iterations)
{
 var ret=1;

 var tmp=0;
 var sign=-1;
 for(var s=0,exp_and_fact=2;s<num_iterations;s++,exp_and_fact+=2)
 {
   ret+=(pow(theta,exp_and_fact)/factorial(exp_and_fact))*sign;
   sign*=-1;
 }
  return ret;
}






function tan(theta, num_iterations)
{
 return sin(theta, num_iterations)/cos(theta, num_iterations);
}



function secant(theta, num_iterations)
{
 return 1/cos(theta, num_iterations);
}




function cosecant(theta, num_iterations)
{
 return 1/sin(theta, num_iterations);
}




Evaluar la Calidad del Documento