/*
   ###########################################################################
   This function computes the circular cosine of an angle expressed in degrees
   in arbitrary precision of up to 100 decimals.

   ARGUMENTS:
   AngDegStr = Numerical angle string in degrees.
               The angle should be in the form of a string, such as '123.4567'
               and in the range from '-360' to '+360' degrees.

   Decimals  = 1 to 100 (Default = 32)

   OUTPUT:
   Circular cosine value for the given angular argument.

   Given angle (A), this function actually computes the sine of (90 - A),
   corresponding to the cosine of angle (A).

   ERRORS:
   Returns FALSE if non-numeric argument.

   NO DEPENDENCIES
   ###########################################################################
*/

   function BC_cos ($AngDegStr, $Decimals=32)
{
   $A = trim($AngDegStr);

// Error if argument is non-numeric.
   if (!Is_Numeric($A)) {return FALSE;}

// define Radians/Degrees interconversion constant to 256 decimals.
   $RadPerDeg = '0.01745329251994329576923690768488612713442871888541725456'
                . '0971914401710091146034494436822415696345094822123044925073'
                . '7905924838546922752810123984742189340471173191682450150107'
                . '6956169755358123860530516878869127117208703296358960264249'
                . '01877043509181733439396980';

// Ensure that output decimals do not exceed 100.
   $q = floor($Decimals);
   $q = ($q < 1)? 1 : $q;
   $q = ($q > 100)? 100 : $q;

// Set internal working decimals.
   $Q = 256;

   $A = trim($AngDegStr);
   $a = ($A > 270)? '-1':'1';
   $A = bcSub('90.0', $A, $Q);
   $A = ($A >= 360)? bcSub($A, '360.0', $Q) : $A;
   $x = bcMul($A, $RadPerDeg, $Q);
   $s = (substr($x, 0 ,1) == '-')? '-' : '';
   $x = Str_Replace('-', '', $x);
   $y = $denom = 0;

   for ($i=0;   $i < 60;   $i++)
  {
   $p = 2*$i + 1;
   for ($j=0;  $j < $p;  $j++) {$denom = ($j == 0)? 1 : bcMul($denom, $j+1);}
   $numer = (($i % 2 == 0)? '+':'-') . bcpow($x, $p, $Q);
   $y = bcAdd($y, bcDiv($numer, $denom, $Q), $Q);
  }

   $w = $s.bcAdd($y, '0.'.Str_Repeat('0', $q).'5', $q);
   $w = Str_Replace('--', '-', $w);
   $w = bcMul($a, $w, $q);
   $w = Rtrim(Rtrim($w, '0'), '.');
   return $w;

} // End of  BC_cos(...)