-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path_math.scss
50 lines (42 loc) · 1.22 KB
/
_math.scss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
@function ch-max($v1, $v2) {
@return if($v1 > $v2, $v1, $v2);
}
@function ch-min($v1, $v2) {
@return if($v1 < $v2, $v1, $v2);
}
@function ch-gcd($a, $b) {
// From: http://rosettacode.org/wiki/Greatest_common_divisor#JavaScript
@if ($b != 0) {
@return ch-gcd($b, $a % $b);
} @else {
@return abs($a);
}
}
@function ch-pow($base, $exponent, $prec: 12) {
// Handles decimal exponents by trying to convert them into a fraction and then use a nthRoot-algorithm for parts of the calculation
@if (floor($exponent) != $exponent) {
$prec2 : ch-pow(10, $prec);
$exponent: round($exponent * $prec2);
$denominator: ch-gcd($exponent, $prec2);
@return ch-nth-root(ch-pow($base, $exponent / $denominator), $prec2 / $denominator, $prec);
}
$value: $base;
@if $exponent > 1 {
@for $i from 2 through $exponent {
$value: $value * $base;
}
} @else if $exponent < 1 {
@for $i from 0 through -$exponent {
$value: $value / $base;
}
}
@return $value;
}
@function ch-nth-root($num, $n: 2, $prec: 12) {
// From: http://rosettacode.org/wiki/Nth_root#JavaScript
$x: 1;
@for $i from 0 through $prec {
$x: 1 / $n * (($n - 1) * $x + ($num / ch-pow($x, $n - 1)));
}
@return $x;
}