FRIHOSTFORUMSSEARCHFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

PHP "add" function [fixed]

 


Fire Boar
If you looked at the title and thought it referred to a function that does exactly the same as "+", you are pretty much spot on. That's exactly what I'm trying to make. Why, you ask? Because I'm not happy with the way it handles big integers, that is, converting them into standard form.

So, following is a function that can add numbers as big as you like together. It works by splitting each number into 8-digit chunks and adding them, then keeping hold of the zeroes. It's doing alright, but there are just two problems. Here is the code:

Code:
<?php
// Adds two numbers together. This allows for very big numbers.
function add($func_number1, $func_number2)
{
   $num1_length = strlen($func_number1);
   $num2_length = strlen($func_number2);
   $num1 = chunk_split($func_number1, 8, ":");
   $num2 = chunk_split($func_number2, 8, ":");
   $array1 = explode(":", $num1);
   $array2 = explode(":", $num2);
   $array1_length = count($array1);
   $array2_length = count($array2);
   $out = array();
   if ($array1_length < $array2_length)
   {
      for ($i = ($array1_length - 1); $i >= 0; $i--)
      {
         $j = ($array2_length - $array1_length) + $i;
         $out[$j] = $array1[$i]+$array2[$j];
      }
   }
   // Doesn't matter if they're the same - we still get the same results.
   else
   {
      for ($i = ($array2_length - 1); $i >= 0; $i--)
      {
         $j = ($array1_length - $array2_length) + $i;
         $out[$j] = $array1[$j]+$array2[$i];
      }
   }
   // Now add them up!
   $carry = 0;
   for ($k = (count($out) - 1); $k >= 0; $k--)
   {
      $addremainder = intval($out[$k])+$carry;
      $out[$k] = strval($addremainder);
      while (strlen($out[$k]) < 8)
      {
         $out[$k] = "0".$out[$k];
      }
      if (strlen($out[$k]) > 8)
      {
         $carry = 1;
      }
      else
      {
         $carry = 0;
      }
   }
   return implode('', $out);
}
?>


That's the function. It's workable in theory, but I think I might have screwed up a little on the logic. Here's why:

Input 1: 0070011401010100010001050101
Input 2: 0116011000510050011801120115

Output: 0000000000000216021802170152015001860224

The input is the way it is for a reason - it realistically mimics the sorts of numbers I will be using the function for.

The output, as you can see, is messed up a little, but it is okay except for two things.

1) There are eight unwanted zeroes prepending the output. The other five are okay, they can stay.
2) The output is back to front. Splitting it into 8 chunks and reversing the order of chunks comes out with the correct answer.


If anyone could help, I'd really appreciate it.

Thanks.


Last edited by Fire Boar on Wed Mar 14, 2007 7:12 pm; edited 1 time in total
MrBlueSky
Why don't you use PHP's arbitrary precision integer mathematical functions?

For example bcadd()
Fire Boar
Because... I had no idea the function existed. Whoops!

*deletes that long and pointless function*

Embarassed
Related topics
PHP mail function ?
PHP mail() Function
SMTP, sendmail e PHP mail function
PHP search function with MySQL fulltext
PHP VS ASP
Add function for link page
php mail(); problem... half fixed?
PHP mail() function...
Including With PHP
PHP Mail Function
Site Nav Suggestions...
tweaking wordpress
Php mail function
PHP Mail - Always Junked?
Mailer settings in Joomla
Error with MySQL and PHP script in my CMS
php: copy function
how to send the mail using smtp in php?
php mail and BCC
Send Mail HTML Form
Sending email via PHP
so, this is what I want to do... help needed
SMTP, DB, & Hosting Domain Qs
crazy ideas eh?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.