Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 540c076

Browse files
committed
add extra tests for comparing floating point values
- __equals() function is overloaded to handle comparisons between double and vector<double> according to TopCoder standards
1 parent a99a612 commit 540c076

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

‎src/com/dogcows/resources/C++Driver

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
#include "$CLASSNAME$.cc"
33

4+
#include <algorithm>
5+
#include <cmath>
46
#include <cstdlib>
57
#include <fstream>
68
#include <iostream>
@@ -12,6 +14,7 @@
1214
using namespace std;
1315

1416

17+
const static double __EPSILON = 1e-9;
1518
static double __time = 0.0;
1619

1720
static void __timer_start()
@@ -99,6 +102,38 @@ std::istream& operator >> (std::istream& in, std::vector<T>& vec)
99102
return in;
100103
}
101104

105+
template <class T>
106+
bool __equals(const T& actual, const T& expected)
107+
{
108+
return actual == expected;
109+
}
110+
111+
bool __equals(double actual, double expected)
112+
{
113+
if (std::abs(actual - expected) < __EPSILON)
114+
{
115+
return true;
116+
}
117+
else
118+
{
119+
double minimum = std::min(expected * (1.0 - __EPSILON), expected * (1.0 + __EPSILON));
120+
double maximum = std::max(expected * (1.0 - __EPSILON), expected * (1.0 + __EPSILON));
121+
return actual > minimum && actual < maximum;
122+
}
123+
}
124+
125+
bool __equals(const std::vector<double>& actual, const std::vector<double>& expected)
126+
{
127+
if (actual.size() != expected.size())
128+
return false;
129+
130+
for (size_t i = 0; i < actual.size(); ++i)
131+
if (!__equals(actual[i], expected[i]))
132+
return false;
133+
134+
return true;
135+
}
136+
102137

103138
int main(int argc, char* argv[])
104139
{
@@ -127,7 +162,7 @@ int main(int argc, char* argv[])
127162

128163
double __t = __timer_stop();
129164

130-
if (__actual == __expected)
165+
if (__equals(__actual, __expected))
131166
{
132167
std::cout << "[PASS] in " << __t << " seconds." << std::endl;
133168
++__pass;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /