Rev 130 Rev 139
Line 1... Line 1...
1 <?php 1 <?php
2 // +----------------------------------------------------------------------+ 2 // +----------------------------------------------------------------------+
3 // | PHP Version 4 | 3 // | PHP Version 4 |
4 // +----------------------------------------------------------------------+ 4 // +----------------------------------------------------------------------+
5 // | Copyright (c) 1997-2004 The PHP Group | 5 // | Copyright (c) 1997-2004 The PHP Group |
6 // +----------------------------------------------------------------------+ 6 // +----------------------------------------------------------------------+
7 // | This source file is subject to version 3.0 of the PHP license, | 7 // | This source file is subject to version 3.0 of the PHP license, |
8 // | that is bundled with this package in the file LICENSE, and is | 8 // | that is bundled with this package in the file LICENSE, and is |
9 // | available at through the world-wide-web at | 9 // | available at through the world-wide-web at |
10 // | http://www.php.net/license/3_0.txt. | 10 // | http://www.php.net/license/3_0.txt. |
11 // | If you did not receive a copy of the PHP license and are unable to | 11 // | If you did not receive a copy of the PHP license and are unable to |
12 // | obtain it through the world-wide-web, please send a note to | 12 // | obtain it through the world-wide-web, please send a note to |
13 // | license@php.net so we can mail you a copy immediately. | 13 // | license@php.net so we can mail you a copy immediately. |
14 // +----------------------------------------------------------------------+ 14 // +----------------------------------------------------------------------+
15 // | Authors: Aidan Lister <aidan@php.net> | 15 // | Authors: Aidan Lister <aidan@php.net> |
16 // +----------------------------------------------------------------------+ 16 // +----------------------------------------------------------------------+
17 // 17 //
18 // $Id: bcinvert.php,v 1.2 2005/11/22 20:24:45 aidan Exp $ 18 // $Id: bcinvert.php,v 1.2 2005/11/22 20:24:45 aidan Exp $
19   19  
20   20  
21 /** 21 /**
22 * Replace bcinvert() 22 * Replace bcinvert()
23 * 23 *
24 * @category PHP 24 * @category PHP
25 * @package PHP_Compat 25 * @package PHP_Compat
26 * @link http://php.net/function.bcinvert 26 * @link http://php.net/function.bcinvert
27 * @author Sara Golemon <pollita@php.net> 27 * @author Sara Golemon <pollita@php.net>
28 * @version $Revision: 1.2 $ 28 * @version $Revision: 1.2 $
29 * @since PHP 5.2.0 29 * @since PHP 5.2.0
30 * @require PHP 4.0.4 (call_user_func_array) 30 * @require PHP 4.0.4 (call_user_func_array)
31 */ 31 */
32 if (!function_exists('bcinvert')) { 32 if (!function_exists('bcinvert')) {
33 function bcinvert($a, $n) 33 function bcinvert($a, $n)
34 { 34 {
35 // Sanity check 35 // Sanity check
36 if (!is_scalar($a)) { 36 if (!is_scalar($a)) {
37 user_error('bcinvert() expects parameter 1 to be string, ' . 37 user_error('bcinvert() expects parameter 1 to be string, ' .
38 gettype($a) . ' given', E_USER_WARNING); 38 gettype($a) . ' given', E_USER_WARNING);
39 return false; 39 return false;
40 } 40 }
41   41  
42 if (!is_scalar($n)) { 42 if (!is_scalar($n)) {
43 user_error('bcinvert() expects parameter 2 to be string, ' . 43 user_error('bcinvert() expects parameter 2 to be string, ' .
44 gettype($n) . ' given', E_USER_WARNING); 44 gettype($n) . ' given', E_USER_WARNING);
45 return false; 45 return false;
46 } 46 }
47 47
48 $u1 = $v2 = '1'; 48 $u1 = $v2 = '1';
49 $u2 = $v1 = '0'; 49 $u2 = $v1 = '0';
50 $u3 = $n; 50 $u3 = $n;
51 $v3 = $a; 51 $v3 = $a;
52   52  
53 while (bccomp($v3, '0')) { 53 while (bccomp($v3, '0')) {
54 $q0 = bcdiv($u3, $v3); 54 $q0 = bcdiv($u3, $v3);
55 $t1 = bcsub($u1, bcmul($q0, $v1)); 55 $t1 = bcsub($u1, bcmul($q0, $v1));
56 $t2 = bcsub($u2, bcmul($q0, $v2)); 56 $t2 = bcsub($u2, bcmul($q0, $v2));
57 $t3 = bcsub($u3, bcmul($q0, $v3)); 57 $t3 = bcsub($u3, bcmul($q0, $v3));
58   58  
59 $u1 = $v1; 59 $u1 = $v1;
60 $u2 = $v2; 60 $u2 = $v2;
61 $u3 = $v3; 61 $u3 = $v3;
62   62  
63 $v1 = $t1; 63 $v1 = $t1;
64 $v2 = $t2; 64 $v2 = $t2;
65 $v3 = $t3; 65 $v3 = $t3;
66 } 66 }
67   67  
68 if (bccomp($u2, '0') < 0) { 68 if (bccomp($u2, '0') < 0) {
69 return bcadd($u2, $n); 69 return bcadd($u2, $n);
70 } else { 70 } else {
71 return bcmod($u2, $n); 71 return bcmod($u2, $n);
72 } 72 }
73 } 73 }
74 } 74 }
75   75  
76 ?> 76 ?>
77 77