Skip to content

Commit

Permalink
Support more functions: gamma, zeta, fact, bellb, csc, sec, cot
Browse files Browse the repository at this point in the history
  • Loading branch information
jtsay362 committed Feb 23, 2015
1 parent f3351c9 commit 776b1d2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
24 changes: 10 additions & 14 deletions calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ Math.log10 = function(arg) {

function calc() {
this.eqcache = new Object;
this.replacements = {"sec" : "Calc.sec", "csc" : "Calc.csc", "cot" : "Calc.csc", "sqrt" : "Math.sqrt",
"asin" : "Calc.asin", "acos" : "Calc.acos", "atan" : "Calc.atan",
"sin" : "Calc.sin", "tan" : "Calc.tan", "cos" : "Calc.cos", "log" : "Math.log10", "pi" : "3.14159265358979", "e" : "2.71828183",
"abs" : "Math.abs", "ln" : "Math.log", "zeta" : "Calc.zeta", "gamma" : "Calc.gamma", "fact" : "Calc.fact", "bellb" : "Calc.bellb", "Math.pow" : "Math.asdf", "Calc.pow" : "pow",
"pow" : "Calc.pow", "Math.asdf" : "Calc.pow"};
this.angles = "radians";
this.loopcounter = 0;
this.eps = calcEps(); //Machine epsilon - the maximum expected floating point error
Expand Down Expand Up @@ -124,23 +119,24 @@ function calc() {
return sum;
}

var log2pi = Math.log(2 * Math.PI);
this.gamma = function(x) {
if (x > 1.0) {
return (exp(x * (ln(x) - 1) + 0.5 * (-ln(x) + log2pi) + 1 / (12 * x) - 1 / (360 * (x * x * x)) + 1 / (1260 * pow(x, 5)) - 1 / (1680 * pow(x, 7))));
if (x > 1.0) {
return (Math.exp(x * (Math.log(x) - 1) + 0.5 * (-Math.log(x) + log2pi) + 1 / (12 * x) - 1 / (360 * (x * x * x)) + 1 / (1260 * Math.pow(x, 5)) - 1 / (1680 * Math.pow(x, 7))));
}
if (x > -0.5) {
return (1.0 + 0.150917639897307 * x + 0.24425221666910216 * pow(x, 2)) / (x + 0.7281333047988399 * pow(x, 2) - 0.3245138289924575 * pow(x, 3));
return (1.0 + 0.150917639897307 * x + 0.24425221666910216 * Math.pow(x, 2)) / (x + 0.7281333047988399 * Math.pow(x, 2) - 0.3245138289924575 * Math.pow(x, 3));
}
if (x < 0) {
if (x == ~~x) {
return;
} else {
return Math.PI / (Math.sin(Math.PI * x) * Gamma((1 - x)));
return Math.PI / (Math.sin(Math.PI * x) * Calc.gamma((1 - x)));
}
} else {
return pow(x - 1, x - 1) * Math.sqrt(2 * Math.PI * (x - 1)) * exp(1 - x + 1 / (12 * (x - 1) + 2 / (5 * (x - 1) + 53 / (42 * (x - 1)))));
return Math.pow(x - 1, x - 1) * Math.sqrt(2 * Math.PI * (x - 1)) * Math.exp(1 - x + 1 / (12 * (x - 1) + 2 / (5 * (x - 1) + 53 / (42 * (x - 1)))));
}
}
};
this.fact = function(ff) {
if (ff === 0 || ff == 1) {
return 1;
Expand All @@ -151,7 +147,7 @@ function calc() {
}
return~~s;
} else if (ff != (~~ff) || ff < 0) {
return Gamma(ff + 1);
return Calc.gamma(ff + 1);
}
}
this.bellb = function(x) {
Expand All @@ -160,9 +156,9 @@ function calc() {
} else {
var sum = 0;
for (var inj = 0; inj < 5; inj++) {
sum += pow(inj, x) / fact(inj);
sum += Calc.pow(inj, x) / Calc.fact(inj);
}
return sum / e;
return sum / Math.E;
}
}

Expand Down
25 changes: 22 additions & 3 deletions parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,9 @@ var Parser = (function (scope) {
"sin": Calc.sin,
"cos": Calc.cos,
"tan": Calc.tan,
sec: Calc.sec,
csc: Calc.csc,
cot: Calc.cot,
"asin": Calc.asin,
"acos": Calc.acos,
"atan": Calc.atan,
Expand All @@ -360,7 +363,11 @@ var Parser = (function (scope) {
"floor": Math.floor,
"round": Math.round,
"-": neg,
"exp": Math.exp
"exp": Math.exp,
"gamma" : Calc.gamma,
"zeta" : Calc.zeta,
"fact" : Calc.fact,
"bellb" : Calc.bellb
};

this.ops2 = {
Expand All @@ -381,7 +388,14 @@ var Parser = (function (scope) {
"max": Math.max,
"pyt": pyt,
"pow": Math.pow,
"atan2": Math.atan2
"atan2": Math.atan2,
sec: Calc.sec,
csc: Calc.csc,
cot: Calc.cot,
"gamma" : Calc.gamma,
"zeta" : Calc.zeta,
"fact" : Calc.fact,
"bellb" : Calc.bellb
};

this.consts = {
Expand All @@ -404,6 +418,9 @@ var Parser = (function (scope) {
sin: Calc.sin,
cos: Calc.cos,
tan: Calc.tan,
sec: Calc.sec,
csc: Calc.csc,
cot: Calc.cot,
asin: Calc.asin,
acos: Calc.acos,
atan: Calc.atan,
Expand All @@ -415,13 +432,15 @@ var Parser = (function (scope) {
floor: Math.floor,
round: Math.round,
random: random,
fact : Calc.fact,
bellb : Calc.bellb,
fac: fac,
exp: Math.exp,
min: Math.min,
max: Math.max,
pyt: pyt,
pow: Math.pow,
atan2: Math.atan2,
atan2: Math.atan2,
E: Math.E,
PI: Math.PI
};
Expand Down

0 comments on commit 776b1d2

Please sign in to comment.