Recently while practicing I found this problem, Please run this code in your local machine.
#include <bits/stdc++.h>
using namespace std;
double power(double a, double b) {
double ans = 1;
for(int i = 1; i <= b; i++){
ans *= a;
}
return ans;
}
int main()
{
double anaconda = 0;
for(int i = 0; i < 25; i++){
anaconda += power(10,i);
cout << setprecision(30) << fixed <<i << " "<< anaconda << endl;
}
}
If you run this code you should see something like this
Your can also see this code running in IDEONE You can see there from i = 16 , the program is giving wrong output. There should be all 1s in every number. Can anyone please tell me
- Why this is giving wrong result even in this small range ?
- And how to avoid this type of calculation problem ?
Thanks in advance ❤️❤️❤️