PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/libraries/3rdparty/Zend/Locale/Math/PhpMath.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 247 lines | 184 code | 31 blank | 32 comment | 39 complexity | 52ffda0dd79bbcb0e3f5960a6a3d97e1 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Locale
  17. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: PhpMath.php 23775 2011-03-01 17:25:24Z ralph $
  20. */
  21. /**
  22. * Utility class for proxying math function to bcmath functions, if present,
  23. * otherwise to PHP builtin math operators, with limited detection of overflow conditions.
  24. * Sampling of PHP environments and platforms suggests that at least 80% to 90% support bcmath.
  25. * This file should only be loaded for the 10% to 20% lacking access to the bcmath extension.
  26. *
  27. * @category Zend
  28. * @package Zend_Locale
  29. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Locale_Math_PhpMath extends Zend_Locale_Math
  33. {
  34. public static function disable()
  35. {
  36. self::$_bcmathDisabled = true;
  37. self::$add = array('Zend_Locale_Math_PhpMath', 'Add');
  38. self::$sub = array('Zend_Locale_Math_PhpMath', 'Sub');
  39. self::$pow = array('Zend_Locale_Math_PhpMath', 'Pow');
  40. self::$mul = array('Zend_Locale_Math_PhpMath', 'Mul');
  41. self::$div = array('Zend_Locale_Math_PhpMath', 'Div');
  42. self::$comp = array('Zend_Locale_Math_PhpMath', 'Comp');
  43. self::$sqrt = array('Zend_Locale_Math_PhpMath', 'Sqrt');
  44. self::$mod = array('Zend_Locale_Math_PhpMath', 'Mod');
  45. self::$scale = array('Zend_Locale_Math_PhpMath', 'Scale');
  46. self::$defaultScale = 0;
  47. self::$defaultPrecision = 1;
  48. }
  49. public static $defaultScale;
  50. public static $defaultPrecision;
  51. public static function Add($op1, $op2, $scale = null)
  52. {
  53. if ($scale === null) {
  54. $scale = Zend_Locale_Math_PhpMath::$defaultScale;
  55. $precision = Zend_Locale_Math_PhpMath::$defaultPrecision;
  56. } else {
  57. $precision = pow(10, -$scale);
  58. }
  59. if (empty($op1)) {
  60. $op1 = 0;
  61. }
  62. $op1 = self::normalize($op1);
  63. $op2 = self::normalize($op2);
  64. $result = $op1 + $op2;
  65. if (is_infinite($result) or (abs($result - $op2 - $op1) > $precision)) {
  66. require_once 'Zend/Locale/Math/Exception.php';
  67. throw new Zend_Locale_Math_Exception("addition overflow: $op1 + $op2 != $result", $op1, $op2, $result);
  68. }
  69. return self::round(self::normalize($result), $scale);
  70. }
  71. public static function Sub($op1, $op2, $scale = null)
  72. {
  73. if ($scale === null) {
  74. $scale = Zend_Locale_Math_PhpMath::$defaultScale;
  75. $precision = Zend_Locale_Math_PhpMath::$defaultPrecision;
  76. } else {
  77. $precision = pow(10, -$scale);
  78. }
  79. if (empty($op1)) {
  80. $op1 = 0;
  81. }
  82. $op1 = self::normalize($op1);
  83. $op2 = self::normalize($op2);
  84. $result = $op1 - $op2;
  85. if (is_infinite($result) or (abs($result + $op2 - $op1) > $precision)) {
  86. require_once 'Zend/Locale/Math/Exception.php';
  87. throw new Zend_Locale_Math_Exception("subtraction overflow: $op1 - $op2 != $result", $op1, $op2, $result);
  88. }
  89. return self::round(self::normalize($result), $scale);
  90. }
  91. public static function Pow($op1, $op2, $scale = null)
  92. {
  93. if ($scale === null) {
  94. $scale = Zend_Locale_Math_PhpMath::$defaultScale;
  95. }
  96. $op1 = self::normalize($op1);
  97. $op2 = self::normalize($op2);
  98. // BCMath extension doesn't use decimal part of the power
  99. // Provide the same behavior
  100. $op2 = ($op2 > 0) ? floor($op2) : ceil($op2);
  101. $result = pow($op1, $op2);
  102. if (is_infinite($result) or is_nan($result)) {
  103. require_once 'Zend/Locale/Math/Exception.php';
  104. throw new Zend_Locale_Math_Exception("power overflow: $op1 ^ $op2", $op1, $op2, $result);
  105. }
  106. return self::round(self::normalize($result), $scale);
  107. }
  108. public static function Mul($op1, $op2, $scale = null)
  109. {
  110. if ($scale === null) {
  111. $scale = Zend_Locale_Math_PhpMath::$defaultScale;
  112. }
  113. if (empty($op1)) {
  114. $op1 = 0;
  115. }
  116. $op1 = self::normalize($op1);
  117. $op2 = self::normalize($op2);
  118. $result = $op1 * $op2;
  119. if (is_infinite($result) or is_nan($result)) {
  120. require_once 'Zend/Locale/Math/Exception.php';
  121. throw new Zend_Locale_Math_Exception("multiplication overflow: $op1 * $op2 != $result", $op1, $op2, $result);
  122. }
  123. return self::round(self::normalize($result), $scale);
  124. }
  125. public static function Div($op1, $op2, $scale = null)
  126. {
  127. if ($scale === null) {
  128. $scale = Zend_Locale_Math_PhpMath::$defaultScale;
  129. }
  130. if (empty($op2)) {
  131. require_once 'Zend/Locale/Math/Exception.php';
  132. throw new Zend_Locale_Math_Exception("can not divide by zero", $op1, $op2, null);
  133. }
  134. if (empty($op1)) {
  135. $op1 = 0;
  136. }
  137. $op1 = self::normalize($op1);
  138. $op2 = self::normalize($op2);
  139. $result = $op1 / $op2;
  140. if (is_infinite($result) or is_nan($result)) {
  141. require_once 'Zend/Locale/Math/Exception.php';
  142. throw new Zend_Locale_Math_Exception("division overflow: $op1 / $op2 != $result", $op1, $op2, $result);
  143. }
  144. return self::round(self::normalize($result), $scale);
  145. }
  146. public static function Sqrt($op1, $scale = null)
  147. {
  148. if ($scale === null) {
  149. $scale = Zend_Locale_Math_PhpMath::$defaultScale;
  150. }
  151. if (empty($op1)) {
  152. $op1 = 0;
  153. }
  154. $op1 = self::normalize($op1);
  155. $result = sqrt($op1);
  156. if (is_nan($result)) {
  157. return NULL;
  158. }
  159. return self::round(self::normalize($result), $scale);
  160. }
  161. public static function Mod($op1, $op2)
  162. {
  163. if (empty($op1)) {
  164. $op1 = 0;
  165. }
  166. if (empty($op2)) {
  167. return NULL;
  168. }
  169. $op1 = self::normalize($op1);
  170. $op2 = self::normalize($op2);
  171. if ((int)$op2 == 0) {
  172. return NULL;
  173. }
  174. $result = $op1 % $op2;
  175. if (is_nan($result) or (($op1 - $result) % $op2 != 0)) {
  176. require_once 'Zend/Locale/Math/Exception.php';
  177. throw new Zend_Locale_Math_Exception("modulus calculation error: $op1 % $op2 != $result", $op1, $op2, $result);
  178. }
  179. return self::normalize($result);
  180. }
  181. public static function Comp($op1, $op2, $scale = null)
  182. {
  183. if ($scale === null) {
  184. $scale = Zend_Locale_Math_PhpMath::$defaultScale;
  185. }
  186. if (empty($op1)) {
  187. $op1 = 0;
  188. }
  189. $op1 = self::normalize($op1);
  190. $op2 = self::normalize($op2);
  191. if ($scale <> 0) {
  192. $op1 = self::round($op1, $scale);
  193. $op2 = self::round($op2, $scale);
  194. } else {
  195. $op1 = ($op1 > 0) ? floor($op1) : ceil($op1);
  196. $op2 = ($op2 > 0) ? floor($op2) : ceil($op2);
  197. }
  198. if ($op1 > $op2) {
  199. return 1;
  200. } else if ($op1 < $op2) {
  201. return -1;
  202. }
  203. return 0;
  204. }
  205. public static function Scale($scale)
  206. {
  207. if ($scale > 9) {
  208. require_once 'Zend/Locale/Math/Exception.php';
  209. throw new Zend_Locale_Math_Exception("can not scale to precision $scale", $scale, null, null);
  210. }
  211. self::$defaultScale = $scale;
  212. self::$defaultPrecision = pow(10, -$scale);
  213. return true;
  214. }
  215. }
  216. Zend_Locale_Math_PhpMath::disable(); // disable use of bcmath functions