![]() |
| | #31 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,768
| |
| brewbuck is offline | |
| | #32 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,768
| I disagree. What if it had read index *= 2? Is that a "sneaky way" to avoid dividing by 0.5? It's just a multiplication. The fact that the number is less than one doesn't really mean anything. |
| brewbuck is offline | |
| | #33 |
| Woof, woof! Join Date: Mar 2007 Location: Australia
Posts: 3,295
| Perhaps an even greater challenge is no division or multiplication operators ![]() But then I suppose you'd define some macros that multiply by addition... *sigh* |
| zacs7 is offline | |
| | #34 |
| Malum in se Join Date: Apr 2007
Posts: 3,188
| nah, if I couldnt use multiplication, Id use bit shifting
__________________ Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off. |
| abachler is offline | |
| | #35 |
| and the hat of marbles Join Date: May 2002 Location: Lund, Sweden
Posts: 2,041
| Yeah, but a correctly implemented Newton's method should ceonverge much faster than that.
__________________ Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling |
| Sang-drax is offline | |
| | #36 |
| Malum in se Join Date: Apr 2007
Posts: 3,188
| a solution that meets all the requirements of the rules, but perhaps not the spirit of the contest - Code: double sqrtfunc(double Input){
__asm FLD Input
__asm FSQRT
__asm FSTP Input
return Input;
}
__________________ Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off. Last edited by abachler; 06-07-2007 at 09:08 AM. |
| abachler is offline | |
| | #37 |
| Massively Single Player Join Date: May 2007 Location: Buffalo, NY
Posts: 141
| Alas, since templates can't take doubles, I can't quite beat that, but... Code: template <int r, int x>
struct SqRootCalc
{
const static int value = x * x == r ? x : SqRootCalc<r, x - 1>::value;
};
template <int r>
struct SqRootCalc<r, 1>
{
const static int value = -1;
};
template <int r>
struct SqRoot
{
const static int value = SqRootCalc<r, r / 2>::value;
};
template <>
struct SqRoot<1>
{
const static int value = 1;
};
template <>
struct SqRoot<0>
{
const static int value = 0;
};
Usage: Code: cout << SqRoot<9>::value << endl;
__________________ There is no greater sign that a computing technology is worthless than the association of the word "solution" with it. |
| AverageSoftware is offline | |
| | #38 |
| and the hat of marbles Join Date: May 2002 Location: Lund, Sweden
Posts: 2,041
| Wow, amazing, except that you are using division.
__________________ Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling |
| Sang-drax is offline | |
| | #39 |
| Registered User Join Date: Jan 2005
Posts: 7,252
| >> Wow, amazing, except that you are using division. But there are no calls to the division operator at runtime, so its close. |
| Daved is offline | |
| | #40 | |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,698
| Quote:
![]() That code's pretty amazing, though. I would never have thought of it.
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, nort, etc. | |
| dwks is offline | |
| | #41 |
| Registered User Join Date: Jan 2005
Posts: 7,252
| >> Besides, you can just change it to *0.5 and you'll be just fine. Except that you can't unfortunately. I believe everything must be integral. |
| Daved is offline | |
| | #42 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,698
| This doesn't work? Code: static_cast<int>(r * .5)
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, nort, etc. |
| dwks is offline | |
| | #43 |
| Registered User Join Date: Jan 2005
Posts: 7,252
| It does, but that's different than AverageSoftware's template metaprogramming based solution. Yours is a run-time operation, but the templated stuff is compile time, which I believe must be integral (which is also why double can't be used instead of int). |
| Daved is offline | |
| | #44 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,698
| Oh, I see. Well, the r/2 could be just r or r-1; it would just be less efficient and involve more "recursion".
__________________ dwk Seek and ye shall find. quaere et invenies. "Simplicity does not precede complexity, but follows it." -- Alan Perlis "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra "The only real mistake is the one from which we learn nothing." -- John Powell Other boards: DaniWeb, TPS Unofficial Wiki FAQ: cpwiki.sf.net My website: http://dwks.theprogrammingsite.com/ Projects: codeform, xuni, atlantis, nort, etc. |
| dwks is offline | |
| | #45 |
| Registered User Join Date: Jan 2005
Posts: 7,252
| True. And now that I look closer I see that it only does one division anyway, which is allowed in the rules. |
| Daved is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| program to calculate the square root | saszew | C Programming | 7 | 10-28-2008 12:53 PM |
| Forced moves trouble!! | Zishaan | Game Programming | 0 | 03-27-2007 06:57 PM |
| Bisection Method function value at root incorrect | mr_glass | C Programming | 3 | 11-10-2005 09:10 AM |
| Square Root ?!? | Tm687 | C++ Programming | 1 | 02-29-2004 04:38 PM |
| Templated Binary Tree... dear god... | Nakeerb | C++ Programming | 15 | 01-17-2003 02:24 AM |