core
[ class tree: core ] [ index: core ] [ all elements ]

Source for file Numbers.class.php

Documentation is available at Numbers.class.php

  1. <?php
  2. /**
  3.  * @package core
  4.  */
  5.  
  6. /**
  7.  * @var array Contains all hexadecimal numbers.
  8.  */
  9. $HEXARR array(
  10.                   "0""1""2""3",
  11.                   "4""5""6""7",
  12.                   "8""9""A""B",
  13.                   "C""D""E""F"
  14.                );
  15.  
  16.  
  17. /**
  18.  *
  19.  */
  20. require_once CLASSPATH."data_structures/ABSTObject.class.php";
  21.  
  22. /**
  23.  * Static methods to handle numeric values.
  24.  *
  25.  * @static
  26.  * @package   core
  27.  * @version   0.1.96
  28.  *
  29.  * @author    Daniel Plücken <daniel@debakel.net>
  30.  *
  31.  * @license   http://www.gnu.org/copyleft/lesser.html
  32.  *             GNU Lesser General Public License
  33.  * @copyright Copyright (c) 2003 Daniel Plücken <daniel@debakel.net>
  34.  *
  35.  *  This library is free software; you can redistribute it and/or
  36.  *  modify it under the terms of the GNU Lesser General Public
  37.  *  License as published by the Free Software Foundation; either
  38.  *  version 2.1 of the License.
  39.  *
  40.  *  This library is distributed in the hope that it will be useful,
  41.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  42.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  43.  *  GNU Lesser General Public License for more details.
  44.  *
  45.  *  You should have received a copy of the GNU Lesser General
  46.  *  Public License along with this library; if not, write to the
  47.  *  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  48.  *  Boston, MA 02111-1307 USA
  49.  */
  50. class Numbers extends ABSTObject
  51. {
  52.  
  53.   // Constructor
  54.   function Numbers()
  55.   {}
  56.  
  57.  
  58.  
  59.   /**
  60.    * Apportions a range of numbers between zero and $rangeEnd in $portions
  61.    * commensurate chunks. Then the closest computed value to $value will
  62.    * be returned.
  63.    *
  64.    * Example:
  65.    * Numbers::getIntervalRoundingOf( 42, 60, 4 );
  66.    * Is the same like:
  67.    *
  68.    * <code>
  69.    * if( $value <= 7 || $value > 52 )
  70.    *   return = 0;
  71.    * else
  72.    * if( $value <= 22 && $value > 7 )
  73.    *   return = 15;
  74.    * else
  75.    * if( $value <= 37 && $value > 22 )
  76.    *   return = 30;
  77.    * else
  78.    * if( $value <= 52 && $value > 37 )
  79.    *   return = 45;
  80.    * </code>
  81.    *
  82.    * In the upper example the function will return 45.
  83.    *
  84.    * @version 1.0
  85.    * @since   0.1.1
  86.    * @author  Daniel Plücken <daniel@debakel.net>
  87.    * @access  public
  88.    * @static
  89.    * @param   number $value    The value to round to the nearest chunk of the
  90.    *                            range.
  91.    * @param   number $rangeEnd The end of the range. The range begins at zero.
  92.    *                            For example if you want to round in the range of
  93.    *                            6 to 9, then set the $rangeEnd to 3 and add 6
  94.    *                            after the rounding.
  95.    * @param   number $portions How many chunks should create in the range for
  96.    *                            rounding base.
  97.    * @return  number 
  98.    */
  99.   function getIntervalRoundingOf$value$rangeEnd$portions )
  100.   {
  101.     $intervalStepAmount     $rangeEnd $portions;
  102.     $halfIntervalStepAmount $intervalStepAmount 2;
  103.     $fa floor($halfIntervalStepAmount);
  104.     $ca ceil($halfIntervalStepAmount);
  105.  
  106.     if$ca == $fa )
  107.     # Rounding like a merchant (e.g. rounding up if the value is 0.5
  108.       #                                in a range of 0 and 1)
  109.       $ca++;
  110.       $fa--;
  111.     }
  112.  
  113.     if $value <= $fa || $value ($rangeEnd $ca) )
  114.        return 0;
  115.     else
  116.        for $i 1$i $portions$i++)
  117.            if (
  118.                 $value <= ( ($i $intervalStepAmount$fa )
  119.              && $value >  ( ($i $intervalStepAmount$ca )
  120.               )
  121.               return ($i $intervalStepAmount);
  122.   }
  123.  
  124.  
  125.  
  126.  
  127.   /**
  128.    * Returns a random number in the range between $including_from and
  129.    * $including_to. Sets automatically a new random set by microtime for real
  130.    * random.
  131.    *
  132.    * @version 1.0
  133.    * @since   0.1.5
  134.    * @author  Daniel Plücken <daniel@debakel.net>
  135.    * @access  public
  136.    * @static
  137.    * @param   integer $including_from 
  138.    * @param   integer $including_to 
  139.    * @return  number 
  140.    */
  141.   function random$including_from$including_to )
  142.   {
  143.      srand(double)microtime(1000000 );
  144.      return rand$including_from$including_to );
  145.   }
  146.  
  147.  
  148.  
  149.  
  150.   /**
  151.    * Returns a double from a given string. Translates a number from other
  152.    * language formates.
  153.    *
  154.    * @version 1.0
  155.    * @since   0.1.95
  156.    * @author  Daniel Plücken <daniel@debakel.net>
  157.    * @access  public
  158.    * @static
  159.    * @param   string $str_number 
  160.    * @param   string $str_decimal_separator 
  161.    * @param   string $str_thousand_separator 
  162.    * @return  double 
  163.    */
  164.   function translateDoubleVal(
  165.                                $str_number,
  166.                                $str_decimal_separator ".",
  167.                                $str_thousand_separator ","
  168.                              )
  169.   {
  170.       $str_out str_replace$str_thousand_separator""$str_number );
  171.       $str_out str_replace$str_decimal_separator"."$str_out );
  172.  
  173.       return doubleval$str_out );
  174.   }
  175.  
  176.  
  177.  
  178.   /**
  179.    * Returns the decimal value of a given hexadecimal value.
  180.    *
  181.    * @version 1.0
  182.    * @since   1.6
  183.    * @author  Daniel Plücken <daniel@debakel.net>
  184.    * @access  public
  185.    * @static
  186.    * @param   string  $string 
  187.    * @return  integer 
  188.    */
  189.   function hex2int$string )
  190.   {
  191.      $out    0;
  192.      $string strtoupper$string );
  193.  
  194.      for$i 0$i strlen$string )$i++ )
  195.      {
  196.        switchsubstr$string$i) )
  197.        {
  198.          case '0':
  199.                    $tmp 0;
  200.                    break;
  201.          case '1':
  202.                    $tmp 1;
  203.                    break;
  204.          case '2':
  205.                    $tmp 2;
  206.                    break;
  207.          case '3':
  208.                    $tmp 3;
  209.                    break;
  210.          case '4':
  211.                    $tmp 4;
  212.                    break;
  213.          case '5':
  214.                    $tmp 5;
  215.                    break;
  216.          case '6':
  217.                    $tmp 6;
  218.                    break;
  219.          case '7':
  220.                    $tmp 7;
  221.                    break;
  222.          case '8':
  223.                    $tmp 8;
  224.                    break;
  225.          case '9':
  226.                    $tmp 9;
  227.                    break;
  228.          case 'A':
  229.                    $tmp 10;
  230.                    break;
  231.          case 'B':
  232.                    $tmp 11;
  233.                    break;
  234.          case 'C':
  235.                    $tmp 12;
  236.                    break;
  237.          case 'D':
  238.                    $tmp 13;
  239.                    break;
  240.          case 'E':
  241.                    $tmp 14;
  242.                    break;
  243.          case 'F':
  244.                    $tmp 15;
  245.                    break;
  246.          default:
  247.                   $tmp = -1;
  248.                   break;
  249.        }
  250.  
  251.        if$tmp == -)
  252.          return -1;
  253.  
  254.        $out += $tmp pow16(strlen$string )-1$i );
  255.      }
  256.  
  257.      return $out;
  258.   }
  259. // End of class Numbers
  260. ?>

Documentation generated on Thu, 05 Jun 2008 19:14:35 +0200 by phpDocumentor 1.4.1