00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 class eZPHPMath
00040 {
00041 function eZPHPMath( $params = array() )
00042 {
00043 }
00044
00045 function &create( $type, $params = array() )
00046 {
00047 $impl = false;
00048 $filename = 'lib/ezmath/classes/mathhandlers/' . $type . '.php';
00049
00050 if ( file_exists( $filename ) )
00051 {
00052 include_once( $filename );
00053 $impl = new $type( $params );
00054 }
00055
00056 return $impl;
00057 }
00058
00059 function add( $a, $b )
00060 {
00061 return ( (float)$a + (float)$b );
00062 }
00063
00064 function sub( $a, $b )
00065 {
00066 return ( (float)$a - (float)$b );
00067 }
00068
00069 function mul( $a, $b )
00070 {
00071 $c = ( (float)$a * (float)$b );
00072 return ( (float)$a * (float)$b );
00073 }
00074
00075 function div( $a, $b )
00076 {
00077 return ( (float)$a / (float)$b );
00078 }
00079
00080 function pow( $base, $exp )
00081 {
00082 return ( pow( $base, $exp ) );
00083 }
00084
00085 function round( $value, $precision, $target )
00086 {
00087 $result = round( $value, $precision );
00088 $result = $this->adjustFractPart( $result, $precision, $target );
00089 return $result;
00090 }
00091
00092 function ceil( $value, $precision, $target )
00093 {
00094 $fractStr = $this->fractval( $value );
00095 $fractPart = (int)substr( $fractStr, 0, $precision );
00096
00097 $fractLen = strlen( $fractStr );
00098
00099 if ( $fractLen > $precision )
00100 $fractPart += 1;
00101
00102
00103 if ( $fractLen < $precision )
00104 $precision = $fractLen;
00105
00106
00107 $fractPart = $this->div( $fractPart, $this->pow( 10, $precision ) );
00108
00109 $result = $this->add( $this->intval( $value ), $fractPart );
00110 $result = $this->adjustFractPart( $result, $precision, $target );
00111
00112 return $result;
00113 }
00114
00115 function floor( $value, $precision, $target )
00116 {
00117 $fractPart = $this->fractval( $value, $precision );
00118
00119
00120 $fractLen = strlen( $fractPart );
00121 if ( $fractLen < $precision )
00122 $precision = $fractLen;
00123
00124 $fractPart = $this->div( $fractPart, $this->pow( 10, $precision ) );
00125
00126 $result = $this->add( $this->intval( $value ), $fractPart );
00127 $result = $this->adjustFractPart( $result, $precision, $target );
00128
00129 return $result;
00130 }
00131
00132
00133 function adjustFractPart( $number, $precision, $target )
00134 {
00135 if ( is_numeric( $target ) )
00136 {
00137 $target = substr( $target, 0, $precision );
00138 $targetPrecision = strlen( $target );
00139 $target = $this->div( $target, $this->pow( 10, $precision ) );
00140
00141 $intPart = $this->intval( $number );
00142 $fractPart = $this->fractval( $number, $precision - $targetPrecision );
00143 $fractPart = $this->div( $fractPart, $this->pow( 10, $this->sub( $precision, $targetPrecision ) ) );
00144 $fractPart = $this->add( $fractPart, $target );
00145
00146 $number = $this->add( $intPart, $fractPart );
00147 }
00148
00149 return $number;
00150 }
00151
00152 function intval( $number )
00153 {
00154 $intPart = 0;
00155 $pos = strpos( $number, '.' );
00156 if ( $pos !== false )
00157 $intPart = substr( $number, 0, $pos );
00158 else
00159 $intPart = $number;
00160
00161 return $intPart;
00162 }
00163
00164 function fractval( $number, $precision = false )
00165 {
00166 $fractPart = 0;
00167 $pos = strpos( $number, '.' );
00168 if ( $pos !== false )
00169 {
00170 if ( $precision === false )
00171 $fractPart = substr( $number, $pos + 1 );
00172 else
00173 $fractPart = substr( $number, $pos + 1, $precision );
00174 }
00175
00176 return $fractPart;
00177 }
00178 }
00179
00180 ?>