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,54 @@ 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, what is the unit of ` (1px) ` <sup ><code >2</code ></sup >? A ` px ` <sup ><code >2</code ></sup >?
48
+
49
+ [ CSS Values and Units 4 Draft ] : https://drafts.csswg.org/css-values-4/#math
50
+
51
+ To avoid issues like this, the exponential functions—` log() ` , ` pow() ` , ` sqrt() ` —
52
+ accept only a unitless number as input, and output a unitless number.
53
+
54
+ The trig functions—` cos() ` , ` sin() ` , ` tan() ` —accept a SassScript number with a
55
+ unit, as long as that unit is an [ angle] [ ] type. If the input is a unitless
56
+ number, it is treated as though it were in ` rad ` . These functions output a
57
+ unitless number.
58
+
59
+ [ angle ] : https://drafts.csswg.org/css-values-4/#angles
60
+
61
+ The inverse trig functions—` acos() ` , ` asin() ` , ` atan() ` —accept a unitless number
62
+ and output a SassScript number in ` rad ` . ` atan2() ` is similar, but it accepts
63
+ two unitless numbers.
64
+
65
+ ` clamp() ` accepts three SassScript numbers with [ compatible] [ ] units: the
66
+ minimum value, preferred value, and maximum value. This function "clamps" the
67
+ preferred value in between the minimum and maximum values, while preserving
68
+ their units appropriately. For example, ` clamp(1in, 15cm, 12in) ` outputs ` 15cm ` ,
69
+ whereas ` clamp(1in, 1cm, 12in) ` outputs ` 1in ` .
70
+
71
+ [ compatible ] : ../spec/built_in_modules/math.md#compatible
72
+
73
+ ` hypot() ` accepts ` n ` SassScript numbers with compatible units, and outputs the
74
+ length of the ` n ` -dimensional vector that has components equal to each of the
75
+ inputs. Since the inputs' units may all be different, the output takes the unit
76
+ of the first input.
77
+
28
78
## Variables
29
79
30
80
### ` $e `
@@ -45,15 +95,13 @@ digits: `3.141592654`.
45
95
clamp($min, $number, $max)
46
96
```
47
97
48
- * If the units of ` $min ` , ` $number ` , and ` $max ` are not [ compatible] [ ] with each
98
+ * If the units of ` $min ` , ` $number ` , and ` $max ` are not compatible with each
49
99
other, throw an error.
50
100
* If ` $min >= $max ` , return ` $min ` .
51
101
* If ` $number <= $min ` , return ` $min ` .
52
102
* If ` $number >= $max ` , return ` $max ` .
53
103
* Return ` $number ` .
54
104
55
- [ compatible ] : ../spec/built_in_modules/math.md#compatible
56
-
57
105
### ` hypot() `
58
106
59
107
```
@@ -145,12 +193,11 @@ sqrt($number)
145
193
cos($number)
146
194
```
147
195
148
- * If ` $number ` has units but is not an [ angle] [ ] , throw an error.
196
+ * If ` $number ` has units but is not an angle, throw an error.
149
197
* If ` $number ` is unitless, treat it as though its unit were ` rad ` .
150
198
* If ` $number == Infinity ` , return ` NaN ` as a unitless number.
151
199
* Return the [ cosine] [ ] of ` $number ` , as a unitless number.
152
200
153
- [ angle ] : https://drafts.csswg.org/css-values-4/#angles
154
201
[ cosine ] : https://en.wikipedia.org/wiki/Trigonometric_functions#Right-angled_triangle_definitions
155
202
156
203
#### ` sin() `
0 commit comments