943,528 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2044
  • C++ RSS
Apr 13th, 2008
0

Square root??

Expand Post »
Hey i've made a simple calculator, but i was thinking about adding a square root option..

But is there a actual way to do this or would i have to create my own??

PLEASE REPLY A.S.A.P
Reputation Points: 25
Solved Threads: 4
Junior Poster
Black Magic is offline Offline
177 posts
since Apr 2008
Apr 13th, 2008
0

Re: Square root??

Read this
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,945 posts
since Aug 2005
Apr 13th, 2008
0

Re: Square root??

Thanks but i think i'll leave that option out
Reputation Points: 25
Solved Threads: 4
Junior Poster
Black Magic is offline Offline
177 posts
since Apr 2008
Apr 13th, 2008
0

Re: Square root??

How about the sqrt function from cmath?
http://www.cplusplus.com/reference/c...math/sqrt.html
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,371 posts
since Jan 2008
Apr 13th, 2008
0

Re: Square root??

You can try to bruteforce it out and just increment by 0.01 maybe that will work out.
But the answer wouldnt be exact.
So u can try in for an approx val.

Then test it up. If the value you got. is squared is lesser than the main number of which you should find the square root then increment. But if the the square of the value increases take the previous number as the approximate square root. So you will only be missing the minutest details
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Apr 13th, 2008
0

Re: Square root??

I've looked at websites etc, but tbh is it worth it?
Reputation Points: 25
Solved Threads: 4
Junior Poster
Black Magic is offline Offline
177 posts
since Apr 2008
Apr 13th, 2008
0

Re: Square root??

I'm confused, is the sqrt function somehow deficient?
Reputation Points: 98
Solved Threads: 22
Posting Whiz in Training
sarehu is offline Offline
269 posts
since Oct 2007
Apr 13th, 2008
0

Re: Square root??

A simple method for calculating square root of a number "a" is given by newton iteration as

C++ Syntax (Toggle Plain Text)
  1. for(int i=0;i<10;i++)
  2. {
  3. xn=.5*(x0+(a/x0));
  4. x0=xn;
  5. }

where x0 is initialized to 1. Use float comparisons and loop till xn equals x0 here I have used a loop for simplicity. Again why cant you use sqrt()?
Reputation Points: 46
Solved Threads: 24
Posting Whiz in Training
hammerhead is offline Offline
248 posts
since May 2006
Apr 14th, 2008
0

Re: Square root??

I never said i cant use sqrt() i just said i dont know if it's worth it
Reputation Points: 25
Solved Threads: 4
Junior Poster
Black Magic is offline Offline
177 posts
since Apr 2008
Apr 14th, 2008
0

Re: Square root??

Click to Expand / Collapse  Quote originally posted by hammerhead ...
A simple method for calculating square root of a number "a" is given by newton iteration as

C++ Syntax (Toggle Plain Text)
  1. for(int i=0;i<10;i++)
  2. {
  3. xn=.5*(x0+(a/x0));
  4. x0=xn;
  5. }

where x0 is initialized to 1. Use float comparisons and loop till xn equals x0 here I have used a loop for simplicity. Again why cant you use sqrt()?
Sorry I forgot to mention that xn will give you the square root. You can initialize to any other number also.
Reputation Points: 46
Solved Threads: 24
Posting Whiz in Training
hammerhead is offline Offline
248 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Windows API - Use QT Now?
Next Thread in C++ Forum Timeline: Hi,Can Anybody give me some addvice please





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC