Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    Super Moderator sage45's Avatar
    Join Date
    May 2002
    Posts
    1,060
    Thanks
    0
    Thanked 13 Times in 13 Posts

    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...

    -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

  • #2
    New Coder
    Join Date
    Jul 2004
    Location
    Azerbaijan
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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)*(ta-floor(ta)
    Or even a simpler:
    S = floor(ta)*n + n + floor(ta)*ta + ta - floor(ta)^2 - floor(ta) =
    = floor(ta)*(ta+n-floor(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 ta-floor(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:
    Code:
    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.

  • #3
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by sage45
    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!!!!!

    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

  • #4
    Regular Coder
    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

  • #5
    Super Moderator sage45's Avatar
    Join Date
    May 2002
    Posts
    1,060
    Thanks
    0
    Thanked 13 Times in 13 Posts
    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:

    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
    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-
    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

  • #6
    Regular Coder
    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

  • #7
    New Coder
    Join Date
    Jul 2004
    Location
    Azerbaijan
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah, shmoove has the correct one. Nice.

  • #8
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by sage45
    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.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #9
    Super Moderator sage45's Avatar
    Join Date
    May 2002
    Posts
    1,060
    Thanks
    0
    Thanked 13 Times in 13 Posts
    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; 02-09-2005 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

  • #10
    New Coder
    Join Date
    Jul 2004
    Location
    Azerbaijan
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    The solution

    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):

    Code:
    $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
    Last edited by OkIDaN; 02-09-2005 at 03:25 AM. Reason: Added negative number support

  • #11
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    What OkIDaN said (not that I even tried reading the Perl code ).

    shmoove

  • #12
    Super Moderator sage45's Avatar
    Join Date
    May 2002
    Posts
    1,060
    Thanks
    0
    Thanked 13 Times in 13 Posts
    Quote Originally Posted by OkIDaN
    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...

    -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

  • #13
    cfc
    cfc is offline
    Regular Coder
    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 high-level language could omit arrays) and this was the first result: Chapter 3: Maple Arrays and Linear Algebra

  • #14
    Super Moderator sage45's Avatar
    Join Date
    May 2002
    Posts
    1,060
    Thanks
    0
    Thanked 13 Times in 13 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •