UNPKG

pybuiltinfunc

Version:

Python built-in Functions and Modules in Javascript

104 lines (103 loc) 3.28 kB
module.exports.sqrt = (some = 0) => parseFloat(Math.sqrt(some)); module.exports.acos = (num = 0) => Math.acos(num); module.exports.acosh = (num = 0) => Math.acosh(num); module.exports.asin = (num = 0) => Math.asin(num); module.exports.asinh = (num = 0) => Math.asinh(num); module.exports.atan = (num = 0) => Math.atan(num); module.exports.atan2 = (x = 0, y= 0) => Math.atan2(x, y); module.exports.atanh = (num = 0) => Math.atanh(num); module.exports.ceil = (num = 0) => { return Math.ceil(num); }; module.exports.factorial = (n = 0) => fact(n); module.exports.comb = (n = 0, r = 0) => { let first = fact(n); let second = fact(r) * fact(n - r); let answer = first / second; return answer; }; function fact(n) { if (n < 0) return; if (n < 2) return 1; return n * fact(n - 1); } module.exports.cbrt = (num = 0) => Math.cbrt(num); module.exports.copysign = (x = 0, y = 8) => { if (Math.sign(x) === -1) { return Math.sign(y) * (x * -1); } return Math.sign(y) * x; }; module.exports.cos = (x = 0) => Math.cos(x); module.exports.cosh = (x = 0) => Math.cosh(x); module.exports.degrees = (rad = 0) => (rad * 180) / Math.PI; module.exports.dist = ([x1, y1] = [0, 0], [x2, y2] = [0, 0]) => Math.hypot(x2 - x1, y2 - y1); module.exports.exp = (x = 1) => Math.exp(x); module.exports.expm1 = (x = 1) => Math.expm1(x); module.exports.floor = (x = 1) => Math.floor(x); module.exports.fmod = (x = 1, y = 1) => x % y; module.exports.gcd = (a = 0, b = 0) => { let run = true; let first = a; let second = b; let third; while (run) { if (second === 0) { return first; } else { third = first; // a first = second; // b second = third % first; } } }; module.exports.hypot = (...x) => Math.hypot(...x); module.exports.sin = (x = 0) => Math.sin(x); module.exports.sinh = (x = 0) => Math.sinh(x); module.exports.tan = (x = 0) => Math.tan(x); module.exports.tanh = (x = 0) => Math.tanh(x); module.exports.e = Math.E; module.exports.pi = Math.PI; module.exports.pow = (x = 0, y = 0) => Math.pow(x, y); module.exports.log = (x = 0) => Math.log(x); module.exports.log2 = (x = 0) => Math.log2(x); module.exports.log10 = (x = 0) => Math.log10(x); module.exports.log1p = (x = 0) => Math.log1p(x); module.exports.prod = (x = 0) => parseInt(x); module.exports.isfinite = (x = 0) => isFinite(x); module.exports.isinf = (x = 0) => !isFinite(x); module.exports.inf = Number.POSITIVE_INFINITY; module.exports.nan = Number.NaN; module.exports.tau = 2 * Math.PI; module.exports.isqrt = (x = 0) => { if (x <= 0) { return "Error: Number should be greater than or equal to 0"; } if (x === 0) { return 0; } else { while (Math.sqrt(x) % 1 !== 0) { x--; } return x; } }; module.exports.randians = (deg = 0) => (deg * Math.PI) / 180; module.exports.isnan = (x = 0) => isNaN(x); module.exports.prod = (list = [0, 0, 0, 0]) => { let sum = 0; for (let i = 0; i < list.length; i++) { if (i === 0) { sum = list[i]; } else { sum = sum * list[i]; } } return sum; }; module.exports.perm = (n = 0, k = 0) => { let first = fact(n); let second = fact(n - k); return first / second; };