DocMGR PHP Cross Reference Groupware Applications

Source: /lib/calc.php - 151 lines - 3654 bytes - Summary - Text - Print

   1  <?php
   2  /*********************************************************
   3  //        FILE: calc.inc.php
   4  // DESCRIPTION: Contains functions that have that are for
   5  //              some sort of mathematical calculations
   6  //              or binary manipulation.
   7  //
   8  //    CREATION
   9  //        DATE: 04-19-2006
  10  //
  11  //     HISTORY:
  12  //
  13  *********************************************************/
  14  /***************************************************************
  15    FUNCTION: bitset_compare
  16    PURPOSE:    for performing bitwise comparisons for permissions
  17              checks
  18    INPUT:        bit1 -> user's permission setting
  19              bit2 -> permission required for function
  20              admin -> also check if user is an admin
  21    RETURNS:    return true if bit2 is in bit1 or admin is in bit1
  22              return false if not
  23  ***************************************************************/
  24  function bitset_compare($bit1,$bit2,$admin=null) {
  25  
  26    return PERM::check($bit2);
  27  
  28  }
  29  
  30  function bit_comp($bit1,$bit2)
  31  {
  32  
  33      $auth = null;
  34  
  35      if ( (int)$bit1 & (int)$bit2 ) $auth = 1;
  36  
  37      if (!$auth) return false;
  38      else return true;
  39  
  40  
  41  }
  42  
  43  
  44  /*********************************************************
  45  *********************************************************/
  46  function bitCal($limit) {
  47  
  48      $num = 1;
  49  
  50      for ($row=0;$row<$limit;$row++) {
  51          if ($limit!=0) $num = $num * 2;
  52      }
  53  
  54      return $num;
  55  }
  56  /*********************************************************
  57  *********************************************************/
  58  function revBitCal($limit) {
  59  
  60      $counter = "0";
  61  
  62      while ($limit!="1") {
  63  
  64          $counter++;
  65          $limit = $limit/2;
  66      }
  67  
  68      return $counter;
  69  
  70  }
  71  /*********************************************************
  72    FUNCTION:    intPercent
  73    PURPOSE:    returns a percentage rounded to the nearest
  74              integer
  75    INPUT:        num -> the number we are checking
  76              total -> number we are dividing against
  77    RETURNS:    integer
  78  *********************************************************/
  79  function intPercent($num,$total) {
  80  
  81      if ($total != 0) {
  82  
  83          $percent = ($num/$total) * 100;
  84          $temp = intval($percent) + ".5";
  85  
  86          //round up if necessary
  87          if ($percent >= $temp) {
  88              $value = intval($percent) + 1;
  89          }
  90          else {
  91              $value = intval($percent);
  92          }
  93  
  94      }
  95      else $value = "0";
  96  
  97      return $value;
  98  
  99  }
 100  /*********************************************************
 101    FUNCTION:    intvalue
 102    PURPOSE:    converts float to integer, but rounds up
 103              at .5 instead of down like intval does
 104    INPUTS:        num -> float we are checking
 105    RETURNS:    integer
 106  *********************************************************/
 107  function intValue($num) {
 108  
 109      $temp = intval($num) + ".5";
 110  
 111      //round up if necessary
 112      if ($num >= $temp) {
 113          $value = intval($num) + 1;
 114      }
 115      else {
 116          $value = intval($num);
 117      }
 118  
 119      return $value;
 120  
 121  }
 122  /*********************************************************
 123    FUNCTION:    floatValue
 124    PURPOSE:    rounds float to nearest desired decimal place
 125              rounds up at .5 instead of down like floatval does
 126    INPUTS:        num -> float we are checking
 127              count -> how many decimal places to round to
 128    RETURNS:    float
 129  *********************************************************/
 130  function floatValue($num,$count) {
 131  
 132    $pos = strpos($num,".") + 1 + $count;
 133  
 134    $floatNum = substr($num,0,$pos);
 135    $checkDigit = substr($num,strlen($floatNum),1);
 136  
 137    $changeDigit = substr($floatNum,strlen($floatNum)-1,1);
 138  
 139    //round up if necessary
 140    if ($checkDigit>=5) $last = $changeDigit + 1;
 141    else $last = $changeDigit;
 142  
 143    $value = substr($floatNum,0,strlen($floatNum)-1).$last;
 144  
 145    return $value;
 146  
 147  }
 148  
 149  
 150  
 151  

title

Description

title

Description

title

Description

title

title

Body