1
- # More Math Functions: Draft 1
1
+ # More Math Functions: Draft 1.1
2
2
3
3
* [ (Issue)] ( https://github.com/sass/sass/issues/851 ) *
4
4
5
5
This proposal adds the following members to the built-in ` sass:math ` module.
6
6
7
7
## Table of Contents
8
+ * [ Background] ( #background )
9
+ * [ Summary] ( #summary )
8
10
* [ Variables] ( #variables )
9
11
* [ ` $e ` ] ( #e )
10
12
* [ ` $pi ` ] ( #pi )
@@ -25,6 +27,55 @@ This proposal adds the following members to the built-in `sass:math` module.
25
27
* [ ` atan2() ` ] ( #atan2 )
26
28
* [ Edge cases] ( #edge-cases )
27
29
30
+ ## Background
31
+
32
+ > This section is non-normative.
33
+
34
+ Sass recently implemented a module system with a new built-in ` sass:math `
35
+ module. The demand for built-in math functions can now be fulfilled safely by
36
+ implementing them inside this module. None of these new functions will be made
37
+ available on the global namespace.
38
+
39
+ ## Summary
40
+
41
+ > This section is non-normative.
42
+
43
+ This proposal defines Sassified versions of all the mathematical functions in
44
+ the [ CSS Values and Units 4 Draft] [ ] , as well as logarithms and the constants
45
+ ` e ` and ` pi ` . Each function is basically equivalent to its mathematical form,
46
+ with stricter unit handling. Proper unit handling prevents these functions from
47
+ creating meaningless units. For instance, consider ` (1px)^(1/3) ` —what does
48
+ the unit ` px^(1/3) ` mean?
49
+
50
+ [ CSS Values and Units 4 Draft ] : https://drafts.csswg.org/css-values-4/#math
51
+
52
+ To avoid issues like this, the exponential functions—` log() ` , ` pow() ` , ` sqrt() ` —
53
+ accept only a unitless number as input, and output a unitless number.
54
+
55
+ The trig functions—` cos() ` , ` sin() ` , ` tan() ` —accept a SassScript number with a
56
+ unit, as long as that unit is an [ angle] [ ] type. If the input is a unitless
57
+ number, it is treated as though it were in ` rad ` . These functions output a
58
+ unitless number.
59
+
60
+ [ angle ] : https://drafts.csswg.org/css-values-4/#angles
61
+
62
+ The inverse trig functions—` acos() ` , ` asin() ` , ` atan() ` —accept a unitless number
63
+ and output a SassScript number in ` rad ` . ` atan2() ` is similar, but it accepts
64
+ two unitless numbers.
65
+
66
+ ` clamp() ` accepts three SassScript numbers with [ compatible] [ ] units: the
67
+ minimum value, preferred value, and maximum value. This function "clamps" the
68
+ preferred value in between the minimum and maximum values, while preserving
69
+ their units appropriately. For example, ` clamp(1in, 15cm, 12in) ` outputs ` 15cm ` ,
70
+ whereas ` clamp(1in, 1cm, 12in) ` outputs ` 1in ` .
71
+
72
+ [ compatible ] : ../spec/built_in_modules/math.md#compatible
73
+
74
+ ` hypot() ` accepts ` n ` SassScript numbers with compatible units, and outputs the
75
+ length of the ` n ` -dimensional vector that has components equal to each of the
76
+ inputs. Since the inputs' units may all be different, the output takes the unit
77
+ of the first input.
78
+
28
79
## Variables
29
80
30
81
### ` $e `
@@ -45,15 +96,13 @@ digits: `3.141592654`.
45
96
clamp($min, $number, $max)
46
97
```
47
98
48
- * If the units of ` $min ` , ` $number ` , and ` $max ` are not [ compatible] [ ] with each
99
+ * If the units of ` $min ` , ` $number ` , and ` $max ` are not compatible with each
49
100
other, throw an error.
50
101
* If ` $min >= $max ` , return ` $min ` .
51
102
* If ` $number <= $min ` , return ` $min ` .
52
103
* If ` $number >= $max ` , return ` $max ` .
53
104
* Return ` $number ` .
54
105
55
- [ compatible ] : ../spec/built_in_modules/math.md#compatible
56
-
57
106
### ` hypot() `
58
107
59
108
```
@@ -145,12 +194,11 @@ sqrt($number)
145
194
cos($number)
146
195
```
147
196
148
- * If ` $number ` has units but is not an [ angle] [ ] , throw an error.
197
+ * If ` $number ` has units but is not an angle, throw an error.
149
198
* If ` $number ` is unitless, treat it as though its unit were ` rad ` .
150
199
* If ` $number == Infinity ` , return ` NaN ` as a unitless number.
151
200
* Return the [ cosine] [ ] of ` $number ` , as a unitless number.
152
201
153
- [ angle ] : https://drafts.csswg.org/css-values-4/#angles
154
202
[ cosine ] : https://en.wikipedia.org/wiki/Trigonometric_functions#Right-angled_triangle_definitions
155
203
156
204
#### ` sin() `
0 commit comments