Skip to content

Instantly share code, notes, and snippets.

@Weiyuan-Lane
Last active June 23, 2019 09:30
Show Gist options
  • Save Weiyuan-Lane/c1c6eba56a81cbb7265e9a2dba4ce1e1 to your computer and use it in GitHub Desktop.
Save Weiyuan-Lane/c1c6eba56a81cbb7265e9a2dba4ce1e1 to your computer and use it in GitHub Desktop.
Function for computing sin of a input radian value
@import 'factorial';
$PI: 3.14159265358979323846;
$TWO_PI: $PI * 2;
$HALF_PI: $PI / 2;
$QUARTER_PI: $PI / 4;
$SIN_ITERATION: 10;
@function sin($rad) {
// Ensure value is between 0 to TWO_PI
$normalisedRad: $rad % $TWO_PI;
@if $normalisedRad < 0 {
$normalisedRad: $normalisedRad + $TWO_PI;
}
// Known whole numbers to return without computing
@if $normalisedRad == 0 or $normalisedRad == $PI {
@return 0;
}
@if $normalisedRad == $HALF_PI {
@return 1;
}
// Memorise squared rad val to reduce recomputation
$radSquare: $normalisedRad * $normalisedRad;
$radPowAccVal: $normalisedRad;
$factorialCounter: 1;
$factorialAccVal: 1;
$totalVal: $normalisedRad;
// Iteratively compute an approx value
@for $i from 0 to $SIN_ITERATION {
$radPowAccVal: $radPowAccVal * -$radSquare;
$factorialAccVal: factorial($factorialCounter + 2, $factorialCounter, $factorialAccVal);
$factorialCounter: $factorialCounter + 2;
$totalVal: $totalVal + ($radPowAccVal / $factorialAccVal);
}
@return $totalVal;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment