|
eZ Publish
[4.0]
|
00001 <?php 00002 // 00003 // Definition of eZPHPMath class 00004 // 00005 // Created on: <04-Nov-2005 12:26:52 dl> 00006 // 00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00008 // SOFTWARE NAME: eZ Publish 00009 // SOFTWARE RELEASE: 4.0.x 00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS 00011 // SOFTWARE LICENSE: GNU General Public License v2.0 00012 // NOTICE: > 00013 // This program is free software; you can redistribute it and/or 00014 // modify it under the terms of version 2.0 of the GNU General 00015 // Public License as published by the Free Software Foundation. 00016 // 00017 // This program is distributed in the hope that it will be useful, 00018 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 // GNU General Public License for more details. 00021 // 00022 // You should have received a copy of version 2.0 of the GNU General 00023 // Public License along with this program; if not, write to the Free 00024 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 00025 // MA 02110-1301, USA. 00026 // 00027 // 00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00029 // 00030 00031 /*! \file ezphpmath.php 00032 */ 00033 00034 /*! 00035 \class eZPHPMath ezphpmath.php 00036 \brief Handles calculation using standard php's functions. 00037 */ 00038 00039 class eZPHPMath 00040 { 00041 function eZPHPMath( $params = array() ) 00042 { 00043 } 00044 00045 static function create( $type, $params = array() ) 00046 { 00047 $filename = 'lib/ezmath/classes/mathhandlers/' . $type . '.php'; 00048 00049 if ( file_exists( $filename ) ) 00050 { 00051 include_once( $filename ); 00052 return new $type( $params ); 00053 } 00054 00055 return false; 00056 } 00057 00058 function add( $a, $b ) 00059 { 00060 return ( (float)$a + (float)$b ); 00061 } 00062 00063 function sub( $a, $b ) 00064 { 00065 return ( (float)$a - (float)$b ); 00066 } 00067 00068 function mul( $a, $b ) 00069 { 00070 $c = ( (float)$a * (float)$b ); 00071 return ( (float)$a * (float)$b ); 00072 } 00073 00074 function div( $a, $b ) 00075 { 00076 return ( (float)$a / (float)$b ); 00077 } 00078 00079 function pow( $base, $exp ) 00080 { 00081 return ( pow( $base, $exp ) ); 00082 } 00083 00084 function round( $value, $precision, $target ) 00085 { 00086 $result = round( $value, $precision ); 00087 $result = $this->adjustFractPart( $result, $precision, $target ); 00088 return $result; 00089 } 00090 00091 function ceil( $value, $precision, $target ) 00092 { 00093 $fractStr = $this->fractval( $value ); 00094 $fractPart = (int)substr( $fractStr, 0, $precision ); 00095 00096 $fractLen = strlen( $fractStr ); 00097 // actual ceiling 00098 if ( $fractLen > $precision ) 00099 $fractPart += 1; 00100 00101 // adjust precision 00102 if ( $fractLen < $precision ) 00103 $precision = $fractLen; 00104 00105 // create resulting value 00106 $fractPart = $this->div( $fractPart, $this->pow( 10, $precision ) ); 00107 00108 $result = $this->add( $this->intval( $value ), $fractPart ); 00109 $result = $this->adjustFractPart( $result, $precision, $target ); 00110 00111 return $result; 00112 } 00113 00114 function floor( $value, $precision, $target ) 00115 { 00116 $fractPart = $this->fractval( $value, $precision ); 00117 00118 // adjust precision 00119 $fractLen = strlen( $fractPart ); 00120 if ( $fractLen < $precision ) 00121 $precision = $fractLen; 00122 00123 $fractPart = $this->div( $fractPart, $this->pow( 10, $precision ) ); 00124 00125 $result = $this->add( $this->intval( $value ), $fractPart ); 00126 $result = $this->adjustFractPart( $result, $precision, $target ); 00127 00128 return $result; 00129 } 00130 00131 00132 function adjustFractPart( $number, $precision, $target ) 00133 { 00134 if ( is_numeric( $target ) ) 00135 { 00136 $target = substr( $target, 0, $precision ); 00137 $targetPrecision = strlen( $target ); 00138 $target = $this->div( $target, $this->pow( 10, $precision ) ); 00139 00140 $intPart = $this->intval( $number ); 00141 $fractPart = $this->fractval( $number, $precision - $targetPrecision ); 00142 $fractPart = $this->div( $fractPart, $this->pow( 10, $this->sub( $precision, $targetPrecision ) ) ); 00143 $fractPart = $this->add( $fractPart, $target ); 00144 00145 $number = $this->add( $intPart, $fractPart ); 00146 } 00147 00148 return $number; 00149 } 00150 00151 function intval( $number ) 00152 { 00153 $intPart = 0; 00154 $pos = strpos( $number, '.' ); 00155 if ( $pos !== false ) 00156 $intPart = substr( $number, 0, $pos ); 00157 else 00158 $intPart = $number; 00159 00160 return $intPart; 00161 } 00162 00163 function fractval( $number, $precision = false ) 00164 { 00165 $fractPart = 0; 00166 $pos = strpos( $number, '.' ); 00167 if ( $pos !== false ) 00168 { 00169 if ( $precision === false ) 00170 $fractPart = substr( $number, $pos + 1 ); 00171 else 00172 $fractPart = substr( $number, $pos + 1, $precision ); 00173 } 00174 00175 return $fractPart; 00176 } 00177 } 00178 00179 ?>