Skip to content

Instantly share code, notes, and snippets.

@terkel
Last active December 22, 2015 21:29
Show Gist options
  • Save terkel/6533355 to your computer and use it in GitHub Desktop.
Save terkel/6533355 to your computer and use it in GitHub Desktop.
// ----
// Sass (v3.2.10)
// Compass (v0.13.alpha.4)
// ----
// http://blog.livedoor.jp/dankogai/archives/51518565.html
// https://github.com/terkel/mathsass
$LN2: 0.6931471805599453;
$SQRT2: 1.4142135623730951;
@function frexp ($n) {
$s: 1;
$x: 0;
@if $n < 0 {
$s: -1;
$n: $n * -1;
}
@if $n == 0 {
$s: 0;
} @else if $n < 0.5 {
@while $n < 0.5 {
$n: $n * 2;
$x: $x - 1;
}
} @else if $n >= 1 {
@while $n >= 1 {
$n: $n / 2;
$x: $x + 1;
}
}
@return s $s, m $n, x $x;
}
@function ldexp ($x, $n) {
$b: if($n >= 0, 2, 1/2);
@if $n < 0 {
$n: $n * -1;
}
@while $n > 0 {
@if $n % 2 == 1 {
$x: $x * $b;
}
$b: $b * $b;
$n: floor($n * 0.5);
}
@return $x;
}
@function log ($n) {
@if $n <= 0 {
@return 0/0;
}
$k: map-get(frexp($n / $SQRT2), x);
$n: $n / ldexp(1, $k);
$n: ($n - 1) / ($n + 1);
$n2: $n * $n;
$i: 1;
$s: $n;
// do/while のつもり…
$n: $n * $n2;
$i: $i + 2;
$sp: $s;
$s: $s + $n / $i;
@while $sp != $s {
$n: $n * $n2;
$i: $i + 2;
$sp: $s;
$s: $s + $n / $i;
}
@return $LN2 * $k + 2 * $s;
}
@function map-get ($map, $key) {
@each $element in $map {
@if index($element, $key) == 1 {
@return nth($element, 2);
}
}
@warn '\"#{ $key }\" is not found in the list.';
@return '';
}
@function pow ($b, $n) {
@if $n == floor($n) {
$r: 1;
$s: 0;
@if $n < 0 {
$n: $n * -1;
$s: 1;
}
// $n: $n * 1;
@while $n > 0 {
@if $n % 2 == 1 {
$r: $r * $b;
}
$n: floor($n * 0.5);
$b: $b * $b;
}
@return if($s != 0, 1 / $r, $r);
} @else {
@return exp(log($b) * $n);
}
}
@function exp ($x) {
$ret: 0;
@for $n from 0 to 24 {
$ret: $ret + pow($x, $n) / fact($n);
}
@return $ret;
}
@function fact ($x) {
$ret: 1;
@while $x > 0 {
$ret: $ret * $x;
$x: $x - 1;
}
@return $ret;
}
.test {
content: pow(3, 0.5);
}
.test {
content: 1.73205;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment