Powers in C++

Started by
14 comments, last by Ekim_Gram 20 years, 5 months ago
quote:Original post by Lektrix
Yes, that looks familiar, yet surely the #if preprocessor directive would be something more like this?

#if _MSC_VER && _MSC_VER <= 1200



#if defined(_MSC_VER) && _MSC_VER <= 1200

Non-VC++ compilers probably won''t define _MSC_VER at all, so you might want to handle that case nicely.
Advertisement
quote:Original post by MichaelT
I don't think that is a problem in his case.

Hence I said 'generally'. You said that you 'find this to be a nicer way to code'; I'm merely stating that it is not a nicer way.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on November 16, 2003 12:25:00 PM]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
quote:Original post by Lektrix
Hence I said ''generally''. You said that you ''find this to be a nicer way to code''; I''m merely stating that it is not a nicer way.


I understand that ;-) However, I still find it a nicer way (personally). To me namespace pollution has never been a problem and quite frankly I would like you to give an example that could not be solved easily without using namespace. And before you start talking team work, I have been doing 12 man team work and none of us have had that problem. You are correct in your point however so I am not looking down on that ok?
No no no no! :)
I don''t know much about C/C++ yet (I''m learning Java right now), but I know that they similarities. Couldn''t you just make your own function as well?

int pow (int a, int b)
{
int result = 1;

for (int count = 0 ; count < b ; count++)
{
result = result*a;
}

return result;
}

Of course this would only work for integer values .

"Even perfection has room for improvement."
-Chris Locke
"Even perfection has room for improvement."-Chris Locke
When using pow(2,...) you could always use bitshifts. ie:
inline unsigned long pow2(int toThe){   return (unsigned long)(1<<toThe);} 

edit-replaced tags with [ source ] tags because the < < was being interpretted as html.<br> <br><br><SPAN CLASS=editedby>[edited by - brassfish89 &#111;n November 16, 2003 11:09:45 PM]</SPAN> <br><br><SPAN CLASS=editedby>[edited by - brassfish89 on November 16, 2003 11:10:15 PM]</SPAN>
quote:Original post by vega12
I don''t know much about C/C++ yet (I''m learning Java right now), but I know that they similarities. Couldn''t you just make your own function as well?

Yup. But the method you posted is inefficient; it runs in O(n). O(log n) methods exist; Google for "fast integer exponentiation".

"Sneftel is correct, if rather vulgar." --Flarelocke

This topic is closed to new replies.

Advertisement