Powers in C++

Started by
14 comments, last by Ekim_Gram 20 years, 5 months ago
I''m working on a decimal to binary console program in C++ and I''m wondering, how do you declare powers in C++? Like 2 to the power of 6 and so on? R.I.P. Mark Osback Solo Pa Mi Gente VG-Force
Advertisement
#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]
Half the people you know are below average.Trogdor the Burninator
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]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
Lektrix I noticed you are using ansi c++. Will that work with win32 programming. I have tried and it didn''t compile.
(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]
No no no no! :)
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]
[ 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
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.

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.
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]
[ 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
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.
No no no no! :)

This topic is closed to new replies.

Advertisement