|
eZ Publish
[4.0]
|
00001 <?php 00002 // 00003 // Definition of eZBCMath 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 ezbcmath.php 00032 */ 00033 00034 /*! 00035 \class eZBCMath ezbcmath.php 00036 \brief Handles calculation using bcmath library. 00037 */ 00038 00039 //include_once( 'lib/ezmath/classes/mathhandlers/ezphpmath.php' ); 00040 00041 class eZBCMath extends eZPHPMath 00042 { 00043 const DEFAULT_SCALE = 10; 00044 00045 function eZBCMath( $params = array () ) 00046 { 00047 if( isset( $params['scale'] ) && is_numeric( $params['scale'] ) ) 00048 $this->setScale( $params['scale'] ); 00049 else 00050 $this->setScale( self::DEFAULT_SCALE ); 00051 } 00052 00053 function scale() 00054 { 00055 return $this->Scale; 00056 } 00057 00058 function setScale( $scale ) 00059 { 00060 $this->Scale = $scale; 00061 } 00062 00063 function add( $a, $b ) 00064 { 00065 return ( bcadd( $a, $b, $this->Scale ) ); 00066 } 00067 00068 function sub( $a, $b ) 00069 { 00070 return ( bcsub( $a, $b, $this->Scale ) ); 00071 } 00072 00073 function mul( $a, $b ) 00074 { 00075 return ( bcmul( $a, $b, $this->Scale ) ); 00076 } 00077 00078 function div( $a, $b ) 00079 { 00080 return ( bcdiv( $a, $b, $this->Scale ) ); 00081 } 00082 00083 function pow( $base, $exp ) 00084 { 00085 return ( bcpow( $base, $exp, $this->Scale ) ); 00086 } 00087 00088 function ceil( $value, $precision, $target ) 00089 { 00090 $result = eZPHPMath::ceil( $value, $precision, $target ); 00091 $result = rtrim( $result, '0' ); 00092 $result = rtrim( $result, '.' ); 00093 return $result; 00094 } 00095 00096 function floor( $value, $precision, $target ) 00097 { 00098 $result = eZPHPMath::floor( $value, $precision, $target ); 00099 $result = rtrim( $result, '0' ); 00100 $result = rtrim( $result, '.' ); 00101 return $result; 00102 } 00103 00104 function round( $value, $precision, $target ) 00105 { 00106 $result = $value; 00107 $fractPart = $this->fractval( $value, $precision + 1 ); 00108 if ( strlen( $fractPart ) > $precision ) 00109 { 00110 $lastDigit = (int)substr( $fractPart, -1, 1 ); 00111 $fractPart = substr( $fractPart, 0, $precision ); 00112 if ( $lastDigit >= 5 ) 00113 $fractPart = $this->add( $fractPart, 1 ); 00114 00115 $fractPart = $this->div( $fractPart, $this->pow( 10, $precision ) ); 00116 00117 $result = $this->add( $this->intval( $value ), $fractPart ); 00118 $result = $this->adjustFractPart( $result, $precision, $target ); 00119 00120 $result = rtrim( $result, '0' ); 00121 $result = rtrim( $result, '.' ); 00122 } 00123 00124 return $result; 00125 } 00126 00127 00128 /// \privatesection 00129 public $Scale; 00130 }; 00131 00132 ?>