943,929 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1941
  • C++ RSS
Nov 9th, 2008
0

double data type

Expand 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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jugnu is offline Offline
15 posts
since Nov 2008
Nov 9th, 2008
0

Re: double data type

Click to Expand / Collapse  Quote originally posted by jugnu ...
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?
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Nov 9th, 2008
0

Re: double data type

Click to Expand / Collapse  Quote originally posted by ArkM ...
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();
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jugnu is offline Offline
15 posts
since Nov 2008
Nov 9th, 2008
0

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
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Nov 9th, 2008
0

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:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Clock tick
Next Thread in C++ Forum Timeline: Problem with constructor





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC