Compile temporary C ++ calculation templates to unsigned long long? on two-local?

I use int compile-time Power calculation of power to calculate n ** p. It uses ints. I want to calculate something more than int, ok? It fits into u64 (unsigned long long). Can C ++ templates do calculations at compile time on u64? Enums cannot do this. In doubles? Maybe?

I would really like the type to be a template argument. Is it possible? My compiler is not C ++ 0x.

Thanks Andrew

template<int N, int P> struct Power {
   enum { val = N * Power<N, P-1>::val };
};

template<int N> struct Power<N, 0> {
   enum { val = 1 };
};

int result = Power<2, 5>; // 2**5 == 32
+3
source share
4 answers

, . , . ++ 0x , , .

+3
template<int N, unsigned int P> struct Power {
   static const unsigned long long val = N * Power<N, P-1>::val;
};

template<int N> struct Power<N, 0> {
   static const unsigned long long val = 1;
}

, P a unsigned int, .

+2

sbi, , ( ).

, 2, (.. 2**x == 1 << x), .

#include <iostream>

template <unsigned long long N, unsigned int P, int Odd = (P&1)> struct Power;

template <unsigned long long N, unsigned int P>
struct Power<N,P,0> { // even (square N and halve the power)
    static const unsigned long long val = Power<N*N,(P/2)>::val;
};

template <unsigned long long N, unsigned int P>
struct Power<N,P,1> { // odd (multiply by N and decrement the power)
    static const unsigned long long val = N * Power<N,(P-1)>::val;
};

template <unsigned long long N>
struct Power<N,0,0> { // zero (x**0 is 1 for all x != 0)
    static const unsigned long long val = 1;
};

int main() {
    std::cout << "2**0 = " << Power<2,0>::val << "\n";
    std::cout << "2**1 = " << Power<2,1>::val << "\n";
    std::cout << "2**2 = " << Power<2,2>::val << "\n";
    std::cout << "2**3 = " << Power<2,3>::val << "\n";
    std::cout << "2**4 = " << Power<2,4>::val << "\n";
    std::cout << "2**5 = " << Power<2,5>::val << "\n";
    std::cout << "2**6 = " << Power<2,6>::val << "\n";
    std::cout << "2**7 = " << Power<2,7>::val << "\n";
    std::cout << "2**8 = " << Power<2,8>::val << "\n";
    std::cout << "2**9 = " << Power<2,9>::val << "\n";
    std::cout << "2**10 = " << Power<2,10>::val << "\n";
    std::cout << "2**11 = " << Power<2,11>::val << "\n";
    std::cout << "2**12 = " << Power<2,12>::val << "\n";
    std::cout << "2**30 = " << Power<2,30>::val << "\n";
    std::cout << "2**40 = " << Power<2,40>::val << "\n";
    std::cout << "2**50 = " << Power<2,50>::val << "\n";
    std::cout << "2**60 = " << Power<2,60>::val << "\n";
    return 0;
}

: , - ( ). . , , -, Odd Power<>.

+2

You can use math with high precision to calculate large numbers (in my example below I use 96-bit calculations with three template parameters, you can use any constant number). You must have more than one integer as a template parameter.

When doing multiplication during compilation, you should probably multiply 32-bit numbers with 64-bit results; The result should be divided into two template parameters.

Overflow checking is possible, but can be difficult.

const uint64_t W = 1000000000; // word size: 2^32 is best; any smaller number is OK
// I use a power of 10 as word size for ease of printing (see main() below)

// The following class performs multiplication of (n0 + W*n1 + W*W*n2) by (base)
template <unsigned n0, unsigned n1, unsigned n2, uint64_t base, unsigned p> class power_temp
{
    typedef power_temp<
        n0 * base % W,
        n1 * base % W + n0 * base / W,
        n2 * base % W + n1 * base / W,
        base, p - 1> mult_type;
public:
    static const unsigned x0 = mult_type::x0;
    static const unsigned x1 = mult_type::x1;
    static const unsigned x2 = mult_type::x2;
};

// The following partial specialization is used to end recursion
template <unsigned n0, unsigned n1, unsigned n2, uint64_t base>
class power_temp<n0, n1, n2, base, 0>
{
public:
    static const unsigned x0 = n0;
    static const unsigned x1 = n1;
    static const unsigned x2 = n2;
};

// The following class calculates a power, using compile-time calculations
template <unsigned base, unsigned p> struct power
{
    static const unsigned x0 = power_temp<1, 0, 0, base, p>::x0;
    static const unsigned x1 = power_temp<1, 0, 0, base, p>::x1;
    static const unsigned x2 = power_temp<1, 0, 0, base, p>::x2;
};

int main()
{
    typedef power<123456789, 3> my1;
    printf("%09d%09d%09d\n", my1::x2, my1::x1, my1::x0);

    typedef power<5, 33> my2;
    printf("%09d%09d%09d\n", my2::x2, my2::x1, my2::x0);
}
+2
source

Source: https://habr.com/ru/post/1783718/


All Articles