Math_BigInteger
[ class tree: Math_BigInteger ] [ index: Math_BigInteger ] [ all elements ]

Source for file demo.php

Documentation is available at demo.php

  1. <?
  2. // $Id$
  3. // Example of how to use of BigInteger.  The output can be compared to the output that the BCMath functions would yield.
  4.  
  5. // bcpowmod is included with Math_BigInteger.php via PHP_Compat.
  6.  
  7. include('../Math/BigInteger.php');
  8.  
  9. $x = mt_rand(1,10000000);
  10. $y = mt_rand(1,10000000);
  11. $z = mt_rand(1,10000000);
  12.  
  13. $_x = new Math_BigInteger($x);
  14. $_y = new Math_BigInteger($y);
  15. $_z = new Math_BigInteger($z);
  16.  
  17. echo "\$x = $x;\r\n";
  18. echo "\$y = $y;\r\n";
  19. echo "\$z = $z;\r\n";
  20.  
  21. echo "\r\n";
  22.  
  23. $result = bcadd($x,$y);
  24. $_result = $_x->add($_y);
  25.  
  26. echo "\$result = \$x+\$y;\r\n";
  27. echo "$result\r\n";
  28. echo $_result->toString();
  29. echo "\r\n\r\n";
  30.  
  31. $result = bcsub($result,$y);
  32. $_result = $_result->subtract($_y);
  33.  
  34. echo "\$result = \$result-\$y;\r\n";
  35. echo "$result\r\n";
  36. echo $_result->toString();
  37. echo "\r\n\r\n";
  38.  
  39. $result = bcdiv($x,$y);
  40. list($_result,) = $_x->divide($_y);
  41.  
  42. echo "\$result = \$x/\$y;\r\n";
  43. echo "$result\r\n";
  44. echo $_result->toString();
  45. echo "\r\n\r\n";
  46.  
  47. $result = bcmod($y,$z);
  48. list(,$_result) = $_y->divide($_z);
  49.  
  50. echo "\$result = \$x%\$y;\r\n";
  51. echo "$result\r\n";
  52. echo $_result->toString();
  53. echo "\r\n\r\n";
  54.  
  55. $result = bcmul($x,$z);
  56. $_result = $_x->multiply($_z);
  57.  
  58. echo "\$result = \$x*\$z;\r\n";
  59. echo "$result\r\n";
  60. echo $_result->toString();
  61. echo "\r\n\r\n";
  62.  
  63. $result = bcpowmod($x,$y,$result);
  64. $_result = $_x->modPow($_y,$_result);
  65.  
  66. echo "\$result = (\$x**\$y)%\$result;\r\n";
  67. echo "$result\r\n";
  68. echo $_result->toString();
  69. echo "\r\n\r\n";
  70.  
  71. // modInverse isn't demo'd because no equivalent to it exists in BCMath.
  72.  
  73. ?>

Documentation generated on Mon, 11 Mar 2019 15:56:35 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.