-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.hpp
92 lines (63 loc) · 2.13 KB
/
test.hpp
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
81
82
83
84
85
86
87
88
89
90
91
92
#pragma once
#include <cmath>
#include <complex>
#ifndef M_PI
#define M_PI 3.1415926535897932
#endif
#ifndef M_PIL
#define M_PIL 3.14159265358979323846264338327950288L
#endif
#define CHECK_CLOSE(a,b,eps) \
do { \
if (std::isinf(a) && std::isinf(b)) { \
CHECK(true); \
} else { \
CHECK((a) == doctest::Approx(b).epsilon(eps)); \
} \
} while (0);
#define CHECK_CLOSE_COMPLEX(a,b,eps) do { \
CHECK_CLOSE(std::real(a), std::real(b), (eps)); \
CHECK_CLOSE(std::imag(a), std::imag(b), (eps)); \
} while (0)
#define CHECK_SMALL(a,eps) CHECK(std::abs(a) < (eps))
#define CHECK_CLOSE_REL(a,b,eps) do { \
const bool pred = is_close_rel((a), (b), (eps)); \
INFO("Comparing numbers " << std::setprecision(17) << (a) << " =?= " << (b) << " with relative precision " << (eps)); \
CHECK(pred); \
} while (0)
inline bool has_inf() noexcept
{
const auto fn = [] (bool return_inf) { return return_inf ? std::numeric_limits<double>::infinity() : 0.0; };
return std::isinf(fn(true));
}
inline bool has_signed_zero() noexcept
{
const auto fn = [] (double x) { return x == 0.0 ? x : x; };
return std::signbit(fn(-0.0));
}
inline bool is_ieee754_compliant() noexcept
{
return has_inf() && has_signed_zero();
}
inline bool is_close_rel(double x, double y, double eps) noexcept
{
const double ma = std::max(std::abs(x), std::abs(y));
if (ma == 0.0) {
return true;
}
return std::abs(x - y)/ma < std::abs(eps);
}
template <class T> T sqr(T x) {
return x*x;
}
template <class T> T pow3(T x) {
return x*x*x;
}
template <class T> T pow4(T x) {
return sqr(sqr(x));
}
template <typename T, typename U>
std::complex<T> to(std::complex<U> z)
{
return std::complex<T>(static_cast<T>(std::real(z)), static_cast<T>(std::imag(z)));
}