help

S

spaglia

Hi,

Can anyone please give me a hint/logic to divide a number with any
number without using '/' '+' '*' '-'.

Thank You.

Regards,
New

I think this will do it. It handles unsigned operands and /0 is not
special cased (assumes 32-bit integers):

typedef struct {unsigned quot; unsigned rem;} udiv_t;
udiv_t udiv(unsigned divdnd, unsigned divisor)
{
udiv_t res;

unsigned rem = 0;
unsigned bits = 32;
do {
// shift dividend left into remainder
rem = (rem << 1) | (divdnd >> 31);
divdnd <<= 1;

// if ramainder larger than divisor then
// correct and add 1 to dividend
if (rem >= divisor)
{
// rem -= divisor
unsigned cnt = divisor;
while (cnt--) rem--;
divdnd |= 1;
}
}
while (--bits);

res.quot = divdnd;
res.rem = rem;

return res;
}

-Steve
 
C

CBFalconer

spaglia said:
I think this will do it. It handles unsigned operands and /0 is
not special cased (assumes 32-bit integers):

The use of "--" or "++" involves the use of '-' or '+'. At any
rate, why spend time on such a foolish project?
 
S

spaglia

The use of "--" or "++" involves the use of '-' or '+'.  At any
rate, why spend time on such a foolish project?

Right, and comparisons are really subtractions and a logical or is
really addition without carry, etc.

I'm guessing this is a school project of some kind.

Steve
 
B

Bart C

CBFalconer said:
The use of "--" or "++" involves the use of '-' or '+'.

'+' could mean the character +, or the operator symbol +, the latter seems
more logical; the OP hasn't clarified. (And disallowing the '*' character
would make comments difficult.)

And whether ++ may or not involve + at some implementation level is
irrelevant; otherwise the entire exercise done correctly could implement the
divide / operator and therefore would be disqualified!
At any rate, why spend time on such a foolish project?

In my case (newish to C) it's a useful exercise.
 
A

Army1987

luvraghu said:
Hi,

Can anyone please give me a hint/logic to divide a number with any
number without using '/' '+' '*' '-'.

For integers:
#include <stdlib.h>
....
int result;
div_t q;
q = div(numer, denom);
result = q.quot;

For floating point (C99 only):
#include <math.h>
#include <limits.h>
result = fma(numer, pow(denom, copysign(1, cos(3)), 0);
 
A

Army1987

user923005 said:
Here are some other ways to divide without using division that are
equally horrible:

#include <stdio.h>
#include <math.h>
#include <float.h>

int double_compare(double d1, double d2)
{
if (d1 > d2)
if ((d1 - d2) < fabs(d1 * DBL_EPSILON))
You can't use - or *, either.
 
A

Army1987

Army1987 said:
For integers:
#include <stdlib.h>
...
int result;
div_t q;
q = div(numer, denom);
result = q.quot;

For floating point (C99 only):
#include <math.h>
#include <limits.h>
result = fma(numer, pow(denom, copysign(1, cos(3)), 0);
or even... copysign(1, EOF)
, (provided <stdio.h> has been #included) of course...
 
C

Chris Dollin

Bart C wrote:
In my case (newish to C) it's a useful exercise.

I think there are /many/ other things about C that would
make more useful initial exercises than re-implementing
arithmetic.

Do you not?
 
B

Bart C

Chris Dollin said:
I think there are /many/ other things about C that would
make more useful initial exercises than re-implementing
arithmetic.

Do you not?

Maybe. The OPs tutor (if s/he exists) seemed to think there was a use. Does
it matter? The learning is the important thing. And the limitation imposed
gave an added dimension. And posting code on c.l.c so that it could possibly
be savaged to pieces was an extra kick.

Bart
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top