Square root??

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 177
Reputation: Black Magic is an unknown quantity at this point 
Solved Threads: 4
Black Magic's Avatar
Black Magic Black Magic is offline Offline
Junior Poster

Square root??

 
0
  #1
Apr 13th, 2008
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
C Plus Plus Coder.
Fourteen Years Of Age
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Square root??

 
0
  #2
Apr 13th, 2008
Read this
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 177
Reputation: Black Magic is an unknown quantity at this point 
Solved Threads: 4
Black Magic's Avatar
Black Magic Black Magic is offline Offline
Junior Poster

Re: Square root??

 
0
  #3
Apr 13th, 2008
Thanks but i think i'll leave that option out
C Plus Plus Coder.
Fourteen Years Of Age
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Square root??

 
0
  #4
Apr 13th, 2008
How about the sqrt function from cmath?
http://www.cplusplus.com/reference/c...math/sqrt.html
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 672
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Square root??

 
0
  #5
Apr 13th, 2008
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
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 177
Reputation: Black Magic is an unknown quantity at this point 
Solved Threads: 4
Black Magic's Avatar
Black Magic Black Magic is offline Offline
Junior Poster

Re: Square root??

 
0
  #6
Apr 13th, 2008
I've looked at websites etc, but tbh is it worth it?
C Plus Plus Coder.
Fourteen Years Of Age
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 269
Reputation: sarehu is on a distinguished road 
Solved Threads: 22
sarehu's Avatar
sarehu sarehu is offline Offline
Posting Whiz in Training

Re: Square root??

 
0
  #7
Apr 13th, 2008
I'm confused, is the sqrt function somehow deficient?
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 248
Reputation: hammerhead is an unknown quantity at this point 
Solved Threads: 24
hammerhead's Avatar
hammerhead hammerhead is offline Offline
Posting Whiz in Training

Re: Square root??

 
0
  #8
Apr 13th, 2008
A simple method for calculating square root of a number "a" is given by newton iteration as

  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()?
There are 10 types of people in the world, those who understand binary and those who don't.

All generalizations are wrong. Even this one.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 177
Reputation: Black Magic is an unknown quantity at this point 
Solved Threads: 4
Black Magic's Avatar
Black Magic Black Magic is offline Offline
Junior Poster

Re: Square root??

 
0
  #9
Apr 14th, 2008
I never said i cant use sqrt() i just said i dont know if it's worth it
C Plus Plus Coder.
Fourteen Years Of Age
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 248
Reputation: hammerhead is an unknown quantity at this point 
Solved Threads: 24
hammerhead's Avatar
hammerhead hammerhead is offline Offline
Posting Whiz in Training

Re: Square root??

 
0
  #10
Apr 14th, 2008
Originally Posted by hammerhead View Post
A simple method for calculating square root of a number "a" is given by newton iteration as

  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.
There are 10 types of people in the world, those who understand binary and those who don't.

All generalizations are wrong. Even this one.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC