Pour mes besoins en calculs mathématiques, certaines fonctions ou optimisations m'étaient nécessaires. C'est pourquoi j'ai crée la classe XMath, qui aurait du a priori étendre la classe Math. Seulement, la classe Math étant composée uniquement de propriétes et de méthodes statiques, l'héritage se faisait mal, et m'obligeait à jongler entre les classes XMath et Math...

Mais c'était sans compter sur le __resolve que j'ai appliqué sur ma classe, me permettant ainsi de ne manipuler qu'une seule classe.

La voici. Evidemment, comme toujours, je suis ouvert à toute proposition d'amélioration! ;)
/**
 * Extended Math class
 * @class XMath
 * @version 0.1
 * @author <a href="http://www.lalex.com/">LAlex</a>
 * @since 2005-06-22
 * @about The class is dynamic to be able to use __resolve...
 */

dynamic class com.lalex.math.XMath {
        // Cosinus and sinus of round values in degree
        private static var __COS:Array = new Array();
        private static var __SIN:Array = new Array();
        // 2*PI
        // Can be used for radians modulos
        private static var __2PI:Number = 2*Math.PI;
       
        /**
        * "Simple" cosinus
        * @param n Round value in degrees
        * @return Cosinus of the degree value, with 2 decimals
        */

        public static function simpleCos(n:Number):Number {
                return __COS[n];
        }
        /**
        * "Simple" sinus
        * @param n Round value in degrees
        * @return Sinus of the degree value, with 2 decimals
        */

        public static function simpleSin(n:Number):Number {
                return __SIN[n];
        }
        /**
        * Return 2*Math.PI value
        */

        public static function get PI2():Number {
                return __2PI;
        }
        /**
        * Initilisation of the XMath static properties
        */

        private static function __init() {
                for (var i:Number = 0 ; i<360 ; i++) {
                        __COS[i] = Math.round(Math.cos(Deg2Rad(i))*100)/100;
                        __SIN[i] = Math.round(Math.sin(Deg2Rad(i))*100)/100;
                }
        }
        /**
        * Launch initialisation
        */

        private static var __initSinCos = __init();
        /**
        * Convert a degree value to radians
        * @param deg Degree value
        * @return Radian value
        */

        public static function Deg2Rad(deg:Number):Number {
                return deg*Math.PI/180;
        }
        /**
        * Convert a radian value to degrees
        * @param rad Radian value
        * @return Degree value
        */

        public static function Rad2Deg(rad:Number):Number {
                return rad*180/Math.PI;
        }
        /**
        * Return a modulo of a value
        * @param a Numeric value
        * @param mod Modulo (optional) Default 360
        * @return value >=0 and <mod
        */

        public static function modulo(a:Number, mod:Number):Number {
                if (!mod) {
                        mod = 360;
                }
                return ((a%mod+mod)%mod);
        }
        /**
        * Return a numeric value with a defined number of decimals
        * @param n Numeric value
        * @param d Number of décimals (default 0)
        * @return "Rounded" value
        */

        public static function roundTo(n:Number, d:Number):Number {
                switch(d) {
                        case 2:
                                return Math.round(n*100)/100;
                        case 1:
                                return Math.round(n*10)/10;
                        case 0:
                        case undefined:
                                return Math.round(n);
                        default:
                                var puis:Number = Math.pow(10, d);
                                return Math.round(n*puis)/puis;
                }
        }
        /**
        * Return the property or method of the Math class
        * if not found in the XMath class
        */

        private static function __resolve(p:String) {
                if (typeof Math[p] == "function")
                        return function() { return Math[p].apply(null, arguments); }
                else
                        return Math[p];
        }       
}
Utilisation :import com.lalex.math.XMath;
trace(XMath.simpleCos(120)); // 0.5
trace(XMath.cos(XMath.PI2)); //0

::Télécharger XMath01.zip::