|
|
|
|
|
|
| |
As a final part to my 'big arithemtic' series for large integers, I have included a function that will carry out arithmetic (+ , - , x , /) on large positive or negative integers.
The procedure relies on the other functions of mine (search for them under my name), big_addition, big_subtraction, big_multiply and big_divide to work, and so these functions must appear alongside this function in order to work.
The argument of the function is ($number_one, $number_two, $operator) , where $operator can be either "+" , "-" , "*" or "/" , depending on what operation you want to carry out on the integers. Remember the integers can be positive or negative, unlike the supporting sub-procedures which require that their argument integers be positive.
| <?php
$number_one = "-44786576456";
$number_two = "1788867";
$answer = big_arithmetic($number_one, $number_two, "+");
print $answer;
print "<br>";
print "<br>";
$answer = big_arithmetic($number_one, $number_two, "-");
print $answer;
print "<br>";
print "<br>";
$answer = big_arithmetic($number_one, $number_two, "*");
print $answer;
print "<br>";
print "<br>";
list ($quotient, $remainder) = big_arithmetic($number_one, $number_two, "/");
print "quotient = ";
print $quotient;
print "<br>";
print "remainder = ";
print $remainder;
function big_arithmetic($number_one, $number_two, $operator) {
if ($number_one[0] == "-") {
$sign_one = "-";
$mod_one = substr($number_one, 1);
}
else {
$sign_one = "+";
$mod_one = $number_one;
}
if ($number_two[0] == "-") {
$sign_two = "-";
$mod_two = substr($number_two, 1);
}
else {
$sign_two = "+";
$mod_two = $number_two;
}
$mod_one_larger_mod_two = false;
if (strlen($mod_one) > strlen($mod_two)) {
$mod_one_larger_mod_two = true;
}
if (strlen($mod_one) == strlen($mod_two)) {
$length = strlen($mod_one);
$index = 0;
$flag = false;
while (($index <= ($length - 1)) && ($flag == false)) {
if ((integer)$mod_one[$index] == (integer)$mod_two[$index]) {
$index++;
}
else {
$flag = true;
if ((integer)$mod_one[$index] > (integer)$mod_two[$index]) {
$mod_one_larger_mod_two = true;
}
}
}
}
if ($operator == "+") {
if (($sign_one == "+") && ($sign_two == "+")) {
$answer = big_addition($mod_one, $mod_two);
}
if (($sign_one == "-") && ($sign_two == "+")) {
if ($mod_one_larger_mod_two == true) {
$answer = "-" . (big_subtract($mod_one, $mod_two));
}
else {
$answer = big_subtract($mod_two, $mod_one);
}
}
if (($sign_one == "+") && ($sign_two == "-")) {
if ($mod_one_larger_mod_two == true) {
$answer = big_subtract($mod_one, $mod_two);
}
else {
$answer = "-" . (big_subtract($mod_two, $mod_one));
}
}
if (($sign_one == "-") && ($sign_two == "-")) {
$answer = "-" . (big_addition($mod_one, $mod_two));
}
if ($answer == "-0") {
$answer = "0";
}
return $answer;
}
if ($operator == "-") {
if (($sign_one == "+") && ($sign_two == "+")) {
if ($mod_one_larger_mod_two == true) {
$answer = big_subtract($mod_one, $mod_two);
}
else {
$answer = "-" . (big_subtract($mod_two, $mod_one));
}
}
if (($sign_one == "-") && ($sign_two == "+")) {
$answer = "-" . (big_addition($mod_one, $mod_two));
}
if (($sign_one == "+") && ($sign_two == "-")) {
$answer = big_addition($mod_one, $mod_two);
}
if (($sign_one == "-") && ($sign_two == "-")) {
if ($mod_one_larger_mod_two == true) {
$answer = "-" . (big_subtract($mod_one, $mod_two));
}
else {
$answer = big_subtract($mod_two, $mod_one);
}
}
if ($answer == "-0") {
$answer = "0";
}
return $answer;
}
if ($operator == "*") {
$argument = big_multiply($mod_one, $mod_two);
if ($argument == "0") {
$answer = $argument;
return $answer;
}
else {
if (($sign_one == "+") && ($sign_two == "+")) {
$answer = $argument;
}
if (($sign_one == "-") && ($sign_two == "+")) {
$answer = "-" . $argument;
}
if (($sign_one == "+") && ($sign_two == "-")) {
$answer = "-" . $argument;
}
if (($sign_one == "-") && ($sign_two == "-")) {
$answer = $argument;
}
return $answer;
}
}
if ($operator == "/") {
list($quotient, $remainder) = big_divide($mod_one, $mod_two);
if ($quotient == "error") {
return array($quotient, $remainder);
}
else {
if (($sign_one == "+") && ($sign_two == "+")) {
return array($quotient, $remainder);
}
if (($sign_one == "-") && ($sign_two == "+")) {
if ($quotient != "0") {
$quotient = "-" . $quotient;
}
if ($remainder != "0") {
$remainder = "-" . $remainder;
}
return array($quotient, $remainder);
}
if (($sign_one == "+") && ($sign_two == "-")) {
if ($quotient != "0") {
$quotient = "-" . $quotient;
}
return array($quotient, $remainder);
}
if (($sign_one == "-") && ($sign_two == "-")) {
return array($quotient, $remainder);
}
}
}
}
?> | | |
|
| Math operations on big numbers Categories : PHP, Math. | | | Latitude-Longitude to Miles Categories : PHP, Utilities, Math. | | | Weighted Random - Random Scripts usually chose one out of each item, and each item have an equal chance to be chosen. But what if you want an item to be chosed more frequently than other? Categories : PHP, Math., Arrays | | | Diffusion-Limited Aggregation visualization Categories : PHP, Graphics, Algorithms, Math. | | | Temperature Conversion Categories : PHP, Math., Beginner Guides | | | decoct -- Decimal to octal Categories : PHP, PHP Functions, Math. | | | How to judge if an integer is odd or is even in Php3? Categories : Math., PHP, Algorithms | | | Calculator for Baroque Violin strings Categories : Math., PHP, Strings | | | Show the steps for converting a number from a given base to base 10. Shows the steps involved in converting a number from a given base to base 10. Categories : PHP, Math., Algorithms | | | Query2Report : Generating Html, Pdf and Csv Reports from SQL Query Categories : PHP, PHP, HTML, PDF, Excel | | | 3dLib - a class for drawing in 3D space. Supported functions: Line, SetPixel, Polygon, FilledPolygon, etc. 3dChart() function has been added for one-call drawing of 3d charts. Support of mostly used 3d-transformations. Categories : Graphics, Math., PHP Classes, PHP, Charts and Graphs | | | Easy to use random number function that seeds with uniqid and allows a max value Categories : Math., PHP | | | Arbitrary Precision Math using BCMATH routines Categories : PHP, Math., BC math | | | Greatest Common Denominator - A simple class that finds the greatest common denominator for two integers.
Categories : PHP, PHP Classes, Math. | | | Prime number finder (Sieve of Erastothenes) Categories : PHP, Algorithms, Math. | |
|
|
|