Skip to content

How to use as scaled decimal number (1.x) #991

@vrqq

Description

@vrqq

When I try to use this code below, the complier error occurred when std::cout.

using DecX = cnl::scaled_integer<cnl::rounding_integer<long long>, cnl::power<-1, 100>>;
DecX num1 = 10.554;
DecX num2 = 10.545;
std::cout<<num1<<" "<<num2<<std::endl;

Clang 12 with libstdc++ and c++ 17

In file included from ../third/cnl-1.1.7/include/cnl/_impl/scaled_integer/extras.h:22:
../third/cnl-1.1.7/include/cnl/_impl/scaled_integer/to_chars.h:52:24: error: no matching function for call to 'scale'
                return scale<Exponent, Radix>(to_rep(scalar));
                       ^~~~~~~~~~~~~~~~~~~~~~
../third/cnl-1.1.7/include/cnl/_impl/scaled_integer/to_chars.h:66:48: note: in instantiation of member function 'cnl::_impl::split<cnl::_impl::number<long long, cnl::native_rounding_tag>, -1, 100, false>::integral' requested here
                return from_integral_and_value(integral(value), value);
                                               ^
../third/cnl-1.1.7/include/cnl/_impl/scaled_integer/to_chars.h:207:32: note: in instantiation of member function 'cnl::_impl::split<cnl::_impl::number<long long, cnl::native_rounding_tag>, -1, 100, false>::operator()' requested here
            auto const split = _impl::split<Rep, Exponent, Radix>{}(value);
                               ^
../third/cnl-1.1.7/include/cnl/_impl/scaled_integer/../to_chars.h:88:28: note: in instantiation of function template specialization 'cnl::_impl::to_chars_positive<cnl::_impl::number<long long, cnl::native_rounding_tag>, -1, 100>' requested here
                    return to_chars_positive(first, last, value);
                           ^
../third/cnl-1.1.7/include/cnl/_impl/scaled_integer/to_chars.h:242:16: note: in instantiation of member function 'cnl::_impl::to_chars_non_zero<cnl::scaled_integer<cnl::_impl::number<long long, cnl::native_rounding_tag>, cnl::power<-1, 100>>, true>::operator()' requested here
        return _impl::to_chars_non_zero<native_rounding_type>{}(
               ^
../third/cnl-1.1.7/include/cnl/_impl/scaled_integer/../to_chars.h:120:23: note: in instantiation of function template specialization 'cnl::to_chars<cnl::_impl::number<long long, cnl::nearest_rounding_tag>, -1, 100>' requested here
        auto result = to_chars(chars.data(), chars.data()+max_num_chars, value);
                      ^
../third/cnl-1.1.7/include/cnl/_impl/scaled_integer/extras.h:210:23: note: in instantiation of function template specialization 'cnl::to_chars<cnl::scaled_integer<cnl::_impl::number<long long, cnl::nearest_rounding_tag>, cnl::power<-1, 100>>>' requested here
        return out << to_chars(fp).data();
                      ^
../study/try_cnl.cpp:43:9: note: in instantiation of function template specialization 'cnl::operator<<<cnl::_impl::number<long long, cnl::nearest_rounding_tag>, -1, 100>' requested here
    cout<<num1<<" "<<num2<<endl;
        ^
../third/cnl-1.1.7/include/cnl/_impl/number/../num_traits/scale.h:52:38: note: candidate template ignored: substitution failure [with Digits = -1, Radix = 100, S = cnl::_impl::number<long long, cnl::native_rounding_tag>]: implicit instantiation of undefined template 'cnl::scale<-1, 100, cnl::_impl::number<long long, cnl::native_rounding_tag>>'
        [[nodiscard]] constexpr auto scale(S const& s)
                                     ^
1 warning and 2 errors generated.

Then I check the source code at: cnl/rounding_integer.h:63
I am confusing that why can't be default_scale<(Digits < 0), (Radix != 2)>?
(tag v1.7, also v1.x branch currently)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions