C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 16th, 2003, 09:01 PM
erockguide erockguide is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Austin
Posts: 5 erockguide User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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)

Reply With Quote
  #2  
Old September 17th, 2003, 03:37 AM
Nick Dunn Nick Dunn is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Stoke-on-Trent, UK
Posts: 2 Nick Dunn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;

Reply With Quote
  #3  
Old September 17th, 2003, 07:29 AM
erockguide erockguide is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Austin
Posts: 5 erockguide User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thank you

Reply With Quote
  #4  
Old September 17th, 2003, 09:56 AM
nascarfreak6 nascarfreak6 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 4 nascarfreak6 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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)

Reply With Quote
  #5  
Old September 17th, 2003, 10:20 AM
Nick Dunn Nick Dunn is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Stoke-on-Trent, UK
Posts: 2 Nick Dunn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Post

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

Reply With Quote
  #6  
Old September 17th, 2003, 11:03 PM
erockguide erockguide is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Austin
Posts: 5 erockguide User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #7  
Old February 3rd, 2004, 11:54 AM
kazu kazu is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 1 kazu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #8  
Old February 10th, 2004, 04:43 AM
Buster77 Buster77 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 28 Buster77 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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...

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > C++ math operators


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap