Re: function without arithematic operator



Kenneth Brody wrote:
>
> rahul8143@xxxxxxxxx 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?
>
> Use division and subtraction instead. :-)
>
> Of course, there are only two reasons to do so that I can think of:
>
> (1) A nonsensical homework assignment.
> (2) A troll.

#include <errno.h>
#include <math.h>

double sqrt(double x)
{
if (x > 0) {
const double a = x;
double b = a / 2 - -0.5;

do {
x = b;
b = (a / x - -x) / 2;
} while (x > b);
} else {
if (0 > x) {
errno = EDOM;
x = -HUGE_VAL;
}
}
return x;
}

--
pete
.



Relevant Pages