Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

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


Report this thread to moderator Post Follow-up to this message
Old Post
rahul8143@gmail.com
08-02-05 02:01 PM


Re: function without arithematic operator
rahul8143@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

Report this thread to moderator Post Follow-up to this message
Old Post
Ben Pfaff
08-02-05 09:59 PM


Re: function without arithematic operator
rahul8143@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:ThisIsASpamTrap@gmail.com>



Report this thread to moderator Post Follow-up to this message
Old Post
Kenneth Brody
08-02-05 09:59 PM


Re: function without arithematic operator
In article <1122991677.514736.32910@z14g2000cwz.googlegroups.com>,
<rahul8143@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++

Report this thread to moderator Post Follow-up to this message
Old Post
Anonymous 7843
08-02-05 09:59 PM


Re: function without arithematic operator
"Anonymous 7843" <anon7843@example.com> wrote in message
news:uhPHe.27079$HV1.5748@fed1read07...
> In article <1122991677.514736.32910@z14g2000cwz.googlegroups.com>,
>  <rahul8143@gmail.com> wrote: 
>
> 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



Report this thread to moderator Post Follow-up to this message
Old Post
Charles M. Reinke
08-02-05 09:59 PM


Re: function without arithematic operator
On 2 Aug 2005 07:07:57 -0700, rahul8143@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
removebalmerconsultingthis@att.net

Report this thread to moderator Post Follow-up to this message
Old Post
Alan Balmer
08-02-05 09:59 PM


Re: function without arithematic operator
Kenneth Brody wrote:
>
> rahul8143@gmail.com wrote: 
>
> 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

Report this thread to moderator Post Follow-up to this message
Old Post
pete
08-03-05 02:59 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

C archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 02:23 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.