DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   double data type (http://www.daniweb.com/forums/thread156211.html)

jugnu Nov 9th, 2008 11:38 am
double data type
 
When i use a variable type double the value e.g 345624.769123 rounded to 345625
but when i take 5 digits before decimal i.e 34562.4769123 then it shows 34562.5

Please explain Why?

Thanks

ArkM Nov 9th, 2008 1:08 pm
Re: double data type
 
Quote:

Originally Posted by jugnu (Post 731908)
When i use a variable type double the value e.g 345624.769123 rounded to 345625
but when i take 5 digits before decimal i.e 34562.4769123 then it shows 34562.5
Please explain Why?
Thanks

Please explain where and when?

jugnu Nov 9th, 2008 1:16 pm
Re: double data type
 
Quote:

Originally Posted by ArkM (Post 731949)
Please explain where and when?

#include<iostream>
#include <conio.h>

using std::cout;
using std::cin;

main()
{
double x=345624.769123;
cout<<"x=";
cout << x;
getch();
}

ddanbe Nov 9th, 2008 1:25 pm
Re: double data type
 
The 5 digits before have nothing to do with it. When your system is rounding to tenths then xxxx.47.. is rounded to xxxx.5 just as xxxxxx.79.. is rounded to xxxxxx+1.
Read also : http://en.wikipedia.org/wiki/Rounding

ArkM Nov 9th, 2008 3:50 pm
Re: double data type
 
It's simply default precision of stream output. Don't worry, double type always keeps ~16 significant digits...
Use setprecision manipulator from <iomanip> to set another output precision:
#include <iomanip>
double x = ......;
...
for (int n = 0; n < 17; ++n)
    cout << setprecision(n) << x << endl;
Try this and see what happens...


All times are GMT -4. The time now is 12:32 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC