double data type

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 3
Reputation: jugnu is an unknown quantity at this point 
Solved Threads: 0
jugnu jugnu is offline Offline
Newbie Poster

double data type

 
0
  #1
Nov 9th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: double data type

 
0
  #2
Nov 9th, 2008
Originally Posted by jugnu View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 3
Reputation: jugnu is an unknown quantity at this point 
Solved Threads: 0
jugnu jugnu is offline Offline
Newbie Poster

Re: double data type

 
0
  #3
Nov 9th, 2008
Originally Posted by ArkM View Post
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();
}
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,909
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 274
ddanbe's Avatar
ddanbe ddanbe is online now Online
Posting Virtuoso

Re: double data type

 
0
  #4
Nov 9th, 2008
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
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: double data type

 
0
  #5
Nov 9th, 2008
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:
  1. #include <iomanip>
  2. double x = ......;
  3. ...
  4. for (int n = 0; n < 17; ++n)
  5. cout << setprecision(n) << x << endl;
Try this and see what happens...
Last edited by ArkM; Nov 9th, 2008 at 3:52 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC