Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/epsilon/r.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ constexpr r<C> add(r<C> x, r<C> y) {
};
}

template <container C>
constexpr r<C> opp(r<C> x) {
return [x = std::move(x)](unsigned int n) -> coro::lazy<z<C>> {
auto xn = co_await x(n);
negate(xn);
co_return xn;
};
}

} // namespace epx

#endif // EPSILON_INC_R_HPP
3 changes: 3 additions & 0 deletions src/epsilon/z.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ constexpr z<C>& normalize(z<C>& num) noexcept {

template <container C>
constexpr z<C>& negate(z<C>& num) noexcept {
if (is_zero(num)) {
return num;
}
num.sgn = is_positive(num) ? sign::negative : sign::positive;
return num;
}
Expand Down
20 changes: 20 additions & 0 deletions src/ut/r_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,24 @@ TEST(r_tests, add) {
}
}

TEST(r_tests, opp) {
{
auto x = epx::make_q(stosz("0"), stosz("1"));
auto expr = epx::opp(x);
EXPECT_EQ("0", epx::to_string(expr, 0));
EXPECT_EQ("0.00000", epx::to_string(expr, 5));
}
{
auto x = epx::make_q(stosz("1"), stosz("1"));
auto expr = epx::opp(x);
EXPECT_EQ("-1", epx::to_string(expr, 0));
EXPECT_EQ("-1.00000", epx::to_string(expr, 5));
}
{
auto x = epx::make_q(stosz("-1"), stosz("7"));
auto expr = epx::opp(x);
EXPECT_EQ("0.14285714285714285714286", epx::to_string(expr, 23));
}
}

} // namespace epxut
20 changes: 20 additions & 0 deletions src/ut/z_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ TEST(z_tests, normalize) {
}
}

TEST(z_tests, negate) {
{
sz zero;
epx::negate(zero);
EXPECT_TRUE(epx::is_zero(zero));
}
{
sz num = {.digits = {1}};
sz expected = {.digits = {1}, .sgn = epx::sign::negative};
epx::negate(num);
EXPECT_EQ(expected, num);
}
{
sz num = {.digits = {1}, .sgn = epx::sign::negative};
sz expected = {.digits = {1}};
epx::negate(num);
EXPECT_EQ(expected, num);
}
}

TEST(z_tests, add) {
{
sz zero, one{.digits = {1}}, minus_one{.digits = {1}, .sgn = epx::sign::negative};
Expand Down