In C++,
What are the random digits that are displayed after giving setprecision() for a floating point number?
Note: After setting the fixed flag.
example:
float f1=3.14;
cout < < fixed<<setprecision(10)<<f1<<endl;
we get random numbers for the remaining 7 digits? But it is not the same case in double.
Two things to be aware of:
floats are stored in binary.floathas a maximum of 24 significant bits. This is equivalent to 7.22 significant digits.So, to your computer, there’s no such number as 3.14. The closest you can get using
floatis 3.1400001049041748046875.doublehas 53 significant bits (~15.95 significant digits), so you get a more accurate approximation, 3.140000000000000124344978758017532527446746826171875. The “noise” digits don’t show up withsetprecision(10), but would withsetprecision(17)or higher.