-11

I have add the two double number.

double a=4.0;
double b=4.0;
cout<<a+b;

Answer is 8 only but required 8.0.

Alireza
  • 100,211
  • 27
  • 269
  • 172
Bilal Siddiq
  • 117
  • 1
  • 1
  • 10
  • 6
    what precision have you lost? – TZHX Jul 14 '17 at 10:32
  • 6
    Please be aware that 8.0 and 8 are the exact same numbers - they would have the same bitwise representation. They are indistinguishable. If you want to specify an *output format* that's a different matter. – Jon Skeet Jul 14 '17 at 10:35
  • but i have solve the hackerrank problem . where test case accept the 8.0 value not 8. he is not submit the answer – Bilal Siddiq Jul 14 '17 at 10:39
  • 1
    @BilalSiddiq I will repeat what already was said: you are having issue with specifying **output format**, and **not** with the actual precision of the number, as is suggested by the answer. In addition: if you can't understand what comments are saying, there's no need to tell us, that we don't know the answer. – Algirdas Preidžius Jul 14 '17 at 10:54
  • Here is my code int main() { double d = 4.0; double doubleValue; cin>>doubleValue; cout< – Bilal Siddiq Jul 14 '17 at 10:57
  • 1
    The problem is **not** the addition; all of the values involved can be exactly represented as floating-point values. The problem is in how you **display** the result. – Pete Becker Jul 14 '17 at 11:27
  • the answer required 8.0 not 8 – Bilal Siddiq Jul 14 '17 at 11:47

1 Answers1

2

I believe what you want is something like this:

cout << std::setprecision(1) << a+b;
msc
  • 33,420
  • 29
  • 119
  • 214
Hashibuto
  • 748
  • 6
  • 10