Skip to content

Instantly share code, notes, and snippets.

@jslegers
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jslegers/dbab91f74a4e603458ac to your computer and use it in GitHub Desktop.
Save jslegers/dbab91f74a4e603458ac to your computer and use it in GitHub Desktop.
// Some code is my own
// Some code is coming from https://github.com/adambom/Sass-Math/blob/master/math.scss
// Some code is coming from http://thesassway.com/advanced/inverse-trigonometric-functions-with-sass
// Some code coming from http://thesassway.com/advanced/math-sequences-with-sass
@function power($base, $exponent) {
$ret: 1;
@if $exponent > 0 {
@for $i from 1 through $exponent {
$ret: $ret * $base;
}
} @else if $exponent < 0 {
@for $i from $exponent to 0 {
$ret: $ret / $base;
}
}
@return $ret;
}
@function factorial ($x) {
$ret: 1;
@if $x > 0 {
@while $x > 0 {
$ret: $ret * $x;
$x: $x - 1;
}
}
@return $ret;
}
@function sin ($x) {
$ret: 0;
@for $n from 0 to 25 {
$ret: $ret + power(-1, $n) * power($x, 2 * $n + 1) / factorial(2 * $n + 1);
}
@return $ret;
}
@function cos ($x) {
$ret: 0;
@for $n from 0 to 25 {
$ret: $ret + power(-1, $n) * power($x, 2 * $n) / factorial(2 * $n);
}
@return $ret;
}
@function exp ($x) {
$ret: 0;
@for $n from 0 to 25 {
$ret: $ret + power($x, $n) / factorial($n);
}
@return $ret;
}
@function ln($x) {
$ret: 0;
$n: 1;
$dx: .001;
@while $n <= $x {
$ret: $ret + $dx / $n;
$n: $n + $dx;
}
@return $ret;
}
@function sqrt($x) {
@return exp(0.5 * ln($x));
}
$default-threshold: pi()/180/10;
@function convert-angle($value, $unit-name) {
$factors: (
rad: 1rad,
deg: 180deg/pi(),
grad: 200grad/pi(),
turn: .5turn/pi()
);
@if not unitless($value) {
@warn '`#{$value}` should be unitless';
@return false;
}
@if not map-has-key($factors, $unit-name) {
@warn 'unit `#{$unit-name}` is not a valid unit - please make sure it is either `deg`, `rad`, `grad` or `turn`';
@return false;
}
@return $value*map-get($factors, $unit-name);
}
@function asin($z, $unit-name: deg, $threshold: $default-threshold) {
$sum: 0;
$complement: false;
$sign: $z/abs($z);
$z: abs($z);
@if $z > sin(pi()/4) {
$complement: true;
$z: sqrt(1 - pow($z, 2));
}
$term: $z;
$i: 0;
$k: 1;
@while $term > $threshold {
$sum: $sum + $term;
$i: $i + 1;
$k: $k*(2*$i - 1)/(2*$i);
$j: 2*$i + 1;
$term: $k*pow($z, $j)/$j;
}
@return convert-angle($sign*(if($complement, pi()/2 - $sum, $sum)), $unit-name);
}
@function acos($z, $unit-name: deg, $threshold: $default-threshold) {
@return convert-angle(pi()/2, $unit-name) - asin($z, $unit-name, $threshold);
}
@function atan($z, $unit-name: deg, $threshold: $default-threshold) {
@return asin($z/sqrt(1 + pow($z, 2)), $unit-name, $threshold);
}
@function roundfloat($v, $d) {
$d : power(10,$d);
@return round($v * $d) / $d;
}
@function fibonacci($n) {
$fib: 0 1;
@for $i from 1 through $n {
$new: nth($fib, length($fib)) + nth($fib, length($fib) - 1);
$fib: append($fib, $new);
}
@return $fib;
}
@function juggler($n) {
$juggler: ($n);
@while nth($juggler, length($juggler)) != 1 {
$last : nth($juggler, length($juggler));
$x : if($last % 2 == 0, 1/2, 3/2);
$new : pow($last, $x);
$juggler : append($juggler, $new);
}
@return $juggler;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment