| | |
round
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
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.
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.
•
•
Join Date: Apr 2009
Posts: 50
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
double pi1=0; for (int long n=1; n<=10000; n++) {//star for loop pi1 += (4*pow(-1.0, n+1))/(2*n - 1); }//end loop
for example user give 3 the pi1 =3.140
Last edited by Akis2000; May 8th, 2009 at 4:04 pm.
•
•
•
•
before loop for i want the pi1 round=number by the user...C++ Syntax (Toggle Plain Text)
double pi1=0; for (int long n=1; n<=10000; n++) {//star for loop pi1 += (4*pow(-1.0, n+1))/(2*n - 1); }//end loop
for example user give 3 the pi1 =3.140
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."
•
•
•
•
With themath.h(or cmath someone correct me on this) header you can use thefloorandceilfunctions if I remember correctly to round down and round up respectively.
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."
•
•
Join Date: Apr 2009
Posts: 50
Reputation:
Solved Threads: 0
ok i have this... but i have overloaded... error with pow...
C++ Syntax (Toggle Plain Text)
ceil(pi1 * pow( 10,digits ) - 0.5) /pow(10,digits);
•
•
•
•
ok i have this... but i have overloaded... error with pow...
C++ Syntax (Toggle Plain Text)
ceil(pi1 * pow( 10,digits ) - 0.5) /pow(10,digits);
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
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.
_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.
c++ Syntax (Toggle Plain Text)
const double fullPi=3.141592653589793; char * pi = new char[8]; _gcvt(fullPi, 8, pi);
Last edited by NathanOliver; May 8th, 2009 at 7:40 pm.
if you write
If your thread is solved please mark it as solved
using namespace std; you do not need to write std::something in your program.If your thread is solved please mark it as solved
•
•
Join Date: Apr 2009
Posts: 50
Reputation:
Solved Threads: 0
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.......
for example, i have value pi1: and i want to round this value before i go to the calculation.......
C++ Syntax (Toggle Plain Text)
double pi1=0; //here i want a code that tell to the for loop that the pi1 is only 2 digits.... ( the user select the number ) 3.14 it's example for (int long n=1; n<=10000; n++) { pi1+= (4*pow(-1.0, n+1))/(2*n - 1); }
Last edited by Akis2000; May 8th, 2009 at 8:42 pm.
![]() |
Similar Threads
- round robin algorithm (Computer Science)
- Round Robin Algorithm Simulation (C++)
- Round Robin algorithm? (Java)
Other Threads in the C++ Forum
- Previous Thread: library management system in cpp
- Next Thread: copy a stack to another instance?
| Thread Tools | Search this Thread |
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting string strings studio temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






