help

L

luvraghu

Hi,

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

Thank You.


Regards,
New
 
R

Richard Heathfield

(e-mail address removed) said:
Hi,

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

You don't need any of those character constants. Use the / operator
instead. Note that / and '/' are not the same thing.

The arithmetic operators are there for a good reason. Use them when using
them is the simple, sensible thing to do.
 
R

Ravishankar S

Hi,

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

Thank You.


Regards,
New

hint: division of ints can be done by repeated subtraction.
 
K

karthikbalaguru

Hi,

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

Hint - Mix Bit shifting techniques and Tricks using Modulo operations.

Karthik Balaguru
 
U

user923005

Hi,

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

Since the problem is totally impractical, how about an equally
impractical solution:

Write a program that writes a program that has every possible pair of
numerator and denominator allowed by your CPU precalculated.

Your function will simply look up the precalculated answer. Make sure
to write a separate function for every single pair and email the
answer to your instructor. I think it may discourage the use of this
particular assignment in the future.

Your main driver function will have a huge switch of all possible
numerators, and within each numerator it will have another switch with
every possible denominator. The individual case will call the
appropriate function.

If he wants you to use double, no problem. There are only 2^64th
possible values in an 8 byte double {which is clearly finite} and so
your code will probably be a little on the large size.

Even if the program takes unsigned char as the inputs it will still
have a "fun" size.
 
R

Ravishankar S

ahh..missed the '-'. only other op now to use is %.



I need to use only bitwise operators....

Kunth's book Fundamental Algorithms or Seminumerical algorithms may have the
answer.
 
U

user923005

I need to use only bitwise operators....

Kunth's book Fundamental Algorithms or Seminumerical algorithms may have the
answer.

I doubt it.
Program D on page 273 "Division of nonnegative integers" uses both
multiplication and subtraction to compute division.

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))
return 0;
else
return 1;
if (d1 < d2)
if ((d2 - d1) < fabs(d2 * DBL_EPSILON))
return 0;
else
return -1;
return 0;
}

double divide(double x, double y)
{
double y1;
double y2 = DBL_MAX;
double delta;
int i;
y1 = 0.001; /* A horrible estimate just so it will
be fun
* to watch in the debugger. */
for (i = 0; i < 100; i++) {
y1 *= (2 - y * y1);
if (double_compare(y2, y1) == 0)
break;
y2 = y1;
}
return x * y1;
}

double divide2(double x, double y)
{
return pow(10.0, log10(x) - log10(y));
}

int main(void)
{
printf("result of division of %f by %f is:%f\n", 1.0, 2.0,
divide(1.0, 2.0));
printf("result of division of %f by %f is:%f\n", 1.0, 17.0,
divide(1.0, 17.0));
printf("result of division of %f by %f is:%f\n", 3.0, 19.0,
divide(3.0, 19.0));
printf("result of division of %f by %f is:%f\n", 19.0, 3.0,
divide(19.0, 3.0));
printf("result of division of %f by %f is:%f\n", 12.5, 13.5,
divide(12.5, 13.5));

printf("result of division of %f by %f is:%f\n", 1.0, 2.0,
divide2(1.0, 2.0));
printf("result of division of %f by %f is:%f\n", 1.0, 17.0,
divide2(1.0, 17.0));
printf("result of division of %f by %f is:%f\n", 3.0, 19.0,
divide2(3.0, 19.0));
printf("result of division of %f by %f is:%f\n", 19.0, 3.0,
divide2(19.0, 3.0));
printf("result of division of %f by %f is:%f\n", 12.5, 13.5,
divide2(12.5, 13.5));

return 0;
}
 
U

user923005

Improved icky[tm] divider:

#include <stdio.h>
#include <math.h>
#include <float.h>
/*
Icky[tm] division. Use at your own peril.
*/
int double_compare(double d1, double d2)
{
if (d1 > d2)
if ((d1 - d2) < fabs(d1 * DBL_EPSILON))
return 0;
else
return 1;
if (d1 < d2)
if ((d2 - d1) < fabs(d2 * DBL_EPSILON))
return 0;
else
return -1;
return 0;
}

double divide(double x, double y)
{
double y1;
double y2 = DBL_MAX;
double delta;
int i;
y1 = 0.001; /* A horrible estimate just so it will
* be fun to watch in the debugger. */
for (i = 0; i < 100; i++) {
y1 *= (2 - y * y1);
if (double_compare(y2, y1) == 0)
break;
y2 = y1;
}
return x * y1;
}

double divide2(double x, double y)
{
return pow(10.0, log10(x) - log10(y));
}

double divide3(double x, double y)
{
return exp(log(x) - log(y));
}

int main(void)
{
char sx[25];
char sy[25];
double x,y;
printf("result of Newton division of %f by %f is:%f\n", 1.0, 2.0,
divide(1.0, 2.0));
printf("result of Newton division of %f by %f is:%f\n", 1.0, 17.0,
divide(1.0, 17.0));
printf("result of Newton division of %f by %f is:%f\n", 3.0, 19.0,
divide(3.0, 19.0));
printf("result of Newton division of %f by %f is:%f\n", 19.0, 3.0,
divide(19.0, 3.0));
printf("result of Newton division of %f by %f is:%f\n", 12.5,
13.5, divide(12.5, 13.5));

printf("result of Naperian log division of %f by %f is:%f\n", 1.0,
2.0, divide2(1.0, 2.0));
printf("result of Naperian log division of %f by %f is:%f\n", 1.0,
17.0, divide2(1.0, 17.0));
printf("result of Naperian log division of %f by %f is:%f\n", 3.0,
19.0, divide2(3.0, 19.0));
printf("result of Naperian log division of %f by %f is:%f\n",
19.0, 3.0, divide2(19.0, 3.0));
printf("result of Naperian log division of %f by %f is:%f\n",
12.5, 13.5, divide2(12.5, 13.5));

printf("result of Natural log division of %f by %f is:%f\n", 1.0,
2.0, divide3(1.0, 2.0));
printf("result of Natural log division of %f by %f is:%f\n", 1.0,
17.0, divide3(1.0, 17.0));
printf("result of Natural log division of %f by %f is:%f\n", 3.0,
19.0, divide3(3.0, 19.0));
printf("result of Natural log division of %f by %f is:%f\n", 19.0,
3.0, divide3(19.0, 3.0));
printf("result of Natural log division of %f by %f is:%f\n", 12.5,
13.5, divide3(12.5, 13.5));

puts("Your turn, give me x:");
fgets(sx, sizeof sx, stdin);
x = atof(sx);
retry:
puts("Give me y {must not be 0}:");
fgets(sy, sizeof sy, stdin);
y = atof(sy);
if (y == 0) goto retry;
printf("result of Newton division of %f by %f is:%f\n", x, y,
divide(x, y));

printf("result of Naperian log division of %f by %f is:%f\n", x,
y, divide2(x, y));

printf("result of Natural log division of %f by %f is:%f\n", x, y,
divide3(x, y));
return 0;
}
 
F

Flash Gordon

I need to use only bitwise operators....

OK, here is a function using only bitwise operators to divide a number
with any number.

int div_zero_by_any(int num)
{
return 0 | 0;
}

You can use this template to write functions for any other number you want.

Or if this is homework you could do your own homework.
Or if this is not homework you could stop using stupid restrictions.
 
B

Bart C

Hi,

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

If ++ and -- are allowed then the following might work. But probably not
what your teacher had in mind. It can also be slow.

int divide(int a,int b)
{
int bb,d,r=0;

if (a<0) a=abs(a); /* negative results left as an exercise */
if (b<0) b=abs(b);

if (a==0) return 0;
if (b==0)return 0; /* divide-by-zero ignored */

d=0;

while (1)
{
bb=b;
r=0;
while (bb)
{
--a;
--bb;
if (a==0)
if (a==bb) return d+1; else return d;
++r; /* calculate remainder although not returned */
};
++d;
};

return 0;
}
 
B

Bart C

Richard said:
(e-mail address removed) said:
...
The arithmetic operators are there for a good reason. Use them when
using them is the simple, sensible thing to do.

This is obviously an assignment to help develop lateral thinking but, if a
new numeric type has been created for which the 4 main arithmetic ops do not
yet exist, there might well be a use for this.

Bart
 
J

Joachim Schmitz

Bart said:
If ++ and -- are allowed then the following might work. But probably
not what your teacher had in mind. It can also be slow.

int divide(int a,int b)
{
int bb,d,r=0;

if (a<0) a=abs(a); /* negative results left as an exercise */
if (b<0) b=abs(b);

if (a==0) return 0;
if (b==0)return 0; /* divide-by-zero ignored */

d=0;

while (1)
{
bb=b;
r=0;
while (bb)
{
--a;
--bb;
if (a==0)
if (a==bb) return d+1; else return d;
beep, failed
++r; /* calculate remainder although not returned */
};
++d;
};

return 0;
}

Bye, Jojo
 
B

Bart C

Joachim said:
beep, failed

Dangling else problem, you mean?

Ok. But it seemed to work on 2 compilers and for a,b up to 1000 each, or at
least gave the same results as a/b. (My test code not included in post)

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,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top