Source: math/WKcMath.js

/*jslint plusplus: true, white: true, indent: 2, maxlen: 90 */
/*global $wk$ */

/**
 * @author    Wolfgang Kowarschick
 * @copyright 2011-2013, Wolfgang Kowarschick
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted under the terms of the 
 * Creative Commons License Attribution-NonCommercial-ShareAlike 3.0 Unported    
 * (CC BY-NC-SA 3.0: http://creativecommons.org/licenses/by-nc-sa/3.0/). 
 */

////////////////////////////////////////////////////////////////////////////////
// Class "$wk$.math.WKcMath"
////////////////////////////////////////////////////////////////////////////////

$wk$("$wk$.WKcClass", 
function($)
{ "use strict";
  //console.log("WKcMath", $.$trace$);

  /** 
   *  The class <code>WKcMath</code> contains sone useful static methods 
   *  and constants.
   * 
   *  @class
   *  @name $wk$.math.WKcMath
   *  @property {Number} EPSILON - Two <code>Number</code>s are supposed to 
   *                               be equal, if their absolute difference is 
   *                               less than or equal to 
   *                               <code>WKcMath.EPSILON</code>.
   *  @property {Number} PI
   *  @property {Number} PI_2 - <code>2*WKcMath.PI</code>
   *  @returns {Object} A new <code>WKcMath</code> object, which, however
   *                   is not needed (as there are only static members).
   */
  var WKcMath =
  new $.WKcClass
  ({fullName: "$wk$.math.WKcMath",
    
    staticMethods: 
    { EPSILON: 1.0e-15,   //"@EPSILON": "CONSTANT",
      PI:      Math.PI,   //"@PI":      "CONSTANT",
      PI_2:    Math.PI*2, //"@PI_2":    "CONSTANT",
      
      /**
       *  @method
       *  @name $wk$.math.WKcMath.random
       *  
       *  @param   {Number|Object|Array} p_interval
       *           A single number, an object with two properties 
       *           <code>min</code> and <code>max</code> whose values are 
       *           numbers, or an array containing several intervals,.
       *  @returns {Number} 
       *           A random number whithin the range of the closed interval
       *           defined by <code>p_interval</code>.
       */ 
      random:
        function(p_interval)
        { if (p_interval instanceof Array)
          { p_interval =
              p_interval[Math.floor(Math.random()*p_interval.length)]; 
          }
          if (p_interval === null)
          { return 0; }
          if (p_interval instanceof Number)
          { return p_interval; }
          
          if (p_interval instanceof Object)
          { var l_min = p_interval.min, l_max = p_interval.max;
            return (l_min === l_max)
                     ? l_min
                     : l_min+Math.random()*(l_max-l_min)
                   ;
          }
          throw new Error( p_interval + " is no interval");    
        },
      
      /**
       *  @method
       *  @name $wk$.math.WKcMath.randomInt
       *  
       *  @param   {number|Object|Array} p_interval
       *           A single number, an object with two properties 
       *           <code>min</code> and <code>max</code> whose values are 
       *           numbers, or an array containing several intervals,.
       *  @returns {int} 
        *          A random integer whithin the range of the closed interval
       *           defined by <code>p_interval</code>.
       */  
      randomInt:
        function(p_interval)
        { if (p_interval instanceof Array)
          { p_interval =
              p_interval[Math.floor(Math.random()*p_interval.length)]; 
          }
          if (p_interval === null)
          { return 0; }
          if (p_interval instanceof Number)
          { return Math.floor(p_interval); }
          if (p_interval instanceof Object)
          { var l_min = p_interval.min, l_max = p_interval.max;
            return (l_min === l_max)
                     ? Math.floor(l_min)
                     : l_min+Math.floor(Math.random()*(l_max-l_min+1))
                   ;
          }
          throw new Error( p_interval + " is no interval");    
        },
      
      /**
       *  @method
       *  @name $wk$.math.WKcMath.signum
       *  
       *  @param {Number} p_number
       *  @returns {Number} The signum of <code>p_number<code>.
       */
      signum:
        function(p_number) { return p_number?p_number<0?-1:1:0; },
    },
  });

  // can be removed, if constants are supported by WKcClass.
  Object.defineProperty(WKcMath, "EPSILON", { writable : false });
  Object.defineProperty(WKcMath, "PI",      { writable : false });
  Object.defineProperty(WKcMath, "PI_2",    { writable : false });
  
  $.moduleAdd({ name:   "$wk$.math.WKcMath",
                module: { WKcMath: WKcMath }
             });
});

////////////////////////////////////////////////////////////////////////////////
// End of class "$wk$.math.WKcMath"
////////////////////////////////////////////////////////////////////////////////
W. Kowarschick © 2013 (CC BY-NC-SA 3.0: http://creativecommons.org/licenses/by-nc-sa/3.0/)
Documentation generated by JSDoc 3.3.0-dev on Sat Oct 05 2013 15:14:39 GMT+0200 (MESZ) using the DocStrap template.