number display precision

Hi,

I wrote some code to calculate variance of data. From calculation with Excel, the result should be 801.1667. However, when I use the following code to display printf("%.1f\n", double(variance)); it displays 801.2.

I don't know whether this is because of C++ traditional precision problem (all variables I use are defined as "double"), or just because I didn't ask for more precision in my output code.

Could someone please explain me? Thanks a lot.
The print statement you posted there explicitly tells the program to round the output to one decimal place. If you want 4 decimal places, it would instead be printf("%.4f\n", double(variance));.
Why are you casting variance to a double? Isn't it already a double?

The 1 in %.1f means that it should show one digit after the decimal point. If you want 4 digits after the decimal point you should write printf("%.4f\n", double(variance));
Oh thanks, I just forgot that small thing in the command. Problem solved!
Topic archived. No new replies allowed.