


Dev Articles Community Forums
> Programming
> C/C++ Help

C++ math operators
Discuss C++ math operators in the C/C++ Help forum on Dev Articles. C++ math operators C/C++ Help forum discussing building and maintaining applications in C/C++. Find out why these languages are the foundation on which other languages are built.






Dev Articles Community Forums Sponsor:


September 16th, 2003, 10:01 PM

Registered User


Join Date: Sep 2003
Location: Austin
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0


C++ math operators
I'm learning C++ and need to know if there is a math operator for raising a number to a power. Example 2 sqaured would be written how???? (Other than 2 * 2)

September 17th, 2003, 04:37 AM

Registered User


Join Date: Sep 2003
Location: StokeonTrent, UK
Posts: 2
Time spent in forums: < 1 sec
Reputation Power: 0


You can use the power operator: ^
eg. 2 to the power of three would be 2^3;

September 17th, 2003, 08:29 AM

Registered User


Join Date: Sep 2003
Location: Austin
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0


Thank you

September 17th, 2003, 10:56 AM

Registered User


Join Date: Oct 2002
Posts: 4
Time spent in forums: < 1 sec
Reputation Power: 0


C++ does not have a ^ operator. Use the pow() function.
Example: 2 to the power of 3 would be pow(2,3)

September 17th, 2003, 11:20 AM

Registered User


Join Date: Sep 2003
Location: StokeonTrent, UK
Posts: 2
Time spent in forums: < 1 sec
Reputation Power: 0


Oops. Sorry My mistake  that's VB, not C++

September 18th, 2003, 12:03 AM

Registered User


Join Date: Sep 2003
Location: Austin
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0


Well now I have a new problem. I have been trying to figure it out for hours, and no luck.
I'm trying to have the program calculate this equation:
N = P * 2 ^ (T/10). T is days, and integer. P is any whole number, an integer. N should be a decimal (double), but it always comes out to be another whole number.
I'm using a loop to calculate N, when P is entered by the user, for 10 days.
IE:
for (T=1; T<=10; T++)
{
N = P * (2^(T/10));
cout << T;
cout << " " ;
cout << N << '\n';
}
This is probably a very simple thing, but I can't figure out why the output is wrong.
Any ideas?

February 3rd, 2004, 12:54 PM

Registered User


Join Date: Feb 2004
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0


Ok, I did this once. if you are calculating with integers the result will be integer. try for instance printing the value of you T/10.... you wold be surprised. Even if you know these T and P should be integers, declare them as floats, and see what happens.
Now I have a little prolem: does anybody know what is the function in c++ which gives the maximum between two values?
cheers,
Kazu.
Quote:
Originally Posted by erockguide
Well now I have a new problem. I have been trying to figure it out for hours, and no luck.
I'm trying to have the program calculate this equation:
N = P * 2 ^ (T/10). T is days, and integer. P is any whole number, an integer. N should be a decimal (double), but it always comes out to be another whole number.
I'm using a loop to calculate N, when P is entered by the user, for 10 days.
IE:
for (T=1; T<=10; T++)
{
N = P * (2^(T/10));
cout << T;
cout << " " ;
cout << N << '\n';
}
This is probably a very simple thing, but I can't figure out why the output is wrong.
Any ideas?


February 10th, 2004, 05:43 AM

Registered User


Join Date: Feb 2004
Posts: 28
Time spent in forums: < 1 sec
Reputation Power: 0


Kazu is essentially right, but I wouldn't declare an integer as a float if it should be an integer. Declare T and P as integers, but convert them for the calculation:
for (T=1; T<=10; T++)
{
N = static_cast<double>P * (2^(static_cast<double>T/10.0));
cout << T;
cout << " " ;
cout << N << '\n';
}
This way you retain the benefits of knowing that T and P are integers, as they are supposed to be for the rest of the program (also notice the division is by 10.0).
Hope this was in time to help...

Developer Shed Advertisers and Affiliates
Thread Tools 
Search this Thread 


Display Modes 
Rate This Thread 
Linear Mode


Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off




