Archived

This topic is now archived and is closed to further replies.

Ekim_Gram

Powers in C++

Recommended Posts


#include <stdio.h>
#include <math.h>

int main()
{
int num=pow(2,6);
cout<<"2 to the power of 6 is "<<num<<endl;
return 0;
}


[edited by - Kaezin on November 15, 2003 9:31:28 PM]

[edited by - Kaezin on November 15, 2003 9:33:30 PM]

Share this post


Link to post
Share on other sites
No, he said C++:

#include <iostream>
#include <cmath> // for std::pow


int main()
{
std::cout << "4^7 = " << std::pow(4, 7) << std::endl;

return 0;
}

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

[edited by - Lektrix on November 16, 2003 3:26:52 AM]

Share this post


Link to post
Share on other sites
(if you are using vs6 like me it will state that).
Remove std:: from the pow and it will compile.

Also, I find this to be a nicer way to code:


#include <iostream>
#include <cmath>
using namespace std;

int main()
{
cout << "4^7 = " << pow(4, 7) << endl;
return 0;
}



[edited by - MichaelT on November 16, 2003 4:38:59 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by dr_slash_uh
Will that work with win32 programming.


Yes.
quote:
Original post by dr_slash_uh
I have tried and it didn't compile.

Well, it's the compiler then; what I posted was standard-compliant code. If you are using MSVC++ 6 or earlier, I don't think that pow() is placed in the std namespace, so you would not have to use the scope resolution operator to qualify it. I believe there is a way to determine whether the compiler is MSVC++ using the preprocessor, and whether it is less than or equal to MSVC++ 6, and therefore you could compile the correct code segment, resulting in portable code, yet I don't use MSVC++ and so don't have much experience with it. Anyway, this code should be compatable with MSVC++ 6:

#include <iostream>
#include <cmath>

int main()
{
std::cout << "4^7 = " << pow(4, 7) << std::endl;

return 0;
}

quote:
Original post by MichaelT
Also, I find this to be a nicer way to code


You might find it nicer, but it's generally not. You are just reintroducing the global namespace pollution, which destroys the point in having namespaces in the first place.

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

[edited by - Lektrix on November 16, 2003 5:23:32 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Lektrix
You might find it nicer, but it''s generally not. You are just reintroducing the global namespace pollution, which destroys the point in having namespaces in the first place.
This advice of course only applies if you work on a large project and use more than one namespace.

Share this post


Link to post
Share on other sites
quote:
Original post by Lektrix
I believe there is a way to determine whether the compiler is MSVC++ using the preprocessor, and whether it is less than or equal to MSVC++ 6, and therefore you could compile the correct code segment, resulting in portable code...
#if _MSC_VER <= 1200, I believe.

Share this post


Link to post
Share on other sites
quote:
Original post by alnite
This advice of course only applies if you work on a large project and use more than one namespace.


No, the advice does not, of course, only apply to "large projects using more than one namespace".
quote:
Original post by Beer Hunter
#if _MSC_VER <= 1200, I believe.


Yes, that looks familiar, yet surely the #if preprocessor directive would be something more like this?

#if _MSC_VER && _MSC_VER <= 1200

Well, you can have a fiddle with it, Ekim_Gram.

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

[edited by - Lektrix on November 16, 2003 5:39:38 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Lektrix
You might find it nicer, but it''s generally not. You are just reintroducing the global namespace pollution, which destroys the point in having namespaces in the first place.



I don''t think that is a problem in his case.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
When using pow(2,...) you could always use bitshifts. ie:

inline unsigned long pow2(int toThe)
{
return (unsigned long)(1<<toThe);
}

edit-replaced [ code ] tags with [ source ] tags because the < < was being interpretted as html.


[edited by - brassfish89 on November 16, 2003 11:09:45 PM]

[edited by - brassfish89 on November 16, 2003 11:10:15 PM]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites