|
-
March 1st, 2008, 03:39 PM
#1
double is shown as float in size (newbie)
Hello there codeguru gurus...
here is my code:
PHP Code:
#include <iostream>
using namespace std;
double sqrtOfTwo(int counter)
{
if (counter==0)
return 0;
else
{
counter--;
return 1.0/(2.0+sqrtOfTwo(counter));
}
}
int main()
{
cout << rec(10); // doesn't really matter what number comes here
return 0;
}
when i run this code the output will be : 0.414214 , which is shorter than what i'd expect to receive, having declared this function as a double, not as a float. this may be something i simply do not understand in the way variable types work. either way, i'd really appreciate details regarding this issue.
Last edited by dankilman; March 1st, 2008 at 03:46 PM.
-
March 1st, 2008, 03:46 PM
#2
Re: double is shown as float in size (newbie)
This is just because of the default ostream outputprecision.
try like this
Code:
#include <iostream>
#include <iomanip>
using namespace std;
double sqrtOfTwo(int counter) {
if (counter==0)
return 0;
else {
counter--;
return 1.0/(2.0+sqrtOfTwo(counter));
}
}
int main() {
cout << setprecision(12) << sqrtOfTwo(10); // doesn't really matter what number comes here
return 0;
}
Kurt
-
March 1st, 2008, 03:49 PM
#3
Re: double is shown as float in size (newbie)
thanks!
that was quick...
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
|