Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Integer conversions
#include <iostream> #include <type_traits> #include <limits> /** * The size of a integer types domain is 2^digits - 1 * The ratio between two types is then (2^d1 - 1) / (2^d2 - 1) * which for power of 2 number of digits can be written as: * * (-1 + x)(1+x^1)(1+x^2)(1+x^4)(1+x^8)...(1+x^d1/2) * -------------------------------------------------- * (-1 + x)(1+x^1)(1+x^2)(1+x^4)(1+x^8)...(1+x^d2/2) * * assuming that d1 > d2 can be simplified to: * * (1+x^d2)...(1+x^d1/2) */ /* Recursive implementation * / (2^D1+1) * f(D1*2, D2) if D1 != D2 * f(D1,D2) = { * \ 1 if D1 == D2 */ template <typename T, size_t D1, size_t D2> struct ratioImpl { static const T value = ((T{1} << D2) + T{1}) * ratioImpl<T, D1, D2*2>::value; }; // termination. template <typename T, size_t D> struct ratioImpl<T, D, D>{ static const T value = T{1}; }; /* * Calculate the ratio between to Integer domain sizes. * The number of digits in "To" should always be the larger * or equal to the number digits in "From". */ template <typename To, typename From> struct ratio { static_assert(std::numeric_limits<To>::digits >= std::numeric_limits<From>::digits, "Invalid type order: To is smaller then From"); static const To value = ratioImpl<To, std::numeric_limits<To>::digits, std::numeric_limits<From>::digits>::value; }; int main() { std::cout << "char->char " << (int)ratio<unsigned char, unsigned char>::value << std::endl; std::cout << "char->short " << ratio<unsigned short, unsigned char>::value << std::endl; std::cout << "char->int " << ratio<unsigned int, unsigned char>::value << std::endl; std::cout << "char->longlong " << ratio<unsigned long long, unsigned char>::value << std::endl; std::cout << std::endl; std::cout << "short->char " << 1.0 / ratio<unsigned short, unsigned char>::value << std::endl; std::cout << "short->short " << ratio<unsigned short, unsigned short>::value << std::endl; std::cout << "short->int " << ratio<unsigned int, unsigned short>::value << std::endl; std::cout << "short->longlong " << ratio<unsigned long long, unsigned short>::value << std::endl; std::cout << std::endl; std::cout << "int->char " << 1.0 / ratio<unsigned int, unsigned char>::value << std::endl; std::cout << "int->short " << 1.0 / ratio<unsigned int, unsigned short>::value << std::endl; std::cout << "int->int " << ratio<unsigned int, unsigned int>::value << std::endl; std::cout << "int->longlong " << ratio<unsigned long long, unsigned int>::value << std::endl; std::cout << std::endl; std::cout << "longlong->char " << 1.0 / ratio<unsigned long long, unsigned char>::value << std::endl; std::cout << "longlong->short " << 1.0 / ratio<unsigned long long, unsigned short>::value << std::endl; std::cout << "longlong->int " << 1.0 / ratio<unsigned long long, unsigned int>::value << std::endl; std::cout << "longlong->longlong " << ratio<unsigned long long, unsigned long long>::value << std::endl; }
run
|
edit
|
history
|
help
0
Narrowing error
overloading
function pointer
ArrayList Example Starter Code 2
Erase a std::unordered_map::local_iterator by key
HTML Timetable generator.cpp
IsContainer
user defined exception
LOOL
Program to Time Difference