Skip to content

Commit

Permalink
Try to fix issue #991
Browse files Browse the repository at this point in the history
  • Loading branch information
vrqq authored and John McFarlane committed Dec 22, 2023
1 parent aedf166 commit 0ff81a4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 12 deletions.
12 changes: 0 additions & 12 deletions include/cnl/_impl/num_traits/scale.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,6 @@ namespace cnl {
: _impl::default_scale<Digits, Radix, S> {
};

template<int Digits, int Radix, template<typename, typename>
class TNUM, typename S, typename Tag>
struct scale<Digits, Radix, TNUM<S, Tag>, _impl::enable_if_t<(
Digits<0 && cnl::_impl::is_integral<S>::value)>>
{
CNL_NODISCARD constexpr auto operator()(TNUM<S, Tag> const& s) const
-> decltype(s/_impl::power_value<S, -Digits, Radix>())
{
return s/_impl::power_value<S, -Digits, Radix>();
}
};

namespace _impl {
// cnl::_impl::scale - convenience wrapper for cnl::scale
template<int Digits, int Radix=2, class S>
Expand Down
10 changes: 10 additions & 0 deletions include/cnl/rounding_integer.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ namespace cnl {
scale<Digits, Radix, Rep>{}(_impl::to_rep(s)));
}
};

template<int Digits, int Radix, class Rep, class RoundingTag>
struct scale<Digits, Radix, _impl::number<Rep, RoundingTag>,
_impl::enable_if_t<Digits < 0 && _impl::is_rounding_tag<RoundingTag>::value>> {
CNL_NODISCARD constexpr auto operator()(_impl::number<Rep, RoundingTag> const& s) const
-> decltype(s/_impl::power_value<Rep, -Digits, Radix>())
{
return s/_impl::power_value<Rep, -Digits, Radix>();
}
};

////////////////////////////////////////////////////////////////////////////////
// cnl::set_rep<rounding_integer, Rep>
Expand Down
45 changes: 45 additions & 0 deletions test/unit/scaled_integer/rounding/rounding_scaled_integer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,49 @@ namespace {
DecX num2 = 10.545;
std::cout<<num1<<" "<<num2<<std::endl;
}

TEST(rounding_scaled_integer, 991AssignTest)
{
using DecX_2 = cnl::scaled_integer<cnl::rounding_integer<long long>, cnl::power<-2, 10>>;
using DecX_4 = cnl::scaled_integer<cnl::rounding_integer<long long>, cnl::power<-4, 10>>;

DecX_2 n1 = 10.0151;
DecX_2 n2 = 10.0249;
EXPECT_EQ(n1, n2); // 10.02

int64_t xn = (int64_t)n1;
double xm = (double)n2;
EXPECT_EQ(xn, 10);
EXPECT_DOUBLE_EQ(xm, 10.02);

static_assert(std::is_same_v<decltype(n1*n2), DecX_4>);
EXPECT_EQ(n1*n2, DecX_4{100.4004});
EXPECT_DOUBLE_EQ((double)(n1*n2), 100.4004);

n1 += 0.6;
EXPECT_EQ((int)n1, 11);

n1 += 0.38;
EXPECT_EQ(n1, 11);

xn += n1;
xm -= n1;
}

TEST(rounding_scaled_integer, 991CmpTest)
{
using DecX_2 = cnl::scaled_integer<cnl::rounding_integer<long long>, cnl::power<-2, 10>>;

DecX_2 n1;
DecX_2 n2 = 10.019;
DecX_2 n3{20.029};
EXPECT_DOUBLE_EQ((double)n2, 10.02);
EXPECT_DOUBLE_EQ((double)n3, 20.03);

n1 = 3.999; //4.00 instead
n3 = DecX_2(8);
EXPECT_NE(n1, 3.9899999999999999999999999);
EXPECT_EQ(n1, 4.0000000000000000000000001);
EXPECT_EQ(n3, 8);
}
}

0 comments on commit 0ff81a4

Please sign in to comment.