In article <11*********************@z14g2000cwz.googlegroups. com>,
<ra*******@gmail.com> wrote:
hello,
Is it possible to write a math library function sqrt in C without
arithmatic operators(*,+)?
can anyone give me hints to do that?
regards,
rahul
If it's in the math library it doesn't have to be implemented
in C, so if the processor has a sqrt instruction you can just
use that. That's cheating (kind of) because it's likely that the
processor itself will do operations similar to * and + in order
to execute.
exp(log(x)/2.0) is workable, but divide is an arithmetic
operator, and although you didn't specifically mention it in
your list and I can't tell from your words whether the list
is meant to be exhaustive or to exemplify. Again, exp and
log are likely to do some arithmetic operations behind
the scenes.
It's _probably_ possible to use shifts and bitwise operations
to implement an integer sqrt function. Another integer-only
solution would be something really lame like:
int sqrt(int x)
{
if (x < 4) return 1;
if (x < 9) return 2;
if (x < 16) return 3;
if (x < 25) return 4;
if (x < 36) return 5;
/* and so on, up to sqrt(INT_MAX) */
}
But perhaps you have handed in your homework by now and nothing
I have said is helpful.
--
7842++