CNL  2.0.2 (development)
Compositional Numeric Library
cnl Namespace Reference

compositional numeric library More...

Namespaces

 literals
 user-defined literals
 
 numbers
 compositional numeric library, numbers header/namespace
 

Classes

struct  constant
 type whose value is also its non-type template parameter More...
 
struct  convert
 converts a value from one type to another More...
 
struct  custom_operator
 customization point for operator overloads More...
 
struct  custom_operator< _impl::convert_op, op_value< Input, _impl::native_tag >, op_value< scaled_integer< ResultRep, power< ResultExponent, ResultRadix > >, neg_inf_rounding_tag > >
 cnl::neg_inf_rounding_tag More...
 
struct  custom_operator< _impl::convert_op, op_value< Input, _impl::native_tag >, op_value< scaled_integer< ResultRep, power< ResultExponent, ResultRadix > >, tie_to_pos_inf_rounding_tag > >
 cnl::tie_to_pos_inf_rounding_tag More...
 
struct  custom_operator< _impl::convert_op, op_value< Input, nearest_rounding_tag >, op_value< Result, power< 0, Radix > > >
 cnl::nearest_rounding_tag More...
 
struct  fraction
 numeric type represented as the fraction, numerator / denominator More...
 
struct  from_rep
 generic function object that returns the number encapsulating a given value More...
 
struct  from_rep< Number, Rep >
 Specialization of from_rep for integer types. More...
 
struct  from_rep< scaled_integer< ArchetypeRep, power< Exponent, Radix > >, Rep >
 scaled_integer specialization of from_rep More...
 
struct  native_overflow_tag
 tag to match the overflow behavior of fundamental arithmetic types More...
 
struct  native_rounding_tag
 tag to match the rounding behavior of fundamental arithmetic types More...
 
struct  nearest_rounding_tag
 tag to specify round-to-nearest behavior in arithemtic operations More...
 
struct  neg_inf_rounding_tag
 tag to specify floor or round towards minus infinity rounding behavior in arithmetic operations More...
 
class  op_value
 operand or result of operation;used as Operands parameter of custom_operator More...
 
struct  power
 tag representing the scaling of an integer by a fixed factor More...
 
struct  rep_of
 meta-function object that returns the Rep type of a component More...
 
struct  rounding
 given a numeric type, defines member type as its rounding mode More...
 
struct  saturated_overflow_tag
 tag to match the overflow behavior of fundamental arithmetic types More...
 
struct  set_digits
 convert a type to a similar type with the given number of digits More...
 
struct  set_rep
 meta-function object that transforms a component from one Rep type to another More...
 
struct  set_rep< elastic_integer< Digits, Narrowest >, Rep >
 elastic_integer specialization of set_rep More...
 
struct  set_rounding
 given a numeric type, defines member type as the equivalent type with the given rounding mode User-specializations of this type are permitted. More...
 
struct  set_tag
 meta-function object that transforms a component from one Tag type to another More...
 
struct  set_tag< elastic_integer< Digits, Narrowest >, elastic_tag< NewDigits, NewNarrowest > >
 elastic_integer specialization of set_tag More...
 
struct  tag_of
 meta-function object that returns the Tag type of a component More...
 
struct  tie_to_pos_inf_rounding_tag
 tag to specify tie to positive inf rounding behavior in arithmetic operations More...
 
struct  to_rep
 Returns the value encapsulated in number. More...
 
struct  trapping_overflow_tag
 tag to specify trap-on-overflow behavior in arithemtic operations More...
 
struct  undefined_overflow_tag
 tag to specify undefined-behavior-on-overflow behavior in arithemtic operations More...
 

Typedefs

template<int Digits = digits_v<int>, integer Narrowest = int>
using elastic_integer = _impl::wrapper< typename elastic_tag< Digits, Narrowest >::rep, elastic_tag< Digits, Narrowest > >
 An integer type with auto-widening operators. More...
 
template<int Digits, scaled_tag Scale = power<>, class Narrowest = signed>
using elastic_scaled_integer = scaled_integer< elastic_integer< Digits, Narrowest >, Scale >
 literal real number approximation that uses fixed-point arithmetic and auto-widens to avoid overflow More...
 
template<unsigned NumDigits = digits_v<int>>
using multiprecision = signed_multiprecision< NumDigits >
 alias to a signed, fixed-size Boost.Multiprecision type suitable for specializing scaled_integer More...
 
template<typename Rep = int, overflow_tag Tag = undefined_overflow_tag>
using overflow_integer = _impl::wrapper< Rep, Tag >
 An integer for which out-of-range arithmetic results in overflow. More...
 
template<typename Rep = int, rounding_tag Tag = nearest_rounding_tag>
using rounding_integer = _impl::wrapper< Rep, Tag >
 An integer which facilitates different rounding modes. More...
 
template<typename Number >
using rounding_t = typename rounding< Number >::type
 helper alias of rounding More...
 
template<integer Rep = int, class Scale = power<>>
using scaled_integer = _impl::wrapper< Rep, Scale >
 literal real number approximation that uses fixed-point arithmetic More...
 
template<class T , int Digits>
using set_digits_t = typename set_digits< T, Digits >::type
 Alias to set_digits.
 
template<typename Number , rounding_tag RoundingTag>
using set_rounding_t = typename set_rounding< Number, RoundingTag >::type
 helper alias of set_rounding More...
 
template<unsigned NumDigits = digits_v<int>>
using signed_multiprecision = _sized_integer_impl::number< NumDigits+1, _bmp::signed_magnitude >
 alias to a signed, fixed-size Boost.Multiprecision type suitable for specializing scaled_integer More...
 
template<int Digits = digits_v<int>, rounding_tag RoundingTag = nearest_rounding_tag, overflow_tag OverflowTag = undefined_overflow_tag, class Narrowest = int>
using static_integer = _impl::static_integer< Digits, RoundingTag, OverflowTag, Narrowest >
 a general-purpose integer type More...
 
template<int Digits, int Exponent = 0, rounding_tag RoundingTag = nearest_rounding_tag, overflow_tag OverflowTag = undefined_overflow_tag, integer Narrowest = int>
using static_number = scaled_integer< _impl::static_integer< Digits, RoundingTag, OverflowTag, Narrowest >, power< Exponent > >
 a general-purpose fixed-point real number type More...
 
template<unsigned NumDigits = digits_v<unsigned>>
using unsigned_multiprecision = _sized_integer_impl::number< NumDigits, _bmp::unsigned_magnitude >
 alias to an unsigned, fixed-size Boost.Multiprecision type suitable for specializing scaled_integer More...
 
template<int Digits = digits_v<int>, typename Narrowest = int>
using wide_integer = _impl::wrapper< typename wide_tag< Digits, Narrowest >::rep, wide_tag< Digits, Narrowest > >
 An integer of limitless width.
 

Functions

template<typename Rep , class Scale >
constexpr auto abs (scaled_integer< Rep, Scale > const &x) noexcept -> decltype(-x)
 absolute value More...
 
template<class Rep , int Exponent>
constexpr auto exp2 (scaled_integer< Rep, power< Exponent >> x) -> scaled_integer< Rep, power< Exponent >>
 Calculates exp2(x), i.e. 2^xAccurate to 1LSB for up to 32 bit underlying representation. More...
 
template<typename Narrowest = int, ::cnl::intmax_t Value = 0>
constexpr auto make_elastic_scaled_integer (constant< Value >) -> elastic_scaled_integer< std::max(digits_v< constant< Value >> - trailing_bits(Value), 1), power< trailing_bits(Value)>, Narrowest >
 generate an elastic_scaled_integer object of given value More...
 
template<typename Narrowest = void, typename Integral = int>
constexpr auto make_elastic_scaled_integer (Integral const &value) -> elastic_scaled_integer< std::numeric_limits< Integral >::digits, power<>, typename std::conditional< std::is_same< void, Narrowest >::value, _impl::adopt_signedness_t< int, Integral >, Narrowest >::type >
 generate an elastic_scaled_integer object of given value More...
 
template<typename Value >
constexpr auto make_scaled_integer (Value const &value)
 makes a scaled_integer object from a given value More...
 
template<rounding_tag RoundingTag = _impl::tag_of_t<rounding_integer<>>, overflow_tag OverflowTag = _impl::tag_of_t<overflow_integer<>>, integer Narrowest = int, class Input = int, ::cnl::intmax_t Value>
constexpr auto make_static_number (constant< Value > const &) -> static_number< _impl::used_digits(Value) - trailing_bits(Value), trailing_bits(Value), RoundingTag, OverflowTag, Narrowest >
 constructs a static_number from a given constant value
 
template<rounding_tag RoundingTag = nearest_rounding_tag, overflow_tag OverflowTag = undefined_overflow_tag, integer Narrowest = int, class Input = int>
constexpr auto make_static_number (Input const &input) -> static_number< std::numeric_limits< Input >::digits, 0, RoundingTag, OverflowTag, Narrowest >
 constructs a static_number from a given variable
 
template<int LhsExponent, int RhsExponent, int Radix>
constexpr auto operator+ (power< LhsExponent, Radix >, power< RhsExponent, Radix >) -> power< std::min(LhsExponent, RhsExponent), Radix >
 value of template parameter, Exponent
 
template<class Dividend , class Divisor >
constexpr auto quotient (Dividend const &dividend, Divisor const &divisor)
 calculates the quotient of two scaled_integer values More...
 
template<int Digits, class Narrowest >
constexpr auto sqrt (elastic_integer< Digits, Narrowest > const &x)
 elastic_integer overload of cnl::sqrt More...
 
template<integer Integer>
constexpr auto sqrt (Integer const &x)
 integer overload of cnl::sqrt More...
 
template<typename Rep , int Exponent, int Radix>
constexpr auto sqrt (scaled_integer< Rep, power< Exponent, Radix >> const &x)
 scaled_integer overload of cnl::sqrt More...
 

Variables

template<typename T >
constexpr int digits_v = 0
 provide number of binary digits of the given type More...
 

Detailed Description

compositional numeric library

Typedef Documentation

◆ elastic_integer

template<int Digits = digits_v<int>, integer Narrowest = int>
using cnl::elastic_integer = typedef _impl::wrapper< typename elastic_tag<Digits, Narrowest>::rep, elastic_tag<Digits, Narrowest> >

An integer type with auto-widening operators.

Template Parameters
Digitsa count of the number of digits needed to express the number
Narrowestthe most narrow integer type to use for storage

Arithmetic operations return numbers with the appropriate number of digits necessary to store the correct value, without risk of overflow. For instance, when two elastic_integer values are multiplied together, the resultant type has Digits set to the sum of the operands' Digits.

Note
The lowest value of a signed elastic_integer type is the negative of its maximum value. E.g. the range of elastic_integer<7> is [-127..127], and not [-128..127]. This prevents range errors or wasteful stretching or Digits that would otherwise occur in relation to - and / operators.
The value is stored in an object of Narrowest or set_digits_t<Narrowest, Digits>.
See also
elastic_scaled_integer

◆ elastic_scaled_integer

template<int Digits, scaled_tag Scale = power<>, class Narrowest = signed>
using cnl::elastic_scaled_integer = typedef scaled_integer<elastic_integer<Digits, Narrowest>, Scale>

literal real number approximation that uses fixed-point arithmetic and auto-widens to avoid overflow

Template Parameters
Digitsthe total number of integer and fractional digits stored
Scalethe scale of the integer; defaults to power
Narrowestthe most narrow integer type to use to represent values
See also
elastic_integer

◆ multiprecision

template<unsigned NumDigits = digits_v<int>>
using cnl::multiprecision = typedef signed_multiprecision<NumDigits>

alias to a signed, fixed-size Boost.Multiprecision type suitable for specializing scaled_integer

Template Parameters
NumDigitsnumber of digits constituting the integer value (excluding sign bit)
See also
signed_multiprecision, unsigned_multiprecision

◆ overflow_integer

template<typename Rep = int, overflow_tag Tag = undefined_overflow_tag>
using cnl::overflow_integer = typedef _impl::wrapper<Rep, Tag>

An integer for which out-of-range arithmetic results in overflow.

Template Parameters
Repthe underlying type used to represent the value; defaults to int
Tagtag specifying the overflow-handling strategy; defaults to undefined_overflow_tag
See also
native_overflow_tag, saturated_overflow_tag, trapping_overflow_tag, undefined_overflow_tag

◆ rounding_integer

template<typename Rep = int, rounding_tag Tag = nearest_rounding_tag>
using cnl::rounding_integer = typedef _impl::wrapper<Rep, Tag>

An integer which facilitates different rounding modes.

Template Parameters
Repthe underlying type used to represent the value; defaults to int
Tagtag specifying the rounding mode; defaults to nearest_rounding_tag
See also
native_rounding_tag, nearest_rounding_tag, neg_inf_rounding_tag, tie_to_pos_inf_rounding_tag

◆ rounding_t

template<typename Number >
using cnl::rounding_t = typedef typename rounding<Number>::type

◆ scaled_integer

template<integer Rep = int, class Scale = power<>>
using cnl::scaled_integer = typedef _impl::wrapper<Rep, Scale>

literal real number approximation that uses fixed-point arithmetic

Template Parameters
Repthe underlying type used to represent the value; defaults to int
Scalethe scale of the value represented with Rep; defaults to power

Uses an integer to approximate a real number. Scales the integer by a factor specified by Scale to produce the scaled number. By default, represents an int with no fractional digits using binary arithmetic.

Note
Currently, only binary support and partial decimal support is tested.
The type makes no effort to avoid errors resulting from overflow or underflow. Consider specializing with:
Examples

To define a fixed-point value 1 byte in size with a sign bit, 4 integer bits and 3 fractional bits:

constexpr auto n = scaled_integer<std::int8_t, power<-3>>{-2.75};
static_assert(n == -2.75, "fixed-point type was unable to store the value");

◆ set_rounding_t

template<typename Number , rounding_tag RoundingTag>
Tag cnl::set_rounding_t< _impl::rep_of_t< Number >, Tag >
Initial value:
{
}

helper alias of set_rounding

See also
set_rounding, rounding_t, native_rounding_tag, nearest_rounding_tag

◆ signed_multiprecision

template<unsigned NumDigits = digits_v<int>>
using cnl::signed_multiprecision = typedef _sized_integer_impl::number<NumDigits + 1, _bmp::signed_magnitude>

alias to a signed, fixed-size Boost.Multiprecision type suitable for specializing scaled_integer

Template Parameters
NumDigitsnumber of digits constituting the integer value (excluding sign bit)
See also
unsigned_multiprecision, multiprecision

◆ static_integer

template<int Digits = digits_v<int>, rounding_tag RoundingTag = nearest_rounding_tag, overflow_tag OverflowTag = undefined_overflow_tag, class Narrowest = int>
using cnl::static_integer = typedef _impl::static_integer<Digits, RoundingTag, OverflowTag, Narrowest>

a general-purpose integer type

Template Parameters
Digitsnumber of binary digits
OverflowTagbehavior exhibited on out-of-range conditions
RoundingTagbehavior exhibited on precision loss
Narrowestnarrowest integer with which to represent the value
See also
static_number

◆ static_number

template<int Digits, int Exponent = 0, rounding_tag RoundingTag = nearest_rounding_tag, overflow_tag OverflowTag = undefined_overflow_tag, integer Narrowest = int>
using cnl::static_number = typedef scaled_integer< _impl::static_integer<Digits, RoundingTag, OverflowTag, Narrowest>, power<Exponent> >

a general-purpose fixed-point real number type

Template Parameters
Digitsnumber of binary digits
Exponentthe exponent used to scale the integer value
OverflowTagbehavior exhibited on out-of-range conditions
RoundingTagbehavior exhibited on precision loss
Narrowestnarrowest integer with which to represent the value
See also
static_integer

◆ unsigned_multiprecision

template<unsigned NumDigits = digits_v<unsigned>>
using cnl::unsigned_multiprecision = typedef _sized_integer_impl::number<NumDigits, _bmp::unsigned_magnitude>

alias to an unsigned, fixed-size Boost.Multiprecision type suitable for specializing scaled_integer

Template Parameters
NumDigitsnumber of digits constituting the integer value (excluding sign bit)
See also
signed_multiprecision, multiprecision

Function Documentation

◆ abs()

template<typename Rep , class Scale >
constexpr auto cnl::abs ( scaled_integer< Rep, Scale > const &  x) -> decltype(-x)
constexprnoexcept

absolute value

Parameters
xinput parameter
Returns
|x|
See also
std::abs

◆ exp2()

template<class Rep , int Exponent>
constexpr auto cnl::exp2 ( scaled_integer< Rep, power< Exponent >>  x) -> scaled_integer<Rep, power<Exponent>>
constexpr

Calculates exp2(x), i.e. 2^xAccurate to 1LSB for up to 32 bit underlying representation.

Template Parameters
xthe input value as a scaled_integer
Returns
the result of the exponential, in the same representation as x

◆ make_elastic_scaled_integer() [1/2]

template<typename Narrowest = int, ::cnl::intmax_t Value = 0>
constexpr auto cnl::make_elastic_scaled_integer ( constant< Value >  ) -> elastic_scaled_integer< std::max(digits_v<constant<Value>> - trailing_bits(Value), 1), power<trailing_bits(Value)>, Narrowest>
constexpr

generate an elastic_scaled_integer object of given value

Template Parameters
Narrowestthe narrowest type to use as storage in the resultant elastic_scaled_integer object
Integralthe type of Value
Valuethe integer number to be represented
Returns
the given value to be represented using an elastic_scaled_integer type
Note
The return type is guaranteed to be no larger than is necessary to represent the value.
Example

To define an int-sized object using make_elastic_scaled_integer and constant

// std::uint8_t specifies the type of const_integer - not elastic
constexpr auto n = make_elastic_scaled_integer(constant<0xAA>{});
static_assert(n == 0xAA, "n now has the value, 1024");
static_assert(
std::is_same<decltype(n), elastic_scaled_integer<7, power<1>, int> const>::value,
"by default make_elastic_scaled_integer uses the most efficient type it can");

◆ make_elastic_scaled_integer() [2/2]

template<typename Narrowest = void, typename Integral = int>
constexpr auto cnl::make_elastic_scaled_integer ( Integral const &  value) -> elastic_scaled_integer< std::numeric_limits<Integral>::digits, power<>, typename std::conditional< std::is_same<void, Narrowest>::value, _impl::adopt_signedness_t<int, Integral>, Narrowest>::type>
constexpr

generate an elastic_scaled_integer object of given value

Template Parameters
Narrowestthe most narrow storage type of the resultant cnl::elastic_scaled_integer object
Integralthe type of value
Parameters
valuethe value with which to initialize the elastic_scaled_integer object
Note
The return type is guaranteed to be no larger than is necessary to represent the value.
Example

To define a byte-sized object using make_elastic_scaled_integer and _c:

constexpr auto n = make_elastic_scaled_integer<char>(constant<1536>{});
static_assert(n == 1536, "n now has the value, 1536");
static_assert(
std::is_same<decltype(n), elastic_scaled_integer<2, power<9>, char> const>::value,
"by default make_elastic_scaled_integer uses the most efficient type it can");

◆ make_scaled_integer()

template<typename Value >
constexpr auto cnl::make_scaled_integer ( Value const &  value)
constexpr

makes a scaled_integer object from a given value

Template Parameters
Valuethe type of the value that is to be made into a scaled_integer value
Parameters
valuethe value from which to make the scaled_integer object

Referenced by quotient().

◆ quotient()

template<class Dividend , class Divisor >
constexpr auto cnl::quotient ( Dividend const &  dividend,
Divisor const &  divisor 
)
constexpr

calculates the quotient of two scaled_integer values

Template Parameters
Dividendthe dividend (top number) of the division
Divisorthe divisor (bottom number) of the division
Parameters
dividend,divisordividend and divisor
Returns
quotient: dividend / divisor
Note
This function provides 'quasi-exact' division as described in P1368.

References make_scaled_integer().

◆ sqrt() [1/3]

template<int Digits, class Narrowest >
auto cnl::sqrt ( elastic_integer< Digits, Narrowest > const &  x)
constexpr

elastic_integer overload of cnl::sqrt

Returns
square root of x
Note
This function has O(n) complexity where n is the number of significant digits.
Precondition
x must be non-negative

References sqrt().

◆ sqrt() [2/3]

template<integer Integer>
constexpr auto cnl::sqrt ( Integer const &  x)
constexpr

integer overload of cnl::sqrt

Returns
square root of x
Note
This function has O(n) complexity where n is the number of significant digits.
The algorithm is taken from here.
Precondition
x must be non-negative

Referenced by sqrt().

◆ sqrt() [3/3]

template<typename Rep , int Exponent, int Radix>
auto cnl::sqrt ( scaled_integer< Rep, power< Exponent, Radix >> const &  x)
constexpr

scaled_integer overload of cnl::sqrt

Returns
square root of x
Note
This function has O(n) complexity where n is the number of significant digits.
Precondition
x must be non-negative
Exponent must be even

References sqrt().

Variable Documentation

◆ digits_v

template<typename T >
constexpr int cnl::digits_v< _bmp::number< _bmp::cpp_int_backend< NumBits, NumBits, _bmp::unsigned_magnitude > > > = 0
constexpr

provide number of binary digits of the given type

Template Parameters
Tthe source type
Digitsthe desired number of digits
Note
does not include a sign bit
See also
set_digits
std::is_same
cnl::elastic_scaled_integer
scaled_integer< elastic_integer< Digits, Narrowest >, Scale > elastic_scaled_integer
literal real number approximation that uses fixed-point arithmetic and auto-widens to avoid overflow
Definition: elastic_scaled_integer.h:41
std::int8_t
cnl::scaled_integer
_impl::wrapper< Rep, Scale > scaled_integer
literal real number approximation that uses fixed-point arithmetic
Definition: definition.h:52
cnl::make_elastic_scaled_integer
constexpr auto make_elastic_scaled_integer(constant< Value >) -> elastic_scaled_integer< std::max(digits_v< constant< Value >> - trailing_bits(Value), 1), power< trailing_bits(Value)>, Narrowest >
generate an elastic_scaled_integer object of given value
Definition: elastic_scaled_integer.h:65