470,821 Members | 1,995 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,821 developers. It's quick & easy.

Beginner Question - Data Types

Hi,
I'm having trouble with data types. I understand that the double data
type is 64-bit, which should correspond to 15 digits(?).
However, it seems from running this program that it can only handle up
to 6 digits:
#include <iostream>
using namespace std;

main () {

double w = 500 - 0.001;
cout << w << endl;

double x = 500 - 0.0001;
cout << x << endl;

system("PAUSE");
return 0;
}
since the output is:

500
499.99

Where am I going wrong?

Thanks for your help,
Tom

Feb 26 '06 #1
3 1488
i know in C the default output width is 6 places, so maybe the same
thing is happening in C++. i think there's a stream comand called setw
or something like that that you can use to set how many decimal places
are showing. you're problem is definatley an i/o problem, not an
actual math issue. the actual values on w and x are exactly what you'd
expect them to be. as an example try the C type of i/o. include
<stdio.h> and then do printf("%.15le\n",w) and printf("%.15le\n",x) and
see what you get.

Feb 26 '06 #2
<to******@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com
Hi,
I'm having trouble with data types. I understand that the double data
type is 64-bit, which should correspond to 15 digits(?).
However, it seems from running this program that it can only handle up
to 6 digits:
#include <iostream>
using namespace std;

main () {

double w = 500 - 0.001;
cout << w << endl;

double x = 500 - 0.0001;
cout << x << endl;

system("PAUSE");
return 0;
}
since the output is:

500
499.99

Where am I going wrong?

Thanks for your help,
Tom


I suspect the output is the other way around. In any event, two
explanations:

1. cout will round its output to some number of decimal places independently
of what a double is capable of storing. Add

#include <iomanip>

and try

cout << setprecision(15) << x << endl;

2. doubles store numbers as binary (base 2) numbers. 1/10 or any reciprocal
of a power of 10 cannot be exactly represented as a base 2 number. Expect
all floating point arithmetic to only be approximate.
--
John Carson


Feb 26 '06 #3

John Carson wrote in message...
<to******@gmail.com> wrote in message
Hi,
I'm having trouble with data types. I understand that the double data
type is 64-bit, which should correspond to 15 digits(?).
However, it seems from running this program that it can only handle up
to 6 digits:

#include <iostream>
using namespace std;
main () {
double w = 500 - 0.001;
cout << w << endl;
double x = 500 - 0.0001;
cout << x << endl;
system("PAUSE");
return 0;
}
since the output is:
500
499.99
Where am I going wrong?
Thanks for your help,
Tom


I suspect the output is the other way around. In any event, two
explanations:

1. cout will round its output to some number of decimal places independently
of what a double is capable of storing. Add

#include <iomanip>

and try

cout << setprecision(15) << x << endl;


Add: If you have much to output, you can 'set' the stream:

int OutPre = std::cout.precision(); // default==6
std::cout<<"Current output precision is "<<OutPre<<std::endl;
std::cout.setf( std::ios::fixed );
std::cout.precision( 15 );
std::cout << x << std::endl;
// ... more output here ...
std::cout.precision( OutPre ); // 'reset' it

BTW tomrobin: It's ALWAYS:
int main()
NOT
main()
....and the return should be:
0, EXIT_SUCCESS or EXIT_FAILURE.

--
Bob R
POVrookie
Feb 27 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Cao Tran | last post: by
12 posts views Thread by Blaze | last post: by
4 posts views Thread by a | last post: by
12 posts views Thread by =?Utf-8?B?Qi4gQ2hlcm5pY2s=?= | last post: by
22 posts views Thread by ddg_linux | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.