View Full Version : Multiplication of Decimals by addition only...
sage45 02082005, 01:50 AM How would you do this progmatically???
I was thinking something along these lines:
Get two variables (a and n)
if a is less than n use it as the loop integer, else use n as the loop integer.
whichever is the loop integer the other is the addition integer.
Ex:
S = 0; a = 5 and n = 20
For 1 to a
S = S + n
Next a
Now if:
S = 0; a = 1.2 and n = 20
I found an interesting method which involves using the floor(a) + 1 to run the loop integer.
ta = a
a = floor(a) + 1
For 1 to a
S = S + n
S = S + (ta  floor(ta))
Next a
S = S + (ta  floor(ta))
The problem with this is that you no longer get an output that is always correct...
For example: 1.2 and 1.2 should give 1.44 but instead give 1.6... I'm stumped...
sage
OkIDaN 02082005, 03:14 AM S = 0; a = 1.2 and n = 20
ta = a
a = floor(a) + 1
For 1 to a
S = S + n
S = S + (ta  floor(ta))
Next a
I'm not a php coder but I guess this line
After the first loop we will have:
S = n + (ta  floor(ta))
After the second one:
S = 2n + 2(ta  floor(ta))
After the last one:
S = (floor(ta)+1)*n + (floor(ta)+1)*(tafloor(ta)
Or even a simpler:
S = floor(ta)*n + n + floor(ta)*ta + ta  floor(ta)^2  floor(ta) =
= floor(ta)*(ta+nfloor(ta)1) + n
This one is the actual equation. So, you don't really need that loop.
Now let's check it:
ta=1.2 n=20
S = (1)*(1.2 + 20  1  1) + 20 = 19.2 (adding tafloor(ta) wont make it better)
As we see, the equation is wrong.
The problem is in this line:
S = S + (ta  floor(ta))
I don't really know how to fix this code without using multiplication. But with using multiplication it should look like:
ta = a
a = floor(a) + 1
For 1 to a
S = S + n
Next a
S = S  (1 (ta  floor(ta)))*n
And this code with multiplication is kiddish.
Sorry if I couldn't help you. It ^^ was meant to be my thoughts on that piece of code. I will perhaps append something later.
Spookster 02082005, 03:25 AM How would you do this progmatically???
I was thinking something along these lines:
Get two variables (a and n)
if a is less than n use it as the loop integer, else use n as the loop integer.
whichever is the loop integer the other is the addition integer.
Ex:
S = 0; a = 5 and n = 20
For 1 to a
S = S + n
Next a
Now if:
S = 0; a = 1.2 and n = 20
I found an interesting method which involves using the floor(a) + 1 to run the loop integer.
ta = a
a = floor(a) + 1
For 1 to a
S = S + n
S = S + (ta  floor(ta))
Next a
S = S + (ta  floor(ta))
The problem with this is that you no longer get an output that is always correct...
For example: 1.2 and 1.2 should give 1.44 but instead give 1.6... I'm stumped...
sage
This sounds very much like a homework assignment. We will not do your homework for you!!!!! :D
The funny thing is I think I had a programming assignment like this back in my first year in college.
shmoove 02082005, 08:59 AM It indeed sounds like a homework assignment. So no solution but I will give you another direction to try: think of how you calculate multiplication on paper. It only involves addition and integer multiplication (which you have already solved with only addition).
shmoove
sage45 02082005, 04:42 PM Well to the point it is sort of a homework assignment but not exactly... We are using a program called Maple which is a math program (kind of stupid if you ask me that we have to create a procedure that adds when it can multiply already, but any who...)
The procedure I created from the original looks like this:
> #3. Rewrite the inclass multiplication process, identifying and correcting any flaws.
> multiplication := proc (a,n)
> absa := 0;
> absn := 0;
> s := 0;
> if ( a < 0 ) then ta:=abs(a); absa := 1; else ta:=a; end if:
> if ( n < 0 ) then tn:=abs(n); absn := 1; else tn:=n; end if:
> if ( ta <= tn ) then
> if ( ta > floor (ta)) and ( ta < floor (ta + 1)) then tfa := floor(ta)  ta; else tfa := 0; end if:
> if ( tfa < 0 ) then tafa:=abs(tfa); else tafa:=tfa; end if:
> for i from 1 to ta do
> s := s + tn;
> s := s + tafa;
> od:
> s := s + tafa;
> if ( absn = 1 ) and ( absa = 1 ) then absa := 0; absn :=0; end if:
> if ( absn = 1 ) or ( absa = 1 ) then s := s * (1); end if:
> return s:
> end if:
> if ( ta > tn ) then
> if ( tn > floor (tn)) and ( tn < floor (tn + 1)) then tfn := floor(tn)  tn; else tfn := 0; end if:
> if ( tfn < 0 ) then tafn:=abs(tfn); else tafn:=tfn; end if:
> for i from 1 to tn do
> s := s + ta;
> s := s + tafn;
> od:
> s := s + tafn;
> if ( absn = 1 ) and ( absa = 1 ) then absa := 0; absn :=0; end if:
> if ( absn = 1 ) or ( absa = 1 ) then s := s * (1); end if:
> return s:
> end if:
> end proc:
Warning, `absa` is implicitly declared local to procedure `multiplication`
Warning, `absn` is implicitly declared local to procedure `multiplication`
Warning, `s` is implicitly declared local to procedure `multiplication`
Warning, `ta` is implicitly declared local to procedure `multiplication`
Warning, `tn` is implicitly declared local to procedure `multiplication`
Warning, `tfa` is implicitly declared local to procedure `multiplication`
Warning, `tafa` is implicitly declared local to procedure `multiplication`
Warning, `i` is implicitly declared local to procedure `multiplication`
Warning, `tfn` is implicitly declared local to procedure `multiplication`
Warning, `tafn` is implicitly declared local to procedure `multiplication`
multiplication(505,397);
multiplication(505,397);
multiplication(505,397);
multiplication(505,397);
multiplication(397,505);
multiplication(397,505);
multiplication(397,505);
multiplication(397,505);
200485
200485
200485
200485
200485
200485
200485
200485
The instructor didn't really identify as decimals being a flaw, however, the more I looked at this, the more I realized. I'm not looking for anyone to rewrite or solve this, just point me in the right direction... ;)
sage
shmoove 02082005, 04:50 PM Like I said you can replicate that algorithm you use when you do multiplication on paper. Even if the numbers are decimal, you still only use integer multiplication.
shmoove
OkIDaN 02082005, 09:38 PM Yeah, shmoove has the correct one. Nice.
Spookster 02082005, 10:44 PM We are using a program called Maple which is a math program (kind of stupid if you ask me that we have to create a procedure that adds when it can multiply already, but any who...)
sage
Oh I hated using Maple. It's great if you are fluent in using it because it can help out alot but as a beginner it is a pain to use.
sage45 02082005, 11:02 PM I found Maple to be awsome when working to prove De Morgan's Law...
not p or not q = not (p and q)
however, trying to nest if then statements in Maple was a huge pain in the buttox... :(
Shmoove, I get what your saying, however, using strict addition.
1.2 * 1.2 would not be added 1.2 at 1.2 times.
First you have to use a for loop in order to multiply by addition. Since you do not know what the decimal is, you cannot use a set decimal or corrective means.
1.2 * 1.2 = 1.2 + .24 which = 1.44
In using a for loop you can only specify whole integers. Which is why the floor is chosen. When the loop iterates you get,
1.2 + .2 which equals 1.4 the reason there is an addition sum statement on the outside of the loop is to solve the issue with the loop not adding the final time through, ultimately this loop ends up giving 1.6 as the response.
on numbers such as 1.5 or 1.25 the loop works like a charm.
I'm cursed to wait until 281 I guess... :(
sage
OkIDaN 02092005, 01:40 AM Hmm...
sage45 wrote:
Shmoove, I get what your saying, however, using strict addition.
1.2 * 1.2 would not be added 1.2 at 1.2 times.
First you have to use a for loop in order to multiply by addition. Since you do not know what the decimal is, you cannot use a set decimal or corrective means.
1.2 * 1.2 = 1.2 + .24 which = 1.44
In using a for loop you can only specify whole integers. Which is why the floor is chosen. When the loop iterates you get,
1.2 + .2 which equals 1.4 the reason there is an addition sum statement on the outside of the loop is to solve the issue with the loop not adding the final time through, ultimately this loop ends up giving 1.6 as the response.
Well, yes the loop count has to be a decimal. But, lets view the multiplication on paper:
1.2
1.4 *
 (remembering the (sum of) number of digits after the dot  two)
48
120 +

168 (placing the dot to its place)
1.68
Now lets program this (code is in perl):
$a=1.2; $b=1.6232;
#checking if the result will be negative
$negative=1 if (($a > 0 && $b < 0)  ($a < 0 && $b > 0));
#First, we have to split all of the numbers of both multipliers to their arrays.
#Dont want to deal with mods (%), so i will just change it to a string format
#first:
$st1="$a"; $st2="$b";
$st1=~s/^//; $st2=~s/^//; #removing the negative signs, if they exist
($st1, $mlten1)=("$1" . "$2", length($2)) if ($st1=~/^(\d*)\.(\d*)$/);
#The upper line assigns the $mlten variable the number of digits after the dot
#(if it exists), and removes the dot from $str1 by concatenating both of the
#sides of the dot. The same for $str2:
($st2, $mlten2)=("$1" . "$2", length($2)) if ($st2=~/^(\d*)\.(\d*)$/);
$mlten=$mlten1+$mlten2;
#now we have the sum of number of digits after the dot
#we could just expontiate 10 by 10^$mlten, but to avoid any multiplication:
$dotrem=1;
for ($f=0; $f<$mlten;$f++){
$dotrem=sage45_func($dotrem, 10);
}
#splitting the strings of numbers into arrays
@arr1=split ("", $st1); @arr2=split ("", $st2);
#prearation to the actual multiplication process (as that on paper):
@result=();
@first=@arr1; @second=@arr2;
if ($#arr2 < $#arr1){
@first=@arr2; @second=@arr1;
}
@second = reverse @second;
$first=join ("", @first);
int($first);
#The multiplication
$i=0;
foreach $n (@second){
int ($n);
#we could expontiate just by $exp=10^$i, but to avoid multiplication even this time:
if ($i == 0){
$exp=1;
}elsif ($i == 1){
$exp=10;
}else{
$exp=sage45_func($exp, 10) if ($exp > 1); #multiplication of decimals by addition
}
print "Exponent for ${i}th turn: $exp\n";
$n=sage45_func($n, $exp);
print $n . "\n";
#each time it will multiply (by addition) by 10's exponents like doing it on the paper
$temp=sage45_func($n, $first);
$res=$res+$temp;
$i++;
}
#now that we got the decimal result, we can put the do back on its place:
$res=$res/$dotrem;
$res=0$res if ($negative);
print "Multiplication of $a and $b without actual multiplication: $res\n";
sub sage45_func{
$ret=0;
($m1, $m2)=@_;
($smaller, $larger)=($m1, $m2);
($smaller, $larger)=($m2, $m1) if ($m2 < $m1);
for ($r=0; $r<$smaller; $r++){
$ret=$ret+$larger;
}
return $ret;
}
Here is the clean code in perl: http://okidan.binaryshadow.org/addition.txt
shmoove 02092005, 08:33 AM What OkIDaN said (not that I even tried reading the Perl code :D ).
shmoove
sage45 02092005, 04:27 PM sub sage45_func{
$ret=0;
($m1, $m2)=@_;
($smaller, $larger)=($m1, $m2);
($smaller, $larger)=($m2, $m1) if ($m2 < $m1);
for ($r=0; $r<$smaller; $r++){
$ret=$ret+$larger;
}[/code]
ROFL, I have a perl function named after meh... I feel so special.
Thank OkIDaN. Just one problem with maple, I don't believe that it uses arrays... :(
Shmoove, I knew you'd chime in... :p
sage
Are you sure? I did a google search for "Arrays in Maple" (I'm not familiar with the language, but refused to believe anyone writing a highlevel language could omit arrays) and this was the first result: Chapter 3: Maple Arrays and Linear Algebra (http://www.owlnet.rice.edu/~ceng303/Maple/ch3.html)
sage45 02102005, 01:04 AM Well if thats the case then everyone on the board can slap me around (cept for Spookitha)...
Sorry, my minds been twisted around the Derivative and programming a Compound Interest loop... blegh...
sage

