C++ question(Decimal places)very weird

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

Join Date: May 2005
Posts: 4
Reputation: maherddd is an unknown quantity at this point 
Solved Threads: 0
maherddd maherddd is offline Offline
Newbie Poster

C++ question(Decimal places)very weird

 
0
  #1
May 29th, 2005
this is an example of what is happening

consider the program:


#include<iostream.h>

void main(void)
{

double x=2.3767553235;
cout<<x<<endl;

}

when i compile it

the output is only giving 2.37676
Why?

I dont want only 5 decimal places,plz someone tell me why is this happening.

i want it to be say for example 8 or 10 decimal places

thank u so much
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 4
Reputation: maherddd is an unknown quantity at this point 
Solved Threads: 0
maherddd maherddd is offline Offline
Newbie Poster

Re: C++ question(Decimal places)very weird

 
0
  #2
May 29th, 2005
so my point is i want 2.3767553235 in the output,why is the pc only putting 2.37676
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: C++ question(Decimal places)very weird

 
0
  #3
May 29th, 2005
>> Why?
Why not? It's concievable that there would be a default precision for the stream, one that would be valid everywhere so that it could be portable. It just so happens that that's the case. The default is 6:
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. double d = 123.4567890123;
  6.  
  7. std::cout << "Precision: " << std::cout.precision() << '\n';
  8. std::cout << "Value: " << d << '\n';
  9. std::cout.precision(4);
  10. std::cout << "Value: " << d << '\n';
  11. }
>> i want it to be say for example 8 or 10 decimal places
Assuming your system is capable of holding an accurate precision to 10 digits past the radix including an arbitrary number of digits before the radix, you would do it like this:
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. double x = 2.3767553235;
  6.  
  7. std::cout.precision(11);
  8. std::cout << x << '\n';
  9. }
However, because the precision is all digits in the number, to get an exact precision of 10 for any whole value, you would need a way to determine the number of digits before the radix. An easy to understand solution is to make a string and take its length:
  1. #include <iostream>
  2. #include <sstream>
  3.  
  4. namespace jrd {
  5. int digits(double val)
  6. {
  7. std::ostringstream oss;
  8.  
  9. oss << static_cast<int>(val);
  10.  
  11. return oss.str().length();
  12. }
  13. }
  14.  
  15. int main()
  16. {
  17. double x = 2.3767553235;
  18.  
  19. std::cout.precision(10 + jrd::digits(x));
  20. std::cout << x << '\n';
  21. }
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 4
Reputation: maherddd is an unknown quantity at this point 
Solved Threads: 0
maherddd maherddd is offline Offline
Newbie Poster

Re: C++ question(Decimal places)very weird

 
0
  #4
May 29th, 2005
whats the difference between <iostream> and <iostream.h>?
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: C++ question(Decimal places)very weird

 
0
  #5
May 29th, 2005
<iostream.h> is not standard, and therefore is becoming increasingly less supported by compilers as standard C++ matures. This isn't a theoretical difference, by the way. My compiler will refuse to compile <iostream.h>.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1
Reputation: little nell is an unknown quantity at this point 
Solved Threads: 0
little nell little nell is offline Offline
Newbie Poster

Re: C++ question(Decimal places)very weird

 
0
  #6
Jun 7th, 2005
Another important difference between iostream.h and iostream is that iostream.h has its declarations in the global namespace, while iostream has them in the std namespace.

dogtree's method is quite inefficient. It is possible to directly set the number of decimal places, but you have to make sure the output mode is "fixed" first.

This example produces 10 decimal places.
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. double x = 2.3767553235;
  8. cout << fixed << showpoint << setprecision(10) << x << endl;
  9. }

If you google for "C++ decimal places" you will find this article

http://www.cs.fsu.edu/~myers/c++/notes/basics1.html

and right at the end is a short explanation of formatting decimal numbers.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: C++ question(Decimal places)very weird

 
0
  #7
Jun 7th, 2005
You need to include <iomanip> for setprecision().
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC