math-extras
Version:
Useful mathematical functions which should be part of the JavaScript Math object.
53 lines (46 loc) • 975 B
JavaScript
(function() {
Math.radians = function(degrees) {
return degrees * Math.PI / 180;
};
Math.degrees = function(radians) {
return radians * 180 / Math.PI;
};
Math.sign = function(num) {
if (num) {
if (num < 0) {
return -1;
} else {
return 1;
}
} else {
return 0;
}
};
Math.factorial = function(x) {
if (x < 2) {
return 1;
} else {
return Math.factorial(x - 1) * x;
}
};
Math.log10 = function(x) {
return Math.log(x) / Math.LN10;
};
Math.erf = function(x) {
var a1, a2, a3, a4, a5, p, sign, t, y;
a1 = 0.254829592;
a2 = -0.284496736;
a3 = 1.421413741;
a4 = -1.453152027;
a5 = 1.061405429;
p = 0.3275911;
sign = 1;
if (x < 0) {
sign = -1;
}
x = Math.abs(x);
t = 1.0 / (1.0 + p * x);
y = 1.0 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * Math.exp(-x * x);
return sign * y;
};
}).call(this);