Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 14 of 14

02082005, 01:50 AM #1
Multiplication of Decimals by addition only...
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...
sageHTML & CSS Forum Moderator
"If you don't know what you think you know, then what do you know."
R.I.P. Derrick Thomas #58
1/1/1967  2/8/2000
02082005, 03:14 AM
#2
 Join Date
 Jul 2004
 Location
 Azerbaijan
 Posts
 25
 Thanks
 0
 Thanked 0 Times in 0 Posts
I'm not a php coder but I guess this lineS = 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
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:
And this code with multiplication is kiddish.Code:ta = a a = floor(a) + 1 For 1 to a S = S + n Next a S = S  (1 (ta  floor(ta)))*n
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.
02082005, 03:25 AM
#3
 Join Date
 May 2002
 Location
 Marion, IA USA
 Posts
 6,278
 Thanks
 4
 Thanked 83 Times in 82 Posts
Originally Posted by sage45
This sounds very much like a homework assignment. We will not do your homework for you!!!!!
The funny thing is I think I had a programming assignment like this back in my first year in college.
Spookster
CodingForums Supreme Overlord
All Hail Spookster
02082005, 08:59 AM
#4
 Join Date
 Dec 2003
 Posts
 367
 Thanks
 0
 Thanked 0 Times in 0 Posts
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
02082005, 04:42 PM
#5
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:
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...Code:> #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
sage
HTML & CSS Forum Moderator
"If you don't know what you think you know, then what do you know."
R.I.P. Derrick Thomas #58
1/1/1967  2/8/2000
02082005, 04:50 PM
#6
 Join Date
 Dec 2003
 Posts
 367
 Thanks
 0
 Thanked 0 Times in 0 Posts
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
02082005, 09:38 PM
#7
 Join Date
 Jul 2004
 Location
 Azerbaijan
 Posts
 25
 Thanks
 0
 Thanked 0 Times in 0 Posts
Yeah, shmoove has the correct one. Nice.
02082005, 10:44 PM
#8
 Join Date
 May 2002
 Location
 Marion, IA USA
 Posts
 6,278
 Thanks
 4
 Thanked 83 Times in 82 Posts
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.Originally Posted by sage45
Spookster
CodingForums Supreme Overlord
All Hail Spookster
02082005, 11:02 PM
#9
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
Last edited by sage45; 02092005 at 12:21 AM.
HTML & CSS Forum Moderator
"If you don't know what you think you know, then what do you know."
R.I.P. Derrick Thomas #58
1/1/1967  2/8/2000
02092005, 01:40 AM
#10
 Join Date
 Jul 2004
 Location
 Azerbaijan
 Posts
 25
 Thanks
 0
 Thanked 0 Times in 0 Posts
The solution
Hmm...
sage45 wrote:
Well, yes the loop count has to be a decimal. But, lets view the multiplication on paper: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.
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):
Here is the clean code in perl: http://okidan.binaryshadow.org/addition.txtCode:$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; }
Last edited by OkIDaN; 02092005 at 03:25 AM. Reason: Added negative number support
02092005, 08:33 AM
#11
 Join Date
 Dec 2003
 Posts
 367
 Thanks
 0
 Thanked 0 Times in 0 Posts
What OkIDaN said (not that I even tried reading the Perl code ).
shmoove
02092005, 04:27 PM
#12
ROFL, I have a perl function named after meh... I feel so special.Originally Posted by OkIDaN
Thank OkIDaN. Just one problem with maple, I don't believe that it uses arrays...
Shmoove, I knew you'd chime in...
sage
HTML & CSS Forum Moderator
"If you don't know what you think you know, then what do you know."
R.I.P. Derrick Thomas #58
1/1/1967  2/8/2000
02092005, 04:31 PM
#13
 Join Date
 Dec 2004
 Location
 Keswick, Ontario
 Posts
 251
 Thanks
 0
 Thanked 0 Times in 0 Posts
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
02102005, 01:04 AM
#14
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
HTML & CSS Forum Moderator
"If you don't know what you think you know, then what do you know."
R.I.P. Derrick Thomas #58
1/1/1967  2/8/2000