/xampp/php/PEAR/Math/IntegerOp.php

https://github.com/edmondscommerce/XAMPP-Magento-Demo-Site · PHP · 404 lines · 237 code · 17 blank · 150 comment · 37 complexity · adb31c2caa6c9971d671948c9f21b954 MD5 · raw file

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2002 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: IntegerOp.php,v 1.1 2003/01/02 01:55:05 jmcastagnetto Exp $
  20. //
  21. include_once 'Math/Integer.php';
  22. /**
  23. * Class implementing operations on Math_Integer objects. If available it
  24. * will use the GMP or BCMATH libraries. Will default to the standard PHP
  25. * integer representation otherwise.
  26. *
  27. * The operations are implemented as static methods of the class.
  28. *
  29. * @author Jesus M. Castagnetto <jmcastagnetto@php.net>
  30. * @version 0.8
  31. * @access public
  32. * @package Math_Integer
  33. */
  34. class Math_IntegerOp {/*{{{*/
  35. /**
  36. * Checks if the given parameter is a Math_Integer object
  37. *
  38. * @param object Math_Integer $int1
  39. * @return boolean TRUE if parameter is an instance of Math_Integer, FALSE otherwise
  40. * @access public
  41. */
  42. function isMath_Integer(&$int) {/*{{{*/
  43. if (function_exists('is_a')) {
  44. return is_a($int, 'Math_Integer');
  45. } else {
  46. return get_class($int) == 'math_integer'
  47. || is_subclass_of($int, 'math_integer');
  48. }
  49. }/*}}}*/
  50. /**
  51. * Add two Math_Integer objects: $i1 + $i2
  52. *
  53. * @param object Math_Integer $int1
  54. * @param object Math_Integer $int2
  55. * @return object Math_Integer on success, PEAR_Error otherwise
  56. * @access public
  57. */
  58. function &add(&$int1, &$int2) {/*{{{*/
  59. if (PEAR::isError($err = Math_IntegerOp::_validInts($int1, $int2))) {
  60. return $err;
  61. }
  62. switch (MATH_INTLIB) {/*{{{*/
  63. case 'gmp' :
  64. $tmp = gmp_strval(gmp_add($int1->getValue(), $int2->getValue()));
  65. break;
  66. case 'bcmath' :
  67. $tmp = bcadd($int1->getValue(), $int2->getValue());
  68. break;
  69. case 'std' :
  70. $tmp = $int1->getValue() + $int2->getValue();
  71. break;
  72. }/*}}}*/
  73. return new Math_Integer($tmp);
  74. }/*}}}*/
  75. /**
  76. * Substract two Math_Integer objects: $i1 - $i2
  77. *
  78. * @param object Math_Integer $int1
  79. * @param object Math_Integer $int2
  80. * @return object Math_Integer on success, PEAR_Error otherwise
  81. * @access public
  82. */
  83. function &sub(&$int1, &$int2) {/*{{{*/
  84. if (PEAR::isError($err = Math_IntegerOp::_validInts($int1, $int2))) {
  85. return $err;
  86. }
  87. switch (MATH_INTLIB) {/*{{{*/
  88. case 'gmp' :
  89. $tmp = gmp_strval(gmp_sub($int1->getValue(), $int2->getValue()));
  90. break;
  91. case 'bcmath' :
  92. $tmp = bcsub($int1->getValue(), $int2->getValue());
  93. break;
  94. case 'std' :
  95. $tmp = $int1->getValue() - $int2->getValue();
  96. break;
  97. }/*}}}*/
  98. return new Math_Integer($tmp);
  99. }/*}}}*/
  100. /**
  101. * Multiply two Math_Integer objects: $i1 * $i2
  102. *
  103. * @param object Math_Integer $int1
  104. * @param object Math_Integer $int2
  105. * @return object Math_Integer on success, PEAR_Error otherwise
  106. * @access public
  107. */
  108. function &mul(&$int1, &$int2) {/*{{{*/
  109. if (PEAR::isError($err = Math_IntegerOp::_validInts($int1, $int2))) {
  110. return $err;
  111. }
  112. switch (MATH_INTLIB) {/*{{{*/
  113. case 'gmp' :
  114. $tmp = gmp_strval(gmp_mul($int1->getValue(), $int2->getValue()));
  115. break;
  116. case 'bcmath' :
  117. $tmp = bcmul($int1->getValue(), $int2->getValue());
  118. break;
  119. case 'std' :
  120. $tmp = $int1->getValue() * $int2->getValue();
  121. break;
  122. }/*}}}*/
  123. return new Math_Integer($tmp);
  124. }/*}}}*/
  125. /**
  126. * Divide two Math_Integer objects: $i1 / $i2
  127. *
  128. * @param object Math_Integer $int1
  129. * @param object Math_Integer $int2
  130. * @return object Math_Integer on success, PEAR_Error otherwise
  131. * @access public
  132. */
  133. function &div(&$int1, &$int2) {/*{{{*/
  134. if (PEAR::isError($err = Math_IntegerOp::_validInts($int1, $int2))) {
  135. return $err;
  136. }
  137. switch (MATH_INTLIB) {/*{{{*/
  138. case 'gmp' :
  139. $tmp = gmp_strval(gmp_div($int1->getValue(), $int2->getValue()));
  140. break;
  141. case 'bcmath' :
  142. $tmp = bcdiv($int1->getValue(), $int2->getValue());
  143. break;
  144. case 'std' :
  145. $tmp = intval($int1->getValue() / $int2->getValue());
  146. break;
  147. }/*}}}*/
  148. return new Math_Integer($tmp);
  149. }/*}}}*/
  150. /**
  151. * Calculate the modulus of $i1 and $i2: $i1 % $i2
  152. *
  153. * @param object Math_Integer $int1
  154. * @param object Math_Integer $int2
  155. * @return object Math_Integer on success, PEAR_Error otherwise
  156. * @access public
  157. */
  158. function &mod(&$int1, &$int2) {/*{{{*/
  159. if (PEAR::isError($err = Math_IntegerOp::_validInts($int1, $int2))) {
  160. return $err;
  161. }
  162. switch (MATH_INTLIB) {/*{{{*/
  163. case 'gmp' :
  164. $tmp = gmp_strval(gmp_mod($int1->getValue(), $int2->getValue()));
  165. break;
  166. case 'bcmath' :
  167. $tmp = bcmod($int1->getValue(), $int2->getValue());
  168. break;
  169. case 'std' :
  170. $tmp = $int1->getValue() % $int2->getValue();
  171. break;
  172. }/*}}}*/
  173. return new Math_Integer($tmp);
  174. }/*}}}*/
  175. /**
  176. * Raise $i1 to the $i2 exponent: $i1^$i2
  177. *
  178. * @param object Math_Integer $int1
  179. * @param object Math_Integer $int2
  180. * @return object Math_Integer on success, PEAR_Error otherwise
  181. * @access public
  182. */
  183. function &pow(&$int1, &$int2) {/*{{{*/
  184. if (PEAR::isError($err = Math_IntegerOp::_validInts($int1, $int2))) {
  185. return $err;
  186. }
  187. switch (MATH_INTLIB) {/*{{{*/
  188. case 'gmp' :
  189. $tmp = gmp_strval(gmp_pow($int1->getValue(), (int) $int2->toString()));
  190. break;
  191. case 'bcmath' :
  192. $tmp = bcpow($int1->getValue(), $int2->getValue());
  193. break;
  194. case 'std' :
  195. $tmp = pow($int1->getValue(), $int2->getValue());
  196. break;
  197. }/*}}}*/
  198. return new Math_Integer($tmp);
  199. }/*}}}*/
  200. /**
  201. * Compare two Math_Integer objects.
  202. * if $i1 > $i2, returns +1,
  203. * if $i1 == $i2, returns +0,
  204. * if $i1 < $i2, returns -1,
  205. *
  206. * @param object Math_Integer $int1
  207. * @param object Math_Integer $int2
  208. * @return mixed and integer on success, PEAR_Error otherwise
  209. * @access public
  210. * @see Math_IntegerOp::sign
  211. */
  212. function &compare(&$int1, &$int2) {/*{{{*/
  213. if (PEAR::isError($err = Math_IntegerOp::_validInts($int1, $int2))) {
  214. return $err;
  215. }
  216. switch (MATH_INTLIB) {/*{{{*/
  217. case 'gmp' :
  218. $cmp = gmp_cmp($int1->getValue(), $int2->getValue());
  219. break;
  220. case 'bcmath' :
  221. $cmp = bccomp($int1->getValue(), $int2->getValue());
  222. break;
  223. case 'std' :
  224. $cmp = $int1->getValue() - $int2->getValue();
  225. break;
  226. }/*}}}*/
  227. return Math_IntegerOp::sign(new Math_Integer($cmp));
  228. }/*}}}*/
  229. /**
  230. * Returns the sign of a Math_Integer number
  231. * if $i1 > 0, returns +1,
  232. * if $i1 == 0, returns +0,
  233. * if $i1 < 0, returns -1,
  234. *
  235. * @param object Math_Integer $int1
  236. * @return mixed and integer on success, PEAR_Error otherwise
  237. * @access public
  238. */
  239. function &sign(&$int1) {/*{{{*/
  240. if (PEAR::isError($err = Math_IntegerOp::_validInt($int1))) {
  241. return $err;
  242. }
  243. switch (MATH_INTLIB) {/*{{{*/
  244. case 'gmp' :
  245. return gmp_sign($int1->getValue());
  246. break;
  247. case 'bcmath' :
  248. case 'std' :
  249. $tmp = $int1->getValue();
  250. if ($tmp > 0) {
  251. return 1;
  252. } elseif ($tmp < 0) {
  253. return -1;
  254. } else { // $tmp == 0
  255. return 0;
  256. }
  257. break;
  258. }/*}}}*/
  259. }/*}}}*/
  260. /**
  261. * Returns the negative of a Math_Integer number: -1 * $i1
  262. *
  263. * @param object Math_Integer $int1
  264. * @return object Math_Integer on success, PEAR_Error otherwise
  265. * @access public
  266. */
  267. function &neg(&$int1) {/*{{{*/
  268. if (PEAR::isError($err = Math_IntegerOp::_validInt($int1))) {
  269. return $err;
  270. }
  271. switch (MATH_INTLIB) {/*{{{*/
  272. case 'gmp' :
  273. $tmp = gmp_strval(gmp_neg($int1->getValue()));
  274. break;
  275. case 'bcmath' :
  276. $tmp = bcmul(-1, $int1->getValue());
  277. break;
  278. case 'std' :
  279. $tmp = -1 * $int1->getValue();
  280. break;
  281. }/*}}}*/
  282. return new Math_Integer($tmp);
  283. }/*}}}*/
  284. /**
  285. * Returns the (integer) square root of a Math_Integer number
  286. *
  287. * @param object Math_Integer $int1
  288. * @return object Math_Integer on success, PEAR_Error otherwise
  289. * @access public
  290. */
  291. function &sqrt(&$int1) {/*{{{*/
  292. if (PEAR::isError($err = Math_IntegerOp::_validInt($int1))) {
  293. return $err;
  294. }
  295. switch (MATH_INTLIB) {/*{{{*/
  296. case 'gmp' :
  297. $tmp = gmp_strval(gmp_sqrt($int1->getValue()));
  298. break;
  299. case 'bcmath' :
  300. $tmp = bcsqrt(-1, $int1->getValue());
  301. break;
  302. case 'std' :
  303. $tmp = intval(sqrt($int1->getValue()));
  304. break;
  305. }/*}}}*/
  306. return new Math_Integer($tmp);
  307. }/*}}}*/
  308. /**
  309. * Returns the absolute value of a Math_Integer number
  310. *
  311. * @param object Math_Integer $int1
  312. * @return object Math_Integer on success, PEAR_Error otherwise
  313. * @access public
  314. */
  315. function &abs(&$int1) {/*{{{*/
  316. if (PEAR::isError($err = Math_IntegerOp::_validInt($int1))) {
  317. return $err;
  318. }
  319. switch (MATH_INTLIB) {/*{{{*/
  320. case 'gmp' :
  321. $tmp = gmp_strval(gmp_abs($int1->getValue()));
  322. break;
  323. case 'bcmath' :
  324. if ($int1->getValue() < 0) {
  325. $tmp = bcmul(-1, $int1->getValue());
  326. } else {
  327. $tmp = $int1->getValue();
  328. }
  329. break;
  330. case 'std' :
  331. $tmp = abs($int1->getValue());
  332. break;
  333. }/*}}}*/
  334. return new Math_Integer($tmp);
  335. }/*}}}*/
  336. /**
  337. * Checks that the 2 passed objects are valid Math_Integer numbers.
  338. * The objects must be instances of Math_Integer and have been properly
  339. * initialized.
  340. *
  341. * @param object Math_Integer $int1
  342. * @param object Math_Integer $int2
  343. * @return mixed TRUE if both are Math_Integer objects, PEAR_Error otherwise
  344. * @access private
  345. */
  346. function _validInts(&$int1, &$int2) {/*{{{*/
  347. $err1 = Math_IntegerOp::_validInt($int1);
  348. $err2 = Math_IntegerOp::_validInt($int2);
  349. $error = '';
  350. if (PEAR::isError($err1)) {
  351. $error .= 'First parameter: '.$err1->getMessage();
  352. }
  353. if (PEAR::isError($err2)) {
  354. $error .= ' Second parameter: '.$err2->getMessage();
  355. }
  356. if (!empty($error)) {
  357. return PEAR::raiseError($error);
  358. } else {
  359. return true;
  360. }
  361. }/*}}}*/
  362. /**
  363. * Checks that the passed object is a valid Math_Integer number.
  364. * The object must be an instance of Math_Integer and have been properly
  365. * initialized.
  366. *
  367. * @param object Math_Integer $int1
  368. * @return mixed TRUE if is a Math_Integer object, PEAR_Error otherwise
  369. * @access private
  370. */
  371. function _validInt(&$int1) {/*{{{*/
  372. $error = '';
  373. if (!Math_IntegerOp::isMath_Integer($int1)) {
  374. $error = 'Is not a Math_Integer object.';
  375. } elseif (!$int1->initialized()) {
  376. $error = 'Math_Integer object is uninitalized.';
  377. }
  378. if (!empty($error)) {
  379. return PEAR::raiseError($error);
  380. } else {
  381. return true;
  382. }
  383. }/*}}}*/
  384. }/*}}} end of Math_IntegerOp */
  385. // vim: ts=4:sw=4:et:
  386. // vim6: fdl=1:
  387. ?>