473,508 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

function without arithematic operator

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

Nov 15 '05 #1
7 2209
ra*******@gmail.com writes:
Is it possible to write a math library function sqrt in C without
arithmatic operators(*,+)?


There is no good reason to do so.
--
"If I've told you once, I've told you LLONG_MAX times not to
exaggerate."
--Jack Klein
Nov 15 '05 #2
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?


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.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Nov 15 '05 #3
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++
Nov 15 '05 #4
"Anonymous 7843" <an******@example.com> wrote in message
news:uhPHe.27079$HV1.5748@fed1read07...
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.


The code below uses an iterative algorithm to calculate the sqrt of a
double, without any addition or multiplication operations (although
subtraction and division are both used). Comments are welcome.

#include <stdio.h>
#include <stdlib.h>

#define FLIP_SGN(x) -x

double my_abs(double x) {
if(x<0)
return FLIP_SGN(x);
else
return x;
} /* my_abs */

double my_sqrt(double x) {
double ans=0, guess=x, error=1e-12, error2;
int count=0;
error2 = my_abs(ans-guess);

while(error2>error){
ans = (guess-x/FLIP_SGN(guess)) / 2.0; /* Newton's method approximation
*/
error2 = my_abs(ans-guess);
guess = ans;
count--;
} /* while */
printf("Calculated in %d iterations\n", FLIP_SGN(count));

return ans;
} /* my_sqrt */

int main(int argc, char **argv) {
int imag=0;
char *str_test;
double x, ans;

if(argc>1) {
x = strtod(argv[1], &str_test);
if(argv[1]!=str_test) {
if(x<0) {
imag = 1;
x = FLIP_SGN(x);
} /* if */
ans = my_sqrt(x);
if(imag)
printf("sqrt(%f) = %.10fi\n", FLIP_SGN(x), ans);
else
printf("sqrt(%f) = %.10f\n", x, ans);
} /* if */
else
printf("ERROR: Improper input provided; exiting!\n");
} /* if */
else
printf("ERROR: No input provided; exiting!\n");

return 0;
} /* main */
gcc -Wall -ansi -pedantic -lm -o my_sqrt my_sqrt.c
my_sqrt -45.9314

Calculated in 8 iterations
sqrt(-45.931400) = 6.7772708371i

-Charles
Nov 15 '05 #5
On 2 Aug 2005 07:07:57 -0700, 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

exp(log(n)/2);

is probably one of the least efficient ways.

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 15 '05 #6
Kenneth Brody wrote:

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?


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
Nov 15 '05 #7
You can also use SQR function to do that.(If SQR is not an arithmetic
operator)
while not (answer)
{
i++;
if(i>number){
answer=true;
}
}

you can also calculate the decimals with a small improvement.

Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
10137
by: Christophe Barbe | last post by:
I posted a few days ago about the same problem but was not very clear. So here is my second take at it. Basically with GCC 3.3.2, I can't compile the example from the C++ FAQ Lite available...
6
4102
by: blueblueblue2005 | last post by:
here is a friend function of Class Array, which has two private data member: int size, int *ptr // Array's public member function to return size int getSize() const { return size; } friend...
10
2059
by: sjbrown8 | last post by:
I have the piece of code below, and when i try compiling with the line g++ 753075304.cpp I get the following error message: 753075304.cpp: In function 'int main()': 753075304.cpp:29: error:...
3
18790
by: gugdias | last post by:
I'm coding a simple matrix class, which is resulting in the following error when compiling with g++ 3.4.2 (mingw-special): * declaration of `operator/' as non-function * expected `;' before '<'...
21
4087
by: dragoncoder | last post by:
Consider the following code. #include <stdio.h> int main() { int i =1; printf("%d ,%d ,%d\n",i,++i,i++); return 0; }
6
2576
by: Matthew Cook | last post by:
I would like to overload the unary minus operator so that I can negate an instance of a class and pass that instance to a function without creating an explicit temporary variable. Here is an...
28
2817
by: Jess | last post by:
Hello, It is said that if I implement a "swap" member function, then it should never throw any exception. However, if I implement "swap" non- member function, then the restriction doesn't...
13
3934
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
26
4823
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7324
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7382
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
5052
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3193
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3181
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1556
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.