{{ message }}

Instantly share code, notes, and snippets.

# /math.js

Created Feb 15, 2015
Purebasic.Math to Javascript port
 /** * @description Returns the absolute value of the given float value. * @param a * @returns {number} */ function pbAbs(a) { return Math.abs(a) } /** * @description Returns the area hyperbolic cosine of the specified value. * @param a * @returns {number} */ function pbACosH(a) { return Math.log(a + Math.sqrt(a * a - 1)) } /** * @description Returns the arc-sine of the specified value. * @param a * @returns {number} */ function pbASin(a) { return Math.asin(a) } /** * @description Returns the area hyperbolic sine of the specified value. * @param a * @returns {number} */ function pbASinH(a) { return Math.log(a + Math.sqrt(a * a + 1)) } /** * @description Returns the arc-tangent of the specified value. * @param a * @returns {number} */ function pbATan(a) { return Math.atan(a) } /** * @description Calculates the angle in radian between the x axis and a line drawn in the direction specified by 'x' and 'y'. It can be used to calculate angles between lines in 2D or to transform rectangular coordinates into polar coordinates. * @param x * @param y * @returns {number} */ function pbATan2(x, y) { return Math.atan2(x, y) } /** * @description Returns the area hyperbolic tangent of the specified value. * @param a * @returns {number} */ function pbATanH(a) { return 0.5 * Math.log((1 + a) / (1 - a)) } /** * @description Returns the hyperbolic cosine of the specified hyperbolic angle. * @param Angle * @returns {number} */ function pbCosH(Angle) { return (Math.exp(Angle) + Math.exp(-Angle)) / 2 } /** * @description Converts the given angle from radian to degree. * @param Angle * @returns {number} */ function pbDegree(Angle) { return 180 / Math.PI * Angle } /** * @description Converts the given angle from degrees into radian. * @param Angle * @returns {number} */ function pbRadian(Angle) { return Math.PI / 180 * Angle } /** * @description Returns the natural Log (ie log to the base e) of the given number. * @param a * @returns {number} */ function pbLog(a) { return Math.log(a) } /** * @description Returns the log in base 10 of the given number. * @param a * @returns {number} */ function pbLog10(a) { return Math.log(a) / 2.302585092994046 } /** * @description Returns the result of the exponential function. This is the value e raised to the power 'Number'. * @param Number * @returns {number} */ function pbExp(Number) { return Math.exp(Number) } /** * @description Returns the remainder of the division of Number by Divisor. * @param Number * @param Divisor * @returns {Function} */ function pbMod(Number, Divisor) { var b, d = 0, e = b = 0, f = 0; b = Number.toExponential().match(/^..?(.*)e(.+)\$/); d = parseInt(b[2], 10) - (b[1] + "").length; b = Divisor.toExponential().match(/^..?(.*)e(.+)\$/); b = parseInt(b[2], 10) - (b[1] + "").length; b > d && (d = b); b = Number % Divisor; return (-100 > d || 20 < d) ? (e = Math.round(Math.log(b) / Math.log(10)), f = Math.pow(10, e), (b / f).toFixed(e - d) * f) : parseFloat(b.toFixed(-d)); } /** * @description Returns the special floating-point value representing positive infinity. Negative infinity can be calculated using "-Infinity()". * @returns {Number} */ function pbInfinity() { return Number.POSITIVE_INFINITY } /** * @description Returns the special floating-point value representing 'Not a Number'. This value is returned from invalid calculations such as calculating the square root of a negative number. * @returns {Number} */ function pbNaN() { return NaN } /** * @description Returns nonzero if the given value represents positive or negative infinity. * @param a * @returns {number} */ function pbIsInfinity(a) { return a == Number.POSITIVE_INFINITY || a == Number.NEGATIVE_INFINITY ? 1 : 0 } /** * @description Returns the square root of the specified number. * @param a * @returns {number} */ function pbSqr(a) { return Math.sqrt(a) } /** * @description Returns the cosine of the specified angle. * @param a * @returns {number} */ function pbCos(a) { return Math.cos(a) } /** * @description Round the specified float number according to the given mode. * @param Number * @param Mode (Down = 0 / Up = 1 / Nearest = 2) * @returns {number} */ function pbRound(Number, Mode) { switch (Mode) { case 1: return Math.ceil(Number); case 2: return 0 < Number ? Math.floor(Number + 0.5) : Math.ceil(Number - 0.5); default: return Math.floor(Number) } } /** * @description Returns a floating-point value representing the sign of the given number. * @param a * @returns {number} */ function pbSign(a) { return a ? 0 > a ? -1 : 1 : 0 } /** * @description Returns the sine of the specified angle. * @param a * @returns {number} */ function pbSin(a) { return Math.sin(a) } /** * @description Returns the hyperbolic sine of the specified hyperbolic angle. * @param Angle * @returns {number} */ function pbSinH(Angle) { return (Math.exp(Angle) - Math.exp(-Angle)) / 2 } /** * @description Returns the tangent of the specified angle. * @param Angle * @returns {number} */ function pbTan(Angle) { return Math.tan(Angle) } /** * @description Returns the hyperbolic tangent of the specified hyperbolic angle. * @param Angle * @returns {number} */ function pbTanH(Angle) { return (Math.exp(Angle) - Math.exp(-Angle)) / (Math.exp(Angle) + Math.exp(-Angle)) } /** * @description Returns the given number, raised to the given power. * @param Number * @param Power * @returns {number} */ function pbPow(Number, Power) { return Math.pow(Number, Power) } /** * @description Returns the integer part of a float number. * @param a * @returns {number} */ function pbInt(a) { return a | 0 } /** * @description Returns the integer part of a float number as a quad. * @param a * @returns {number} */ function pbIntQ(a) { return a | 0 } /** * @description Returns nonzero if the given value 'Not a Number'. This value is the result of some invalid calculations. It can also be generated using the NaN() function. * @param a * @returns {number} */ function pbIsNAN(a) { return isNaN(a) ? 1 : 0 } /** * @description Returns a random number from zero to the given maximum value (both values included). * @param Maximum * @param Minimum * @returns {*} */ function pbRandom(Maximum, Minimum) { "undefined" === typeof Minimum && (Minimum = 0); return Minimum + (Math.random() * (Maximum - Minimum + 1) | 0) } /** * @description Logical XOR. Can be used to combine the logical true ot false results of the comparison operators to give a result shown in the following table. This operator cannot be used with strings. * @param a * @param b * @returns {*|boolean} */ function pbXOr(a, b) { return (a || b) && !(a && b) }