Archive

Posts Tagged ‘Number theory’

Adding numbers as strings

September 18th, 2009

Introduction

Today i ran into the problem of not being able to operate with numbers larger than a 1024-bit signed integer, equilavant to 2^1023. I wanted to be able to add two numbers in this size range, providing a number bigger than this barrier. So i wrote a script, that made it possible to do this, by using a method analouge to how you’d add large numbers on a piece of paper: i used strings, where I added each element in each string with each other.
The code is fairly simple, and there’s plenty of comments in it, so I’ll just post it as-is.

The code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
function add_as_string($a,$b){
	// Produce an error if either $a or $b is not a string.
	if(!is_string($a) || !is_string($b)){
		return "At least one of the provided numbers was not a string!";
	}
 
	// Finds the length of $a or $b, determined by which of $a and $b is the longest string.
	$length = strlen($a)>strlen($b)?strlen($a):strlen($b);
 
	// Zero-pads the strings if neccesary.
	$a = sprintf("%0{$length}s", $a);
	$b = sprintf("%0{$length}s", $b);
 
	// Here the actual addition takes place. Note that it does the addition "backwards".
	for($x=$length-1;$x>=0;$x--){
		$c[$x] += $a[$x]+$b[$x];
		if($c[$x] >= 10){
			$c[$x-1] += 1;
			$c[$x] -= 10;
		}
	}
	$c = array_reverse($c); // Reverse the array containing the addition numbers.
 
	// Puts all the numbers in $c into a string.
	foreach($c as $number){
		$result .= strval($number);
	}
 
	return $result;
}
 
$a = pow(2,1023); // the highest value php can give me. (corresponds to the maximum value of a 1024-bit signed integer)
$a = sprintf("%1.0f",$a); // makes $a a string.
 
// Here we let the parser give the addition a try. Note that this gives INF (meaning infinity).
echo $a+$a . "<br/>";
 
// We try to add the number via the add_as_string function.
// Note that when $a==$b, every loop of the for-loop is equal to multiplication with two. Meaning that if you run the loop twice, it's the same as 4*$a, and so fourth.
for($x=0;$x<1;$x++){
	$a = add_as_string($a,$a);
}
// We output the value of the 
echo $a . "<br/>";
 
// We try putting through two number's that are unequal in length, therefore making use of the zero-padding.
$a = add_as_string("5","50");
echo $a . "<br/>";
 
// We try putting through a number and a string, which will provide an error.
$a = add_as_string(5,$a);
echo $a . "<br/>";
?>

The above code outputs:
INF
179769313486231590772930519 … 79716304835356329624224137216
55
At least one of the provided numbers was not a string!

Notes

Be aware that the script can only be used to calculate addition of integers!
It should be fairly simple to adopt the method to substraction of numbers. My next script might be multiplication of numbers larger than 2^1023. We’ll se ;D

Ricehigh Micro guides , ,