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

   INPUT:
   Numerical angle string in degrees and desired decimals precision.
   The angle should be in the form of a string, such as '45.6789' and in the
   range from '-360' to '+360' degrees.

   Decimals = 1 to 100 (Default = 32 decimals)

   OUTPUT:
   Circular sine value for the given angular argument.
   Computed by brute-force series evaluation.

   ERRORS:
   Returns error message if non-numeric argument.

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

   function BC_Sin ($AngDegStr, $Decimals=32)
{
// Set number of decimals used for output.
// Decimals = 1 to 100
   $q = floor(trim($Decimals));
   $q = ($q <   1)?   1 : $q;
   $q = ($q > 100)? 100 : $q;

// Set 256 decimals for internal computations.
   $Q = 256;

   $A = trim($AngDegStr);

// define Radians/Degrees interconversion constant to 256 decimals.
   $RadPerDeg = '0.017453292519943295769236907684886127134428718885417254'
                . '56097191440171009114603449443682241569634509482212304492'
                . '50737905924838546922752810123984742189340471173191682450'
                . '15010769561697553581238605305168788691271172087032963589'
                . '6026424901877043509181733439396980';

// Modulate angle to fall in the range from 0 to 360 degrees.
   $NumSign = ($A < 0)? '-' : '';
   $A = Ltrim($A, '-');
   $w = bcSub($A, bcMul('360', bcDiv($A, '360')), $Q);
   $A = bcAdd($w, (($w < 0)? '360' : '0'), $Q);

   $x = bcMul($A, $RadPerDeg, $Q);
   $s = $NumSign;
   $x = Str_Replace('-', '', $x);
   $y = $denom = 0;

   $w = '0.' . Str_Repeat('0', $q-1) . '1';

   for ($i=0;   $i < 50;   $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);
  }

   $y = bcAdd($y, '0.' . Str_Repeat('0', $q) . '5', $q);
   $y = Rtrim(Rtrim($y, '0'), '.');
   $w = Str_Replace('--', '', "$s$y");
   if ($w == '-0') {$w = '0';}
   return $w;

} // End of  BC_Sin (...)