Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 08-19-2004
xddxogm3
i would like to build off this post and ask if it is possible to write a division funciton using only +,-,while,do,for,if,else?
I like the witches math function, and saw the answer for the prior request at that point.
Code:

```int multiply(int a, int b) {         int ret = 0;         for (int i = 0; i < a; i++)                 ret += b;         return ret; }```
How could you modify that to do division?
The only way i can think of would be to turn the value into a string array by using modulus by 10 or something like that. Is there a simple way to calculate this?
• 08-19-2004
pianorain
Actually, that code to multiply only works with two positive integers. For a more general solution:
Code:

```int multiply(int a, int b) {         int ret = 0;         bool neg = false;         if (a<0) {                 a = -a;                 neg = !neg;         }         for (int i = 0; i < a; i++)                 ret += b;         if (neg)                 ret = -ret;         return ret; }```
To divide, you more or less reverse the process, except 0 is a special case.
Code:

```int divide (int a, int b) {   int ret = 0;   bool neg = false;   if (a == 0)         return 0;   if (b == 0)         return 0;   if (a<0) {         a = -a;         neg = !neg;   }   if (b<0) {         b = -b;         neg = !neg;   }   for(;a >= b;ret++)       a -= b;   if (neg)         ret = -ret;   return ret; }```
Of course, note that dividing by 0 returns 0 instead of undefined. You'd probably want to throw an exception if you were going to use this for real.
• 08-20-2004
sean
You would add something like this to the very beginning of the function

Code:

`if (a==0) ThrowException(DivideByZero)`
Except that looks like a function you'd find in Java...
• 08-20-2004
laserlight
Quote:

that code to multiply only works with two positive integers
For 2 non-negative integers, actually.
Which makes sense, since the domain of the factorial function is the set of non-negative integers.

I was hoping that adzza69 would see that if it was possible to implement one's own multiplication function, that it would be possible to 'manually inline' that function into one's own function for calculating factorials in this strange way.

I think that using a form of linear interpolation would be faster than pianorain's algorithm, but then that would also be more difficult to implement, and choosing initial values would be tricky.
• 08-20-2004
xddxogm3
pianorain,

please correct me if i'm wrong, but looking at your solution for division.
It seems that we do not concern ourselves with the remainder.
Am I reading this correctly? I guess that would match the current division process w/o modules.
I know nothing about assembley, so would that language allow the use of pointers?
then we could set the pointers to the division value and the remainder.
• 08-20-2004
pianorain
Aye, that's correct. Integer division returns the quotient without the remainder.
• 08-20-2004
sean
You can use % (modulus) to get just the remainder.
• 08-20-2004
xddxogm3
any assembly guru know if pointers or modules is allowed in that language.
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12