« Buy the Book | Main | Happy New Year! »

Math Object Extensions

I've compiled a selection of extensions for the Math object which includes 4 new constants and 50 new functions to compliment the core Math object constants and methods. I may add to these in future, so note the version number for updates.

To use, copy MathExtensions1.1.as to:
C:\Program Files\Macromedia\Flash MX\Configuration\Include (on PC),
then include the .as file in future scripts without the need for a path:

#include "MathExtensions1.1.as"

At the bottom of the file you'll find a synopsis of the Math object constants and methods, and the Math object extensions of constants and functions, including method/function arguments, displaying an overview for quick application in your scripts. I'll include the list here for your perusal:

////////////////////////////////// Math Object

Constants

1 - E - natural log - Euler's number - 2.71828182845905
2 - LN2 - natural log of 2 - 0.69314718055994528623
3 - LN10 - natural log of 10 - 2.3025850929940459011
4 - LOG2E - base 2 of natural log (E) - 1.442695040888963387
5 - LOG10E - base 10 of natural log (E) - 0.43429448190325181667
6 - PI - ratio of circumference to diameter of a circle - 3.14159265358979
7 - SQRT1_2 - squareroot of 1/2 - 0.707106781186
8 - SQRT2 - squareroot of 2 - 1.414213562373

Methods

1 - abs(x) - absolute number
2 - acos(x) - arccosine of "x"
3 - asin(x) - arcsine of "x"
4 - atan(x) - arctangent (0-90)
5 - atan2(y,x) - arctangent (0-180)
6 - ceil(x) - ceiling - smallest integer above "x"
7 - cos(x) - cosine of "x"
8 - exp(x) - exponential - "E" to the power of "x"
9 - floor(x) - floor - greatest integer below "x"
10 - log(x) - natural log of "x"
11 - max(x,y) - compare "x" and "y" for maximum
12 - min(x,y) - compare "x" and "y" for minimum
13 - pow(x,y) - "x" to the power of "y"
14 - random() - random floating point number between 0-1
15 - round(x) - round floating point number to integer based on .5 split
16 - sin(x) - sine of "x"
17 - sqrt(x) - squareroot of "x"
18 - tan(x) - tangent of "x"

////////////////////////////////// Math Object Extensions

Constants

1 - DEG2RAD - convert degrees to radians - 0.0174532925199433
2 - RAD2DEG - convert radians to degrees - 57.2957795130823
3 - PHI - the golden mean - 1.61803398874989
4 - LAMBDA - Euler-Mascheroni constant - .5772156649

Functions

1 - sec(angle) - secant
2 - asec(ratio) - arcsecant
3 - csc(angle) - cosecant
4 - acsc(ratio) - arccoseceant
5 - cot(angle) - cotangent
6 - acot(ratio) - arccotangent
7 - sinh(n) - hyperbolic sine
8 - asinh(n) - hyperbolic arcsine
9 - cosh(n) - hyperbolic cosine
10 - acosh(n) - hyperbolic arccosine
11 - tanh(n) - hyperbolic tangent
12 - atanh(n) - hyperbolic arctangent
13 - sech(n) - hyperbolic secant
14 - asech(n) - hyperbolic arcsecant
15 - csch(n) - hyperbolic cosecant
16 - acsch(n) - hyperbolic arccosecant
17 - coth(n) - hyperbolic cotangent
18 - acoth(n) - hyperbolic arccotangent
19 - ln(n) - natural logarithm of parameter "n"
20 - log_a(a,n) - logarithm base "a" of "n"
21 - sign(n) - sign of a number
22 - placesRound(a,b) - rounds "a" to "b" decimal places
23 - randomBetween(a,b) - random number between "a" and "b"
24 - summation(n,x) - sum of all numbers between 1 and "n" raised to "x" power
25 - square(n) - number squared
26 - inverse(n) - number inversed
27 - fp(n) - decimal portion of floating point number
28 - pow2(a,n) - solves the negative value input bug
29 - nRoot(a,n) - nth root of a number
30 - factorial(n) - factorial of a positive integer
31 - permutations(n,r) - number of ways to arrange a list - order matters
32 - combinations(n,r) - number of ways to arrange a list - order doesn't matter
33 - isPrime(n) - boolean for prime integer
34 - isEven(n) - boolean for even integer
35 - isOdd(n) - boolean for odd integer
36 - gcd(a,b) - greatest common divisor
37 - lcm(a,b) - lowest common multiple
38 - percentage(a,b) - convert fraction to percentage
39 - mean(arr) - mean of array
40 - variance(arr) - variance of array
41 - sd(arr) - standard deviation of array
42 - distance(x1,y1,x2,y2) - distance between 2 points
43 - angle(x1,y1,x2,y2) - angle of line in degrees
44 - angle2Standard(angle) - Flash angle to standard geometric angle
45 - polygonArea(arr,len) - area for convex and concave polygons
46 - toPoint(vec) - convert vector tail to Cartesian coordinates
47 - toPolar(vec) - convert vector to polar coordinates
48 - round2(num) - rounds negative numbers down
49 - formatDecimals(num,digits) - format number to specified decimals and 0 pad right
50 - toScientific(num,sigDigs) - scientific notation to specified significant digits