944,141 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 28349
  • C++ RSS
May 29th, 2005
0

C++ question(Decimal places)very weird

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
maherddd is offline Offline
4 posts
since May 2005
May 29th, 2005
0

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

so my point is i want 2.3767553235 in the output,why is the pc only putting 2.37676
Reputation Points: 10
Solved Threads: 0
Newbie Poster
maherddd is offline Offline
4 posts
since May 2005
May 29th, 2005
0

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

>> 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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 35
Solved Threads: 3
Posting Whiz in Training
Dogtree is offline Offline
232 posts
since May 2005
May 29th, 2005
0

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

whats the difference between <iostream> and <iostream.h>?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
maherddd is offline Offline
4 posts
since May 2005
May 29th, 2005
0

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

<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>.
Reputation Points: 35
Solved Threads: 3
Posting Whiz in Training
Dogtree is offline Offline
232 posts
since May 2005
Jun 7th, 2005
0

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

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.
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
little nell is offline Offline
1 posts
since Jun 2005
Jun 7th, 2005
0

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

You need to include <iomanip> for setprecision().
Reputation Points: 35
Solved Threads: 3
Posting Whiz in Training
Dogtree is offline Offline
232 posts
since May 2005

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: Problem with "\n"
Next Thread in C++ Forum Timeline: Help in AI





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


Follow us on Twitter


© 2011 DaniWeb® LLC