Beginner Question - Data Types

T

tomrobin

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
 
M

Mike Deskevich

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.
 
J

John Carson

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.
 
B

BobR

John Carson wrote in message...
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top