Square root program without sqrt or pwr

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

Join Date: May 2007
Posts: 56
Reputation: tformed is an unknown quantity at this point 
Solved Threads: 1
tformed's Avatar
tformed tformed is offline Offline
Junior Poster in Training

Square root program without sqrt or pwr

 
0
  #1
Jun 5th, 2007
is there any way one could do a square root program without sqrt or pwr functions?

Any tips?

I am not asking for code btw.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 707
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Square root program without sqrt or pwr

 
0
  #2
Jun 5th, 2007
>is there any way one could do a square root program without sqrt or pwr functions?
No, those functions are magic. There's no way you can simulate their behavior in C++ without pixie dust or phoenix feathers. One might assume that if you know how the mathematics work, you could come up with a sensible facsimile, but in reality, only compiler writers are capable of creating such things...
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Square root program without sqrt or pwr

 
0
  #3
Jun 6th, 2007
You could use a binary search algorithm. (It would be easy to name better ways, but Meh.)
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Square root program without sqrt or pwr

 
0
  #4
Jun 6th, 2007
There seems to be a rash of "do x without using the obvious" problems at the moment.

Are they all at the same school with some dim-wit teacher who thinks that learning dumb tricks is the way to teach programmers.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 56
Reputation: tformed is an unknown quantity at this point 
Solved Threads: 1
tformed's Avatar
tformed tformed is offline Offline
Junior Poster in Training

Re: Square root program without sqrt or pwr

 
0
  #5
Jun 6th, 2007
lol, point me out to these threads if you can, just in case i can point them out lol

anyways, the teacher wants us to write a program without relying on these special functions. I can do them with, but now he wants us to do without.

I do not know of any mathematical formula that will enable me to achieve this without using a square root.
I will ask him, to clarify. Will post back later.
Originally Posted by Salem View Post
There seems to be a rash of "do x without using the obvious" problems at the moment.

Are they all at the same school with some dim-wit teacher who thinks that learning dumb tricks is the way to teach programmers.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 707
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Square root program without sqrt or pwr

 
0
  #6
Jun 6th, 2007
>anyways, the teacher wants us to write a program without relying on these special functions.
Yea, we figured as much. The thing is, there's no point to it if the functions are available. That's why they're there, so you don't have to write them. Anyway, a simple pow is easy to write: Just multiply x with itself y times and you have x^y. sqrt is harder, but you can find examples and explanations all over the web. Most likely you don't care about efficiency for a class, so just pick the first one you find that works. Two relatively simple ones are binary search (like Rashakil suggested) and newton iteration.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 1,066
Reputation: Sturm is on a distinguished road 
Solved Threads: 24
Sturm's Avatar
Sturm Sturm is offline Offline
Veteran Poster

Re: Square root program without sqrt or pwr

 
0
  #7
Jun 6th, 2007
heres one (I do not know what it is called. My friend and I were having a competition who could make the fastest sqrt algorithm, so this is mine):

  1. double sqrt(double num)
  2. {
  3. double mod=1;
  4. double c=0;
  5.  
  6. for(int d=0; d<50; c+=mod, d++)
  7. if(c*c>num)
  8. {
  9. c-=mod;
  10. mod/=10;
  11. }
  12.  
  13. return c;
  14. }
"Hey ass, don't hijack my thread. This is serious." -JoshSCH
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Square root program without sqrt or pwr

 
0
  #8
Jun 6th, 2007
Obviously, my coloured in hint wasn't enough

Compare log(10000) with log(100)
Try it with a few others as well where one is the square root of the other.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Square root program without sqrt or pwr

 
0
  #9
Jun 6th, 2007
Different methods of finding the square root.

> Try it with a few others as well where one is the square root of the other.
Yes, logarithmic method seems to be a good choice.
Last edited by ~s.o.s~; Jun 6th, 2007 at 1:58 pm.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 56
Reputation: tformed is an unknown quantity at this point 
Solved Threads: 1
tformed's Avatar
tformed tformed is offline Offline
Junior Poster in Training

Re: Square root program without sqrt or pwr

 
0
  #10
Jun 7th, 2007
I keep on getting the following as an error:

'ln' undefined; assuming extern returning int

do i need to define ln? If so, what value?
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC