-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathprecision_checker.cpp
More file actions
80 lines (75 loc) · 1.73 KB
/
precision_checker.cpp
File metadata and controls
80 lines (75 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
//Set the precision limit you want for comparing the 2 output files
const double EPS = 1e-7;
int main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
ifstream output1;
ifstream output2;
output1.open("out.txt", ios::in);
output2.open("res.txt", ios::in);
if (!output1) {
cout<<"Can't open output file 1\n";
return 0;
}
if (!output2) {
cout<<"Can't open output file 2\n";
return 0;
}
int lines = 1;
string str1, str2;
double a, b, decimal;
bool point_found;
double error;
while (getline(output1, str1)) {
getline(output2, str2);
a = 0.0, decimal = 1.0;
point_found = false;
for(int i=0; i<str1.length(); ++i) {
if(str1[i] == '.') {
point_found = true;
decimal = 1.0;
}
if(point_found) {
a = a + ((str1[i] - '0') * decimal / 10);
decimal /= 10;
}
else {
a = a * 10.0 + (str1[i] - '0');
}
}
b = 0.0, decimal = 1.0;
point_found = false;
for(int i=0; i<str2.length(); ++i) {
if(str2[i] == '.') {
point_found = true;
decimal = 1.0;
}
if(point_found) {
b = b + ((str2[i] - '0') * decimal / 10);
decimal /= 10;
}
else {
b = b * 10.0 + (str2[i] - '0');
}
}
error = (abs(a - b) / a) / b;
if (error > EPS) {
cout.precision(10);
cout << "Error at line number " << lines << " is more than EPS set\n";
cout << "Actual error found : " << fixed << error << "\n";
cout << "Limit on error set : " << fixed << EPS << "\n";
return 0;
}
lines += 1;
}
cout << "Scanned a total of " << (lines - 1) << " lines from both files\n";
cout << "Both output files are same\n";
output1.close();
output2.close();
return 0;
}