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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
#include "common.hpp"
#include <fpm/math.hpp>
TEST(trigonometry, sin)
{
using P = fpm::fixed<std::int32_t, std::int64_t, 16>;
const double PI = std::acos(-1);
constexpr auto MAX_ERROR_PERC = 0.002;
for (int angle = -1799; angle <= 1800; ++angle)
{
auto flt_angle = angle * PI / 180;
auto sin_real = std::sin(flt_angle);
auto sin_fixed = static_cast<double>(sin(P(flt_angle)));
EXPECT_TRUE(HasMaximumError(sin_fixed, sin_real, MAX_ERROR_PERC));
}
}
TEST(trigonometry, cos)
{
using P = fpm::fixed<std::int32_t, std::int64_t, 16>;
const double PI = std::acos(-1);
constexpr auto MAX_ERROR_PERC = 0.002;
for (int angle = -1799; angle <= 1800; ++angle)
{
auto flt_angle = angle * PI / 180;
auto cos_real = std::cos(flt_angle);
auto cos_fixed = static_cast<double>(cos(P(flt_angle)));
EXPECT_TRUE(HasMaximumError(cos_fixed, cos_real, MAX_ERROR_PERC));
}
}
TEST(trigonometry, tan)
{
using P = fpm::fixed<std::int32_t, std::int64_t, 16>;
const double PI = std::acos(-1);
constexpr auto MAX_ERROR_PERC = 0.002;
for (int angle = -1799; angle <= 1800; ++angle)
{
// Tangent goes to infinite at 90 and -90 degrees.
// We can't represent that with fixed-point maths, so don't test for it.
if ((angle + 90) % 180 != 0)
{
auto flt_angle = angle * PI / 180;
auto tan_real = std::tan(flt_angle);
auto tan_fixed = static_cast<double>(tan(P(flt_angle)));
EXPECT_TRUE(HasMaximumError(tan_fixed, tan_real, MAX_ERROR_PERC));
}
}
#ifndef NDEBUG
EXPECT_DEATH(tan(P::pi()/2), "");
EXPECT_DEATH(tan(-P::pi()/2), "");
#endif
}
TEST(trigonometry, atan)
{
using P = fpm::fixed<std::int32_t, std::int64_t, 12>;
constexpr auto MAX_ERROR_PERC = 0.025;
for (int x = -1000; x <= 1000; ++x)
{
auto value = x / 10.0;
auto atan_real = std::atan(value);
auto atan_fixed = static_cast<double>(atan(P(value)));
EXPECT_TRUE(HasMaximumError(atan_fixed, atan_real, MAX_ERROR_PERC));
}
for (int x = -1000; x <= 1000; ++x)
{
auto value = x / 1000.0;
auto atan_real = std::atan(value);
auto atan_fixed = static_cast<double>(atan(P(value)));
EXPECT_TRUE(HasMaximumError(atan_fixed, atan_real, MAX_ERROR_PERC));
}
}
TEST(trigonometry, asin)
{
using P = fpm::fixed<std::int32_t, std::int64_t, 12>;
constexpr auto MAX_ERROR_PERC = 0.025;
for (int x = -1000; x <= 1000; ++x)
{
auto value = x / 1000.0;
auto asin_real = std::asin(value);
auto asin_fixed = static_cast<double>(asin(P(value)));
EXPECT_TRUE(HasMaximumError(asin_fixed, asin_real, MAX_ERROR_PERC));
}
}
TEST(trigonometry, acos)
{
using P = fpm::fixed<std::int32_t, std::int64_t, 12>;
constexpr auto MAX_ERROR_PERC = 0.025;
for (int x = -1000; x <= 1000; ++x)
{
auto value = x / 1000.0;
auto acos_real = std::acos(value);
auto acos_fixed = static_cast<double>(acos(P(value)));
EXPECT_TRUE(HasMaximumError(acos_fixed, acos_real, MAX_ERROR_PERC));
}
}
TEST(trigonometry, atan2)
{
using P = fpm::fixed<std::int32_t, std::int64_t, 12>;
const double PI = std::acos(-1);
constexpr auto MAX_ERROR_PERC = 0.025;
for (int angle = -1799; angle <= 1800; ++angle)
{
const auto y = std::sin(angle * PI / 1800);
const auto x = std::cos(angle * PI / 1800);
auto atan2_real = std::atan2(y, x);
auto atan2_fixed = static_cast<double>(atan2(P(y), P(x)));
EXPECT_TRUE(HasMaximumError(atan2_fixed, atan2_real, MAX_ERROR_PERC));
}
#ifndef NDEBUG
EXPECT_DEATH(atan2(P(0), P(0)), "");
#endif
}
// Naively, atan2(y, x) does y / x which would overflow for near-zero x with Q16.16.
// Test that we've got protections in place for this.
TEST(trigonometry, atan2_near_zero)
{
constexpr auto MAX_ERROR_PERC = 0.025;
using P = fpm::fixed_16_16;
const auto x = P::from_raw_value(1);
const auto y = P(100);
// Positive x
{
auto atan2_real = std::atan2(static_cast<double>(y), static_cast<double>(x));
auto atan2_fixed = static_cast<double>(atan2(y, x));
EXPECT_TRUE(HasMaximumError(atan2_fixed, atan2_real, MAX_ERROR_PERC));
}
// Negative x
{
auto atan2_real = std::atan2(static_cast<double>(y), static_cast<double>(-x));
auto atan2_fixed = static_cast<double>(atan2(y, -x));
EXPECT_TRUE(HasMaximumError(atan2_fixed, atan2_real, MAX_ERROR_PERC));
}
}
|