By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,987 Members | 971 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,987 IT Pros & Developers. It's quick & easy.

Could someone check the reason why the decimal output of my code does not change.

P: 2
Please check my code why it isn't producing result that has decimal places that i want?
Expand|Select|Wrap|Line Numbers
  1. int main ()
  2. {
  3. double a,b,fa,fb,fc,c,f,x,t,g,y,temp;
  4. int max=500;
  5. int i,z;
  6. cout<<"Enter tolerance: ";
  7. cin>>y;
  8. cout<<"Enter how many decimal places: "<<endl;
  9. cin>>z;
  10. cout<<"Enter the domain:"<<endl;
  11. cout<<"a= ";
  12. cin>>a;
  13. cout<<"b= ";
  14. cin>>b;
  15.  
  16. if (a>b)
  17. {
  18. temp=a;
  19. a=b;
  20. b=temp;
  21. }
  22.  
  23. for (i=0;i<=max;i++)
  24.  
  25. {
  26. fa=a*a*a-3*a-1;
  27. fb=b*b*b-3*b-1;
  28. t=1.0;
  29. c=(b*fa-a*fb)/(fa-fb);
  30. fc=c*c*c-3*c-1;
  31. f=(c-t)/c;
  32. g=fabs(f);
  33.  
  34. if (g <= y)
  35.  
  36. break;
  37.  
  38. else 
  39. if (fa*fc > 0.0)
  40. {
  41. t=c;
  42. a=c;
  43. }
  44. else
  45. {
  46. t=c;
  47. b=c;
  48. }
  49.  
  50. }
  51. if (c<=b && c>=a)
  52. cout<<c<<setprecision(z)<<fixed<<end…
  53. else
  54. cout<<"There's no real root between "<<a<<" and "<<b<<endl;
  55.  
  56.  
  57.  
  58. system ("pause");
  59. return 0;
  60. }
Jul 28 '11 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
setprecision has two meanings. A: for floating-point default the value specifies the number of digits before and after the decimal point. B: For fixed (scientific) notation the value specifies the number of decimal places.

The setprecision I see appears to to be case A above.
Jul 28 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.