Why do Floats and Doubles seem the same?
Feb 11, 2012 at 6:40pm Feb 11, 2012 at 6:40pm UTC
I read that doubles should hold about 15 digits, and floats can only hold 7 (
http://cplusplus.com/doc/tutorial/variables/). But then why does this code output as if both variables are floats? Not sure if this is cout's fault or what... I put a long double in too just to see what would happen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
using namespace std;
int main()
{
float pi = 3.14159265358979;
double superpi = 3.14159265358979;
long double supersuperpi = 3.14159265358979;
cout << pi << endl;
cout << superpi << endl;
cout << supersuperpi << endl;
return 0;
}
Output:
3.14159
3.14159
3.14159
Feb 11, 2012 at 6:49pm Feb 11, 2012 at 6:49pm UTC
Try this one:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float pi = 3.14159265358979;
double superpi = 3.14159265358979;
long double supersuperpi = 3.14159265358979;
cout << setprecision (20);
cout << pi << endl;
cout << superpi << endl;
cout << supersuperpi << endl;
return 0;
}
Feb 11, 2012 at 10:33pm Feb 11, 2012 at 10:33pm UTC
I see, so it's cout's fault after all. Thanks!
Topic archived. No new replies allowed.