Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add ln/log and exp.
  • Loading branch information
colomon authored and sorear committed Jul 7, 2011
1 parent 981a202 commit 4c894a7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
39 changes: 39 additions & 0 deletions lib/Builtins.cs
Expand Up @@ -760,6 +760,45 @@ class SubstrLValue: Variable {
}
}

static Func<Variable,Variable> ln_d = ln;
public static Variable ln(Variable a1) {
P6any o1 = a1.Fetch();
int r1;
if (!o1.mo.is_any)
return HandleSpecial1(a1,o1, ln_d);
P6any n1 = GetNumber(a1, o1, out r1);

if (r1 == NR_COMPLEX) {
// Log z = ln r + iθ
Complex v1 = PromoteToComplex(r1, n1);
return MakeComplex(Math.Log(Math.Sqrt(v1.re * v1.re + v1.im * v1.im)),
Math.Atan2(v1.im, v1.re));
}
{
double v1 = PromoteToFloat(r1, n1);
return MakeFloat(Math.Log(v1));
}
}

static Func<Variable,Variable> exp_d = exp;
public static Variable exp(Variable a1) {
P6any o1 = a1.Fetch();
int r1;
if (!o1.mo.is_any)
return HandleSpecial1(a1,o1, exp_d);
P6any n1 = GetNumber(a1, o1, out r1);

if (r1 == NR_COMPLEX) {
Complex v1 = PromoteToComplex(r1, n1);
return MakeComplex(Math.Exp(v1.re) * Math.Cos(v1.im),
Math.Exp(v1.re) * Math.Sin(v1.im));
}
{
double v1 = PromoteToFloat(r1, n1);
return MakeFloat(Math.Exp(v1));
}
}

static Func<Variable,Variable> sin_d = sin;
public static Variable sin(Variable a1) {
P6any o1 = a1.Fetch();
Expand Down
5 changes: 5 additions & 0 deletions lib/CORE.setting
Expand Up @@ -127,6 +127,11 @@ my class Cool {
method sqrt() { sqrt self }
method sign() { sign self }
method conjugate() { self }
multi method exp() { Q:CgOp { (exp {self}) } }
multi method exp($base) { $base ** self }
method ln() { Q:CgOp { (ln {self}) } }
multi method log() { self.ln }
multi method log($base) { self.ln / $base.ln }
method sin() { Q:CgOp { (sin {self}) } }
method split($matcher, $limit?, :$all?) {
Expand Down

0 comments on commit 4c894a7

Please sign in to comment.