Click here to Skip to main content
15,920,383 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
double x = 3.14159;

	double y = 131.14159;

	double z = .00314159;

	double a = 13114.159;


	cout << setprecision(3) << "x =  " << x << endl;

	cout << "y =  " << y << endl;

	cout  << "z =  " << z << endl;

	cout  << "a =  " << a << endl;


What I have tried:

I am little bit confused about the behavior of setprecision. What I understand that setprecision tries to get the most significant digits before and after the decimal point (included) according to the value of precision. From my understanding,for the variable z it outputs .00314 because 314 are 3 most significant digits.I do not understand why for the last variable it outputs as 1.31e+04?
Posted
Updated 15-Sep-16 16:40pm
v2
Comments
[no name] 15-Sep-16 20:40pm    
Wow. 2+ years here and no clue how to ask a question. Just wow.
Up_CrossYourHeart 15-Sep-16 21:03pm    
Sorry ThePhantomUpvoter. I didn't notice the question did not get posted.

1 solution

The short answer is that 1.31e+04 is just about the only way to express that number with 3 digits precision. 13100 would be misleading - the trailing zeroes might be significant. The leading zeroes in .00314 are clearly not significant.
 
Share this answer
 
Comments
CPallini 16-Sep-16 3:34am    
My 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900