473,245 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,245 software developers and data experts.

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 1620
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
by: lester | last post by:
a pre-beginner's question: what is the pros and cons of .net, compared to ++ I am wondering what can I get if I continue to learn C# after I have learned C --> C++ --> C# ?? I think there...
10
by: Cao Tran | last post by:
How do I pass different types of argument such as short, int, long, etc.. to a function that does routine stuff? Example: int main() { short x=1; int y =1; long z=1
2
by: santa19992000 | last post by:
Guys: Can somebody suggest a "C lang" project with pointers and linked lits for beginner?. Also is there anywahere I can find some C projects?. Thanks in advance.
1
by: Mike Malter | last post by:
I am just starting to work with reflection and I want to create a log that saves relevant information if a method call fails so I can call that method again later using reflection. I am...
12
by: Blaze | last post by:
I am doing the first walk through on the Visual Studio .Net walkthrough book to learn a little about programming. I am having issues with the first tutorial not running correctly. It seems that...
6
by: RaulAbHK | last post by:
Dear all, I guess this is a basic question with an easy answer but I am a beginner and I would much apreciate your feedbacks. Let's say I have a library with some functionality to perform some...
4
by: a | last post by:
Dear all vb.net developer I want to know the time I need to master vb.net? I'm beginner
12
by: =?Utf-8?B?Qi4gQ2hlcm5pY2s=?= | last post by:
I'm doing a web app in VB/Dot Net 2.0. I'm probably a bit rusty and I have no experience using the repeater control. I have a user control I've created with multiple properties. I've created a...
22
by: ddg_linux | last post by:
I have been reading about and doing a lot of php code examples from books but now I find myself wanting to do something practical with some of the skills that I have learned. I am a beginner php...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.