Skip to content

Commit 378a287

Browse files
committed
root function
1 parent 81b633b commit 378a287

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

Math/Core.scope

+20-22
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,33 @@ The result of @"log10" of @"E".
1414
const dec LOG10_E = 0.434294481903251;
1515

1616
/%
17-
@asm
17+
@approx: This function uses the @"pow" function to compute the answer. This function
18+
is not 100% accurate but will provide a very good approximation.
19+
20+
Computes the square root of @"radicant".
1821

19-
Returns the square root of @"x".
20-
Uses the `sqrtsd` assembly instruction.
22+
Use @"root" if you want an arbitrary index.
2123
%/
22-
func dec sqrt(dec x) {
23-
dec o = 0.0;
24-
assembly {
25-
vlist_get rdi, $x$
26-
movq xmm0, rdi
27-
sqrtsd xmm0, xmm0
28-
movq rdi, xmm0
29-
vlist_set $o$
30-
}
31-
ret o;
24+
func dec sqrt(dec radicant) {
25+
ret pow(radicant, 0.5);
26+
}
27+
28+
/%
29+
@approx: This function uses the @"pow" function to compute the answer. This function
30+
is not 100% accurate but will provide a very good approximation.
31+
32+
Computes the @"index"th root of @"radicant".
33+
34+
Use @"sqrt" for an index of `2`.
35+
%/
36+
func dec root(dec index, dec radicant) {
37+
ret pow(radicant, 1.0 / index);
3238
}
3339

3440
/%
3541
@complexity: O(log(@"exponent"))
3642

3743
Computes the power of @"base" (decimal) to @"exponent" (integer).
38-
39-
This function uses the divide-and-conquer algorithm.
4044
%/
4145
func dec powInt(dec base, int exponent) {
4246
// Special cases
@@ -80,8 +84,6 @@ func dec powInt(dec base, int exponent) {
8084
@todo: Add support for negative bases.
8185

8286
Computes @"base" ^ @"exponent".
83-
84-
This function uses @"exp" and @"ln" to compute the power.
8587
%/
8688
func dec pow(dec base, dec exponent) {
8789
// log10(base ^ exponent) = log10(base) * exponent
@@ -163,8 +165,6 @@ func dec log10(dec x) {
163165
is not 100% accurate but will provide a very good approximation.
164166

165167
Computes the natural log (log base e) of @"x".
166-
167-
This function uses @"log10" to compute the natural log.
168168
%/
169169
func dec ln(dec x) {
170170
// ln(x) = log10(x) / log10(e)
@@ -179,8 +179,6 @@ Computes the log of @"x" with an arbitrary base, @"base".
179179

180180
Use @"log10" for a base of `10`.
181181
Use @"ln" for a base of `e`.
182-
183-
This function uses two calls to @"log10" to compute the result.
184182
%/
185183
func dec log(dec x, dec base) {
186184
if (base <= 0.0) {
@@ -196,7 +194,7 @@ func dec log(dec x, dec base) {
196194
@approx: This function uses the Taylor series expansion of exp. This function
197195
is not 100% accurate but will provide a very good approximation.
198196

199-
Computes e ^ @"x".
197+
Computes e ^ @"x" or exp(@"x").
200198
%/
201199
func dec exp(dec x) {
202200
// Special cases

0 commit comments

Comments
 (0)