thank u.

Recommended Answers

All 9 Replies

Well why dont you use a library function from math.h.

#include <math.h>
...
...
sqrt( 4 );

ssharish

Member Avatar for iamthwee

If this isn't a trivial question, you might want to look at using the Newton Raphson method for finding the square root.

Or other iterative methods.

The simplest way is to use Newton method:

#define TOLERANCE 0.0001

double myabs(const double x){
  if (x > 0.0)
    return x;
  return -x;
}

double mysqrt(const double x){
  double assumption = 1.0;
  if (x < 0.0)
    return 0.0;  
  while (myabs(assumption * assumption - x) > TOLERANCE){
    assumption = 0.5 * (assumption + x / assumption);
  }
  return assumption;
}
commented: Code is simple and clear! :) +2
commented: There's help and then there is doing someone's homework. Know the difference. -3

I dont want to use library function sqrt().

Pretty harsh reply by AncientDragon but a very good one...
Just google it and you will find methods and even the source code.

Pretty harsh reply by AncientDragon but a very good one...
Just google it and you will find methods and even the source code.

Yes, but well deserved. The OP gave absolutely no indication at all (in either post) of what he was looking for. We are simply left to guess.

Pretty harsh reply by AncientDragon [...]

Harsh? That is not being harsh. That's practicing at being a jerk without style and heavy on the unoriginal side of the trend.

After all, it wasn't very long ago that they used to write “RTFM”. And no; the F doesn't stand for Read The Fine Manual. Nowadays, it is some form of “include Google apathetic sentence here”. Like if there were not any other search engine in the whole Internet.

Many C programming boards count amount its assets, with one or two talented individuals, in the understanding of the intricacy of the language. By virtue of regular visits, they often answer or read the same questions time and time again developing a syndrome of cynicism or boredom, due to the lack of intelligent questions. To combat the occasional spell of this syndrome; posting witty or sharp answers, with the intent to stir up some form of self reliance in the OP is often common. Which for the most part is celebrated by their acolytes.
On the other hand, there's plenty of copycats. Creating an atmosphere of unfriendliness, and fomenting the typical judgmental post where the underling excuse its always: “You, unworthy lazy poster, if you get any answer from me it is because I am so magnanimous. Better start jumping as many hoops as I tell you and prepare for paying your dues.”

commented: Wow, sooo many big words, fun to read though. +15
Member Avatar for iamthwee

I don't agree that post #4 did anything to help the OP. It just gave him the answer on the plate...

commented: Yep, the ultimate in spoon feeding. +20
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.