|
-
May 15th, 2002, 03:45 PM
#1
really big numbers
I have a program here that doubles .01 64 times. After a while, the numbers start to come out in scientific notation. Is there a type of variable or something that does not do this? I am new to C++, so please make any replies easy to understand. The bold area of the code is the part that displays in scientific notation. Here is the program:
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
int first(void);
int bonus(void);
int main(void)
{
unsigned int response;
cout<<"Checkerboards and Bacterial Reproduction Problem Solver\nby Drew Harris";
do
{
cout<<"\n\nSelect an Option:\n"
<<"1) Answer Questions 1, 2, and 3"<<endl
<<"2) Answer the bonus question"<<endl
<<"3) Exit"<<endl;
cin >>response;
switch(response)
{
case 1:
cout<<endl<<endl;
first();
break;
case 2:
cout<<endl<<endl;
bonus();
break;
default:
cout<<endl<<"That is not a menu choice";
}
}while(response != 3);
cout<<"\nThat's how I solved the checkerboard questions."<<endl;
system("pause");
return 0;
}
int first(void)
{
double totalmoney = .01;
unsigned int cursquare = 1;
cout<<"1 | 0.01"<<endl;
while(totalmoney<=21.00)
{
totalmoney = totalmoney * 2;
cursquare++;
if(totalmoney>21.00)
{
break;
}
cout<<cursquare<<" | "<<totalmoney<<endl;
}
return 0;
}
int bonus(void)
{
double totalmoney = .01;
unsigned int cursquare = 1;
cout<<"1 | 0.01"<<endl;
while(cursquare<64)
{
totalmoney = totalmoney * 2;
cursquare++;
cout<<cursquare<<" | "<<totalmoney<<endl;
}
return 0;
}
-
May 15th, 2002, 04:22 PM
#2
Frenzied Member
In any function using cout or in main( ), use this code:
Code:
cout.precision(20); // 15 is ok
Anything past the 15th digit is not really that accurate.
See this:
http://support.microsoft.com/default...b;EN-US;q42980
to tell you why.
Last edited by wey97; May 15th, 2002 at 04:30 PM.
-
May 15th, 2002, 06:16 PM
#3
thanks
Thanks. That's exactly what I needed.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|