Click to See Complete Forum and Search --> : double is shown as float in size (newbie)


dankilman
March 1st, 2008, 02:39 PM
Hello there codeguru gurus...
here is my 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.

ZuK
March 1st, 2008, 02:46 PM
This is just because of the default ostream outputprecision.
try like this
#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

dankilman
March 1st, 2008, 02:49 PM
thanks!
that was quick...