943,985 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 29895
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 5th, 2007
0

Square root program without sqrt or pwr

Expand Post »
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.
Similar Threads
Reputation Points: 21
Solved Threads: 1
Junior Poster in Training
tformed is offline Offline
58 posts
since May 2007
Jun 5th, 2007
0

Re: Square root program without sqrt or pwr

>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...
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 6th, 2007
2

Re: Square root program without sqrt or pwr

You could use a binary search algorithm. (It would be easy to name better ways, but Meh.)
Team Colleague
Reputation Points: 1135
Solved Threads: 172
Super Senior Demiposter
Rashakil Fol is offline Offline
2,479 posts
since Jun 2005
Jun 6th, 2007
0

Re: Square root program without sqrt or pwr

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jun 6th, 2007
0

Re: Square root program without sqrt or pwr

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.
Click to Expand / Collapse  Quote originally posted by Salem ...
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.
Reputation Points: 21
Solved Threads: 1
Junior Poster in Training
tformed is offline Offline
58 posts
since May 2007
Jun 6th, 2007
0

Re: Square root program without sqrt or pwr

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 6th, 2007
0

Re: Square root program without sqrt or pwr

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):

c++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 343
Solved Threads: 24
Veteran Poster
Sturm is offline Offline
1,067 posts
since Jan 2007
Jun 6th, 2007
0

Re: Square root program without sqrt or pwr

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jun 6th, 2007
0

Re: Square root program without sqrt or pwr

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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,872 posts
since Jun 2006
Jun 7th, 2007
0

Re: Square root program without sqrt or pwr

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?
Reputation Points: 21
Solved Threads: 1
Junior Poster in Training
tformed is offline Offline
58 posts
since May 2007

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: Nested If and Looping
Next Thread in C++ Forum Timeline: can anyone help me with my c++ homework?





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


Follow us on Twitter


© 2011 DaniWeb® LLC