round

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2009
Posts: 50
Reputation: Akis2000 is an unknown quantity at this point 
Solved Threads: 0
Akis2000 Akis2000 is offline Offline
Junior Poster in Training

round

 
0
  #1
May 8th, 2009
hello, i have a double value=0;

and i want before starting the calulation to round this value
in the numbers that the user add, for example 2...

any ideas??? I have despered...........
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: round

 
0
  #2
May 8th, 2009
Originally Posted by Akis2000 View Post
hello, i have a double value=0;

and i want before starting the calulation to round this value
in the numbers that the user add, for example 2...

any ideas??? I have despered...........
I don't understand that, can you give an example ?
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,407
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 226
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: round

 
0
  #3
May 8th, 2009
With the math.h (or cmath someone correct me on this) header you can use the floor and ceil functions if I remember correctly to round down and round up respectively.
Last edited by ShawnCplus; May 8th, 2009 at 3:59 pm.
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 50
Reputation: Akis2000 is an unknown quantity at this point 
Solved Threads: 0
Akis2000 Akis2000 is offline Offline
Junior Poster in Training

Re: round

 
0
  #4
May 8th, 2009
  1. double pi1=0;
  2.  
  3. for (int long n=1; n<=10000; n++)
  4. {//star for loop
  5. pi1 += (4*pow(-1.0, n+1))/(2*n - 1);
  6. }//end loop
before loop for i want the pi1 round=number by the user...
for example user give 3 the pi1 =3.140
Last edited by Akis2000; May 8th, 2009 at 4:04 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: round

 
0
  #5
May 8th, 2009
Originally Posted by Akis2000 View Post
  1. double pi1=0;
  2.  
  3. for (int long n=1; n<=10000; n++)
  4. {//star for loop
  5. pi1 += (4*pow(-1.0, n+1))/(2*n - 1);
  6. }//end loop
before loop for i want the pi1 round=number by the user...
for example user give 3 the pi1 =3.140
I actually wanted to see an example with concrete numbers, and as already mentioned: the floor and ceil functions from the C++ math library perform rounding on numbers
(and oh, if you post a snippet of code, could you please post it using code tags then ?)
Last edited by tux4life; May 8th, 2009 at 4:11 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: round

 
0
  #6
May 8th, 2009
Originally Posted by ShawnCplus View Post
With the math.h (or cmath someone correct me on this) header you can use the floor and ceil functions if I remember correctly to round down and round up respectively.
In C++ cmath is the preferred way, use math.h only if your compiler doesn't support the new header file style (e.g.: cmath ), but the use of math.h is not wrong
Last edited by tux4life; May 8th, 2009 at 4:10 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 50
Reputation: Akis2000 is an unknown quantity at this point 
Solved Threads: 0
Akis2000 Akis2000 is offline Offline
Junior Poster in Training

Re: round

 
0
  #7
May 8th, 2009
ok i have this... but i have overloaded... error with pow...

  1. ceil(pi1 * pow( 10,digits ) - 0.5) /pow(10,digits);
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: round

 
0
  #8
May 8th, 2009
Originally Posted by Akis2000 View Post
ok i have this... but i have overloaded... error with pow...

  1. ceil(pi1 * pow( 10,digits ) - 0.5) /pow(10,digits);
If you're having an error, can you please mention it also, so we can take a look at it?
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 219
Reputation: NathanOliver is an unknown quantity at this point 
Solved Threads: 37
NathanOliver's Avatar
NathanOliver NathanOliver is offline Offline
Posting Whiz in Training

Re: round

 
0
  #9
May 8th, 2009
if you mean that you want to round the number to a specific number of decimal places you might want to make a char array and use _gcvt() to store in the number of digits you want. for your example if you want pi to 5 places you could do this.
  1. const double fullPi=3.141592653589793;
  2. char * pi = new char[8];
  3. _gcvt(fullPi, 8, pi);
_gcvt() takes in a double and outputs the number of digits specified in the second term then it outputs the string into the char[] in the third term. for this you need to have an array that is big enough to hold the digits before the decimal place the decimal itself the number of digits after the decimal point and the terminating null at the end so in this case for 5 places you need a char[] of size 8.
Last edited by NathanOliver; May 8th, 2009 at 7:40 pm.
if you write using namespace std; you do not need to write std::something in your program.
If your thread is solved please mark it as solved
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 50
Reputation: Akis2000 is an unknown quantity at this point 
Solved Threads: 0
Akis2000 Akis2000 is offline Offline
Junior Poster in Training

Re: round

 
0
  #10
May 8th, 2009
yeup, this is correct, but if i want to round a value before i calculate the value???

for example, i have value pi1: and i want to round this value before i go to the calculation.......

  1. double pi1=0;
  2. //here i want a code that tell to the for loop that the pi1 is
  3. only 2 digits.... ( the user select the number ) 3.14 it's example
  4. for (int long n=1; n<=10000; n++)
  5. {
  6. pi1+= (4*pow(-1.0, n+1))/(2*n - 1);
  7. }
  8.  
Last edited by Akis2000; May 8th, 2009 at 8:42 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC