ratio
Overview
The estd::ratio
provides compile-time rational arithmetic support.
This template is used to instantiate types that represent a finite rational number denoted by a numerator and a denominator, which are implemented as compile-time constants of type intmax_t. The ratio is not represented by an object of this type but by the type itself, which uses compile-time constant members to define the ratio. Therefore, ratio can only be used to express constants and cannot contain any value.
N - Numerator. D - Denominator. Their absolute values shall be in the range of representable values of intmax_t, which is the widest signed integer type, and D shall not be zero.
The static constants num and den are not the same as the template arguments N and D if the greatest common divisor among N and D is not one, as the num and den are the results of dividing N and D by that greatest common divisor.
Usage
The usage constraints and guidelines apply for ratio:
Constraints
ratio
with zero denominator cannot be created. Static assertion is thrown if attempt to create is made.
Example
The estd::ratio.h
provides compile-time rational arithmetic support.
Following example shows the basic functionalities of ratio:
// num is used to get the numerator of a ratio.
static_assert((3 == ::estd::ratio<3, 5>::num), "");
// den is used to get the denominator of a ratio.
static_assert((5 == ::estd::ratio<3, 5>::den), "");
static_assert(std::is_same<::estd::ratio<1, 5>::type, ::estd::ratio<1, 5>>::value, "");
static_assert(std::is_same<::estd::ratio<10, 50>::type, ::estd::ratio<1, 5>>::value, "");
static_assert(std::is_same<::estd::ratio<-1, -5>::type, ::estd::ratio<1, 5>>::value, "");
static_assert(std::is_same<::estd::ratio<-10, 50>::type, ::estd::ratio<-1, 5>>::value, "");
Usage example of comparison operator in ratio:
static_assert(::estd::ratio_equal<::estd::ratio<1, 5>, ::estd::ratio<2, 10>>::value, "");
static_assert(!::estd::ratio_not_equal<::estd::ratio<1, 5>, ::estd::ratio<2, 10>>::value, "");
static_assert(!::estd::ratio_less<::estd::ratio<1, 5>, ::estd::ratio<2, 10>>::value, "");
static_assert(!::estd::ratio_greater<::estd::ratio<1, 5>, ::estd::ratio<2, 10>>::value, "");
static_assert(::estd::ratio_less_equal<::estd::ratio<1, 5>, ::estd::ratio<2, 10>>::value, "");
static_assert(
::estd::ratio_greater_equal<::estd::ratio<1, 5>, ::estd::ratio<2, 10>>::value, "");
Usage example of arithmetic operator in ratio:
{
static_assert(
std::is_same<
::estd::ratio<11, 30>,
::estd::ratio_add<::estd::ratio<1, 5>, ::estd::ratio<1, 6>>::type>::value,
"");
static_assert(
std::is_same<
::estd::ratio<1, 30>,
::estd::ratio_subtract<::estd::ratio<1, 5>, ::estd::ratio<1, 6>>::type>::value,
"");
static_assert(
std::is_same<
::estd::ratio<7, 15>,
::estd::ratio_multiply<::estd::ratio<2, 5>, ::estd::ratio<7, 6>>::type>::value,
"");
static_assert(
std::is_same<
::estd::ratio<4, 3>,
::estd::ratio_divide<::estd::ratio<2, 4>, ::estd::ratio<3, 8>>::type>::value,
"");
}
Example of some predefined types in ratio:
static_assert(std::is_same<::estd::ratio<1000000000000000000, 1>, ::estd::exa>::value, "");
static_assert(std::is_same<::estd::ratio<1000000000000000, 1>, ::estd::peta>::value, "");