forked from microsoft/pxt-sample
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathextraMath.ts
More file actions
229 lines (208 loc) · 5.43 KB
/
Copy pathextraMath.ts
File metadata and controls
229 lines (208 loc) · 5.43 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* Extra Maths
*
*/
namespace pxsim.extraMath {
//% block="convert $number to text"
//% inlineInputMode=inline
//% weight=99
//% number.defl=0
//% group="Conversion"
//% help=extraMath/all
/**
* Convert any number to text
*/
export function convert(number: number = 0) {
return number.toString()
}
//% block="convert $angle to radians"
//% inlineInputMode=inline
//% angle.defl=0
//% weight=95
//% help=extraMath/all
//% group="Conversion"
/**
* Convert an angle in degrees to an angle in radians
* @param angle The angle in degrees
*/
export function radians(angle: number = 0) {
return Math.PI * angle / 180
}
//% block="convert $angle to degrees"
//% inlineInputMode=inline
//% angle.defl=0
//% weight=94
//% help=extraMath/all
//% group="Conversion"
/**
* Convert an angle in degrees to an angle in degrees
* @param angle The angle in degrees
*/
export function degrees(angle: number = 0) {
return 180 * angle / Math.PI
}
//% block="sin $theta°"
//% inlineInputMode=inline
//% theta.defl=0
//% weight=93
//% help=extraMath/all
//% group="Trigonometry in Degrees"
/**
* Sine of angle in degrees
* @param theta The angle in degrees
*/
export function sin(theta: number = 0) {
return Math.sin(extraMath.radians(theta))
}
//% block="cos $theta°"
//% inlineInputMode=inline
//% theta.defl=0
//% weight=92
//% help=extraMath/all
//% group="Trigonometry in Degrees"
/**
* Cosine of angle in degrees
* @param theta The angle in degrees
*/
export function cos(theta: number = 0) {
return Math.cos(extraMath.radians(theta))
}
//% block="tan $theta°"
//% inlineInputMode=inline
//% theta.defl=0
//% weight=91
//% help=extraMath/all
//% group="Trigonometry in Degrees"
/**
* Tangent of angle in degrees
* @param theta The angle in degrees
*/
export function tan(theta: number = 0) {
return Math.tan(extraMath.radians(theta))
}
//% block="arcsin $x"
//% inlineInputMode=inline
//% x.defl=0
//% weight=90
//% help=extraMath/all
//% group="Trigonometry in Degrees"
/**
* Inverse sine of angle in degrees
* @param x The operand for inverse sin
*/
export function arcsin(x: number = 0) {
return extraMath.degrees(Math.asin(x))
}
//% block="arccos $x"
//% inlineInputMode=inline
//% x.defl=0
//% weight=89
//% help=extraMath/all
//% group="Trigonometry in Degrees"
/**
* Inverse sine of angle in degrees
* @param x cos
*/
export function arccos(x: number = 0) {
return extraMath.degrees(Math.acos(x))
}
//% block="arctan $x"
//% inlineInputMode=inline
//% x.defl=1
//% weight=88
//% help=extraMath/all
/**
* Inverse sine of angle in degrees
* @param x The operand for inverse tan
*/
export function arctan(x: number = 0) {
return extraMath.degrees(Math.atan(x))
}
//% block="arctan2 $y $x"
//% inlineInputMode=inline
//% x.defl=0
//% y.defl=0
//% weight=87
//% help=extraMath/all
//% group="Trigonometry in Degrees"
/**
* Inverse sine of angle in degrees
* @param x The "adjacent" operand for inverse tan
* @param y The "opposite" operand for inverse tan
*/
export function arctan2(y: number = 0, x: number = 0) {
return extraMath.degrees(Math.atan2(y, x))
}
//% block="ln $x"
//% inlineInputMode=inline
//% x.defl=1
//% weight=86
//% group="Logarithms"
//% help=extraMath/all
/**
* Inverse sine of angle in degrees
* @param x The operand for the natural log
*/
export function ln(x: number = 1) {
return Math.log(x)
}
//% block="log $x base $base"
//% inlineInputMode=inline
//% x.defl=1
//% base.defl=10
//% weight=85
//% help=extraMath/all
//% group="Logarithms"
/**
* Inverse sine of angle in degrees
* @param x The operand for the log
* @param base The base to take the logarithm with respect to
*/
export function log(x: number = 1, base: number = 10) {
return Math.log(x) / Math.log(base) // Use change of base formula
}
//% block="π"
//% inlineInputMode=inline
//% weight=84
//% help=extraMath/all
//% group="Constants"
/**
* The mathematical constant Pi, the circle constant.
*/
export function Pi() {
return Math.PI
}
//% block="e"
//% inlineInputMode=inline
//% weight=83
//% help=extraMath/all
//% group="Constants"
/**
* The mathematical constant e, the exponential constant.
*/
export function e() {
return Math.E
}
//% block="ϕ"
//% inlineInputMode=inline
//% weight=82
//% help=extraMath/all
//% group="Constants"
/**
* The mathematical constant Phi, the golden ratio.
*/
export function Phi() {
return (Math.sqrt(5) + 1) / 2.0
}
//% block="√2"
//% inlineInputMode=inline
//% weight=81
//% group="Constants"
//% help=extraMath/all
/**
* The mathematical constant the square root of 2.
*/
export function Sqrt2() {
return Math.sqrt(2)
}
}