1
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
    float a=3.14159*22500;
    cout<<fixed<<setprecision(4)<<a;
}

Output I am getting is 70685.7734 and the actual answer is 70685.7750, how can I modify my code to get the correct output?

Prithvish111
  • 106
  • 7
  • 1
    A typical `float` only gives you about 6 or 7 digits of decimal precision. Due to that sparseness, the closest `float` to the true value is selected. – Bathsheba Apr 28 '20 at 05:46
  • 3
    It's kind of ironic that a question about the accuracy of floating point arithmetic uses a value of `3.14159` for pi. – john Apr 28 '20 at 05:49
  • You will get better output if you use `double` instead of `float`. But there is no answer to this issue, because many correct answers are simply not representable as floating point values. – john Apr 28 '20 at 05:52
  • But I need this answer to pass my test case – Prithvish111 Apr 28 '20 at 05:54
  • Test cases should not be be checking floating point values for equality (unless you are actually testing the quality of floating point arithmetic). – john Apr 28 '20 at 05:56
  • Okay, double worked. – Prithvish111 Apr 28 '20 at 05:56

0 Answers0