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...........

Recommended Answers

All 24 Replies

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 ?

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.

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

before loop for i want the pi1 round=number by the user...
for example user give 3 the pi1 =3.140

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

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 ?)

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 :)

ok i have this... but i have overloaded... error with pow...

ceil(pi1 * pow( 10,digits ) - 0.5) /pow(10,digits);

ok i have this... but i have overloaded... error with pow...

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?

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.

const double fullPi=3.141592653589793;
char * pi = new char[8];
_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.

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.......

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);
}

I don't think you would like rounding the number the use inputs. You will loose on the accuracy of your result.
What you want may be is that when your display the output, the precision should be only 2 digits. Do something like this:

#include<iostream>
#include<iomanip>
int main()
{
double d=3.1457545;
std::cin>>d;
//do your calculation with d
//with great acuraccy !!
const std::streamsize orig=std::cout.precision();//save the orignal precision
std::cout<<std::setprecision(3)<<d<<std::endl;//set precision to 3 significant digits
std::cout<<std::setprecision(orignal);//regain orignal precision
}

I don't think you would like rounding the number the use inputs. You will loose on the accuracy of your result.
What you want may be is that when your display the output, the precision should be only 2 digits. Do something like this:

#include<iostream>
#include<iomanip>
int main()
{
double d=3.1457545;
std::cin>>d;
//do your calculation with d
//with great acuraccy !!
const std::streamsize orig=std::cout.precision();//save the orignal precision
std::cout<<std::setprecision(3)<<d<<std::endl;//set precision to 3 significant digits
std::cout<<std::setprecision(orignal);//regain orignal precision
}

???? i don't understand anything about that your have write...
sorry.....

I make a code.... If you can tell me if it's correct......

#include<iostream>
#include<cmath>
#include<math.h>
#include<iomanip>
using namespace std;

int main()
{

double num;
double approximate=31415926535.89793;

	cout<<"Enter The Number of round: ";
	cin>>num;

   approximate=approximate/pow(10.0,num);
   cout<<"The number is : "<<fixed << setprecision(num)<<approximate;
   cin>>approximate;
   return 0;

}

As usually (on this forum ;)), you OP don't understand floating-point type data semantics.

Floating-point numbers are approximations of real numbers. For example, double(3.14) is NOT equal (exactly) to 3.14, double(0.1) is NOT equal to 1/10. Try this and see what happens:

double d = 0.0;
for (int i = 0; i < 10; i++)
    d += 0.1;
if (d == 1.0)
    cout << "OK, 10*0.1 == 1\n";
else
    cout << "Oops, 10*0.1 != 1\n"; // This case!!!

Therefore it's IMPOSSIBLE to round double values to n DECIMAL digits exactly. Think about your specifications and desired effects...

As usually (on this forum ;)), you OP don't understand floating-point type data semantics.

Floating-point numbers are approximations of real numbers. For example, double(3.14) is NOT equal (exactly) to 3.14, double(0.1) is NOT equal to 1/10. Try this and see what happens:

double d = 0.0;
for (int i = 0; i < 10; i++)
    d += 0.1;
if (d == 1.0)
    cout << "OK, 10*0.1 == 1\n";
else
    cout << "Oops, 10*0.1 != 1\n"; // This case!!!

Therefore it's IMPOSSIBLE to round double values to n DECIMAL digits exactly. Think about your specifications and desired effects...

so it's possible to round a value that it's 0 ??? ;) i mean we have a double a=0;
and we calculate but we want the results have rounded... for example 3.91

Sorry, I don't understand: do you want to round zero to zero with 2 decimal positions precision? ;)
About rounding of 3.91: re-read my post again.
Try this code:

#include <cmath>

double round(double x, int n = 0)
{
    bool neg = (x < 0.0);
    double ipart, fpart, pow10 = 1.0;
    if (neg)
        x = -x;
    fpart = modf(x,&ipart);
    for (int i = 0; i < n; i++)
        pow10 *= 10.0; // faster than pow(10,n)
    fpart = floor(fpart*pow10+0.5)/pow10;
    x = ipart + fpart;
    return neg? -x: x;
}

const double pi = 3.14159265358;

int main()

{
    printf("%.15f\n",pi); // (I hate setprecision() ;)
    for (int i = 0; i < 12; ++i)
        printf("%.15f\n",round(pi,i));
    return 0;
}
/* Output:
3.141592653580000
3.000000000000000
3.100000000000000
3.140000000000000
3.142000000000000
3.141600000000000
3.141590000000000
3.141593000000000
3.141592700000000
3.141592650000000
3.141592654000000
3.141592653600000
3.141592653580000
*/

Remember: these numbers are not exact values!

Hi siddhantes, there's nothing wrong with your way of thinking, but doesn't your code have to be:

#include<iostream>
#include<iomanip>
int main()
{
double d=3.1457545;
std::cin>>d;
//do your calculation with d
//with great acuraccy !!
const std::streamsize orig=std::cout.precision();//save the orignal precision
std::cout<<std::setprecision(3)<<d<<std::endl;//set precision to 3 significant digits
std::cout<<std::setprecision([B]orig[/B]);//regain orig[U][B]i[/B][/U]nal precision
}

the truth is that i want round zero to zero with 2 decimal positions precision....

before the calculate.... because the program compare step by steps all the results...
i don't want to see that.....

printf("%.15f\n",round(pi,i));

but something like this.....

double x=7839,total=0;
total=x/pow(10.0, 3);

7839/ 10 to the power of 3 = 7.839
There are interesting code that you have post... i will see it and i see what i can do .......

double x=7839,total=0;
total=x/pow(10.0, 3);

Thank you, I have never seen so strange code as it ;)
have you ever seen 1000.0 constant?
What for total=0 initialization?
I see you don't understand my code at all.

total = round(x,3);
double x=7839,total=0;
total=x/pow(10.0, 3);

Thank you, I have never seen so strange code as it ;)
have you ever seen 1000.0 constant?
What for total=0 initialization?
I see you don't understand my code at all.

total = round(x,3);

ok let's say the user select the first series with 5 digits of accuracy

why my code doesn't works???( I'm stupid ??) Perhaps it should I abandon it???
also What for total=0 initialization? Microsoft it's suck, i had error initialize if i don't but 0
:S

i can't understand::

( i used pow because for some reasons round doesn't works)
i

for (int long n=1; n<=10000; n++) 
{//star for loop
	 pi1l+= (4*pow(-1.0, n))/(2*n - 1); 
	 total+=pi1/pow(10.0,digits)
	 
    
     if(pi1!=total)
	 
		  counter++; //counter for the iterations
	  
	 else 
	 if (pi1==total)
	 {
cout<<"Series "<<series<<" is approximate on "<<counter<<" iterations"<<" Series "<<series<<" is "<<pi1<<endl;
	 
	 }
cin.get();

Are you stupid or not - judge for yourself. I see at least two senseless fragments in your code:

total += pi1/pow(10.0,digits);

and

if (pi1 == total)

I can't understand the goal of the 1st one, and I see that you don't know a simplest thing: double numbers are approximations of real numbers so you have no chance to get exact equation in the 2nd one...
Moreover, OP title is incorrect. Probably you want to test (informally) if (abs(pi-sum_of_series) < 10^-n) . It does not bear a relation to rounding at all.

:S Perhaps I'm stupid because all these hours that I wasted I tried to make the pi1 value result's to be equal with the digit's that the user set........ for example if the user add 5 pi1 calculated the series ONLY with 5 digits.......

Yes, Tux, That was a Typo.

I don't think the OP is clear what he wants.

if you don't know what the program ask and you don't know the answer then don't post, it's waste of time for someone that has a problem...........

you didn't help me!!!!!!!!!!
Thanks any way!!!!!!!!

commented: Asshole. -1

if you don't know what the program ask and you don't know the answer then don't post, it's waste of time for someone that has a problem...........

you didn't help me!!!!!!!!!!
Thanks any way!!!!!!!!

If you don't know what you want then don't waste your time on this forum :(

If you don't know what you want then don't waste your time on this forum :(

Υou have right my friend... the truth is i don't know what the project ask... but I have right because my English skills is poor, and I'm sorry about that..........

but i write the whole project exaclly as I took it....., how it's possible you don't understand the meaning???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.