The extensions for the Javascript Number object, available in the file Number-extensions.js, consist of 44 new functions attached to the Number object using Number.prototype. These functions are available to all instances of the Number object, including number literals. For example, the following three lines produce the same result:
alert(Math.pow(2, 16));
var two = 2; alert(two.pow(16));
alert((2).pow(16));
As the code above demonstrates, some of the functions are already available through the Math object. Making these functions directly available through the Number object leads to shorter code, especially when multiple functions are chained together, as the following two equivalent lines demonstrate:
alert(Math.sin(Math.cos(Math.sqrt(Math.PI))));
alert(Math.PI.sqrt().cos().sin());
The Number object extensions
- x.abs()
- returns the absolute value of x
- x.acos()
- returns the inverse cosine of x
- x.acosec()
- returns the inverse cosecant of x
- x.acosech()
- returns the inverse hyperbolic cosecant of x
- x.acosh()
- returns the inverse hyperbolic cosine of x
- x.acot()
- returns the inverse cotangent of x
- x.acoth()
- returns the inverse hyperbolic cotangent of x
- x.asec()
- returns the inverse secant of x
- x.asech()
- returns the inverse hyperbolic secant of x
- x.asin()
- returns the inverse sine of x
- x.asinh()
- returns the inverse hyperbolic sine of x
- x.atan()
- returns the inverse tangent of x
- x.atanh()
- returns the inverse hyperbolic tangent of x
- x.ceil()
- returns the ceiling of x — the smallest integer greater than x
- x.cos()
- returns the cosine of x
- x.cosec()
- returns the cosecant of x
- x.cosech()
- returns the hyperbolic cosecant of x
- x.cosh()
- returns the hyperbolic cosine of x
- x.cot()
- returns the contangent of x
- x.coth()
- returns the hyperbolic cotangent of x
- x.degToRad()
- returns the number of radians equivalent to x degrees
- x.exp()
- returns e raised to the power of x
- x.floor()
- returns the floor of x — the largest integer less than x
- x.isInRange(low, high)
- returns true if and only if x is between low and high inclusive
- x.limit(low, high)
- returns x if x is between low and high inclusive, low if x is less than low, and high if x is greater than high
- x.limitAbove(high)
- returns x if x is less than high, and high otherwise
- x.limitBelow(low)
- returns x if x is greater than low, and low otherwise
- x.ln()
- returns the natural logarithm (logarithm to the base e) of x
- x.log(base)
- returns the logarithm to the base ‘base’ of x
- x.log2()
- returns the logarithm to the base 2 of x — this is quicker than x.log(2)
- x.log10()
- returns the logarithm to the base 10 of x — this is quicker than x.log(10)
- x.mod(modulus)
- returns the positive modulus of x modulus ‘modulus’ — this differs from the modulus operator which may return a negative value
- x.radToDeg()
- returns the number of degrees equivalent to x radians
- x.round()
- returns x rounded to the nearest integer — halves round up (that is, towards positive infinity)
- x.sec()
- returns the secant of x
- x.sech()
- returns the hyperbolic secant of x
- x.sign()
- returns the 0 if x is 0, -1 if x is negative, and 1 if x is positive
- x.sin()
- returns the sine of x
- x.sinh()
- returns the hyperbolic sine of x
- x.sqrt()
- returns the square root of x
- x.pow(exponent)
- returns x raised to power of exponent
- x.tan()
- returns the tangent of x
- x.tanh()
- returns the hyperbolic tangent of x
- x.wrap(low, high)
- returns the value of x ‘wrapped’ between low and high so that the value returned is greater than or equal to low and less than high — if low is 0 then this is equivalent to x.mod(high)