0

I was trying to do some sums and i faced this problem , i wonder if there a way fix it?

here's my code

#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
float a=(3.0/2.0)+2;          // a = 3.5
 float b=(5.5/2.0)+2;         // b = 4.8
  float c=((a+b)/2.0)+2;      // c = 6.1
   float d=((3.5+4.8)/2)+2;   // d = 6.2
   cout<<"a = "<<fixed << setprecision(1)<<a<<endl;
   cout<<"b = "<<fixed << setprecision(1)<<b<<endl;
  cout<<"c = "<<fixed << setprecision(1)<<c<<endl;
 cout<<"d = "<<fixed << setprecision(1)<<d<<endl;
system("pause");
return 0;
}

the result will be:

a = 3.5
b = 4.8
c = 6.1
d = 6.2

How can i make c= 6.2 ?

newbahama
  • 1
  • 1
  • Please read this http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html – Ed Heal Jul 13 '14 at 06:25
  • barak manos no difference – newbahama Jul 13 '14 at 06:26
  • And your comments are wrong, b is 4.75 and c is 6.125. Don't confuse the output with the actual values (a debugger speaks louder than your console ever could). – WhozCraig Jul 13 '14 at 06:27
  • As written in the title i want the variable to be more fixed instead of setprecision(2) i made it setprecision(1) – newbahama Jul 13 '14 at 06:35
  • I think the question is how to make a (floating point) variable behave as "fixed point" decimal variable, although it seems clear that the OP is still learning C++ so the question is not well poised. I don't think this is a duplicate question. – Mark Lakata Jul 13 '14 at 07:19
  • Note that any C++ fixed-point classes are designed to be *binary* fixed point, so that they are not of any use here. If the OP really wants to use a fixed-point decimal representation, then they could simply use string representations for storage of the values, rather than binary storage. – Mark Lakata Jul 13 '14 at 07:22
  • No its not a duplicated question :( Please Mark Lakata Explain what your comment as an answer – newbahama Jul 13 '14 at 07:25

0 Answers0