Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Static Public Member Functions | List of all members
uni20::basic_half_int< T > Class Template Reference

Signed value type that stores multiples of one half exactly. More...

#include <half_int.hpp>

Classes

struct  twice_tag
 Tag type used to construct directly from the doubled representation. More...
 

Public Types

using value_type = T
 Underlying signed storage type.
 

Public Member Functions

constexpr basic_half_int ()=default
 Construct the value zero.
 
constexpr basic_half_int (T twice_value, twice_tag tag)
 Construct directly from the doubled representation.
 
template<std::integral U>
constexpr basic_half_int (U value)
 Construct from an integral value.
 
template<std::floating_point U>
 basic_half_int (U value)
 Construct by rounding a floating-point value to the nearest half-integer.
 
template<std::signed_integral U>
 explicit (std::numeric_limits< T >::digits< std::numeric_limits< U >::digits) const expr basic_half_int(basic_half_int< U > other) noexcept(std::numeric_limits< T >::digits >=std::numeric_limits< U >::digits)
 Convert another half-integer without changing its value.
 
constexpr auto is_integral () const -> bool
 Return whether the represented value is integral.
 
constexpr auto operator++ () -> basic_half_int &
 Prefix increment by one.
 
constexpr auto operator++ (int) -> basic_half_int
 Postfix increment by one.
 
constexpr auto operator+= (basic_half_int other) -> basic_half_int &
 Add another half-integer value.
 
constexpr auto operator-- () -> basic_half_int &
 Prefix decrement by one.
 
constexpr auto operator-- (int) -> basic_half_int
 Postfix decrement by one.
 
constexpr auto operator-= (basic_half_int other) -> basic_half_int &
 Subtract another half-integer value.
 
constexpr auto operator<=> (basic_half_int const &other) const =default
 Compare two half-integer values.
 
constexpr auto to_double () const -> double
 Convert to double.
 
constexpr auto to_int () const -> T
 Convert to the underlying integral type.
 
constexpr auto to_int_assert () const -> T
 Convert to the underlying integral type assuming exact integrality.
 
constexpr auto twice () const -> T
 Return the doubled representation.
 

Static Public Member Functions

static auto parse (std::string_view text) -> basic_half_int
 Parse a textual half-integer representation.
 

Detailed Description

template<std::signed_integral T>
class uni20::basic_half_int< T >

Signed value type that stores multiples of one half exactly.

Template Parameters
TSigned integer storage type used for the doubled representation.

Member Typedef Documentation

◆ value_type

template<std::signed_integral T>
using uni20::basic_half_int< T >::value_type = T

Underlying signed storage type.

Constructor & Destructor Documentation

◆ basic_half_int() [1/4]

template<std::signed_integral T>
constexpr uni20::basic_half_int< T >::basic_half_int ( )
constexprdefault

Construct the value zero.

◆ basic_half_int() [2/4]

template<std::signed_integral T>
template<std::integral U>
constexpr uni20::basic_half_int< T >::basic_half_int ( U  value)
inlineconstexpr

Construct from an integral value.

Parameters
valueIntegral value to represent exactly.

◆ basic_half_int() [3/4]

template<std::signed_integral T>
template<std::floating_point U>
uni20::basic_half_int< T >::basic_half_int ( U  value)
inlineexplicit

Construct by rounding a floating-point value to the nearest half-integer.

Parameters
valueFloating-point value to round.

◆ basic_half_int() [4/4]

template<std::signed_integral T>
constexpr uni20::basic_half_int< T >::basic_half_int ( T  twice_value,
twice_tag  tag 
)
inlineconstexpr

Construct directly from the doubled representation.

Parameters
twice_valueStored doubled value.
tagSelects the direct doubled-value constructor.

Member Function Documentation

◆ explicit()

template<std::signed_integral T>
template<std::signed_integral U>
uni20::basic_half_int< T >::explicit ( ) const
inlinenoexcept

Convert another half-integer without changing its value.

Conversion is implicit and non-throwing when T can represent every doubled value of U. Narrowing requires explicit construction and checks the doubled representation before converting it.

Exceptions
std::overflow_errorIf the doubled value does not fit in T.

◆ is_integral()

template<std::signed_integral T>
constexpr auto uni20::basic_half_int< T >::is_integral ( ) const -> bool
inlineconstexpr

Return whether the represented value is integral.

Returns
true if the value has no half-integer part.

◆ operator++() [1/2]

template<std::signed_integral T>
constexpr auto uni20::basic_half_int< T >::operator++ ( ) -> basic_half_int&
inlineconstexpr

Prefix increment by one.

Returns
Reference to *this.

◆ operator++() [2/2]

template<std::signed_integral T>
constexpr auto uni20::basic_half_int< T >::operator++ ( int  ) -> basic_half_int
inlineconstexpr

Postfix increment by one.

Returns
Previous value.

◆ operator+=()

template<std::signed_integral T>
constexpr auto uni20::basic_half_int< T >::operator+= ( basic_half_int< T >  other) -> basic_half_int&
inlineconstexpr

Add another half-integer value.

Parameters
otherValue to add.
Returns
Reference to *this.

◆ operator--() [1/2]

template<std::signed_integral T>
constexpr auto uni20::basic_half_int< T >::operator-- ( ) -> basic_half_int&
inlineconstexpr

Prefix decrement by one.

Returns
Reference to *this.

◆ operator--() [2/2]

template<std::signed_integral T>
constexpr auto uni20::basic_half_int< T >::operator-- ( int  ) -> basic_half_int
inlineconstexpr

Postfix decrement by one.

Returns
Previous value.

◆ operator-=()

template<std::signed_integral T>
constexpr auto uni20::basic_half_int< T >::operator-= ( basic_half_int< T >  other) -> basic_half_int&
inlineconstexpr

Subtract another half-integer value.

Parameters
otherValue to subtract.
Returns
Reference to *this.

◆ operator<=>()

template<std::signed_integral T>
constexpr auto uni20::basic_half_int< T >::operator<=> ( basic_half_int< T > const &  other) const
constexprdefault

Compare two half-integer values.

Parameters
otherOther value to compare.
Returns
Ordering result based on the doubled representation.

◆ parse()

template<std::signed_integral T>
static auto uni20::basic_half_int< T >::parse ( std::string_view  text) -> basic_half_int
inlinestatic

Parse a textual half-integer representation.

Parameters
textString form such as "3", "5/2", or "1.5".
Returns
Parsed half-integer value.

◆ to_double()

template<std::signed_integral T>
constexpr auto uni20::basic_half_int< T >::to_double ( ) const -> double
inlineconstexpr

Convert to double.

Returns
Floating-point representation.

◆ to_int()

template<std::signed_integral T>
constexpr auto uni20::basic_half_int< T >::to_int ( ) const -> T
inlineconstexpr

Convert to the underlying integral type.

Returns
Integral representation.
Exceptions
std::runtime_errorIf the value is not integral.

◆ to_int_assert()

template<std::signed_integral T>
constexpr auto uni20::basic_half_int< T >::to_int_assert ( ) const -> T
inlineconstexpr

Convert to the underlying integral type assuming exact integrality.

Returns
Integral representation.
Precondition
is_integral()

◆ twice()

template<std::signed_integral T>
constexpr auto uni20::basic_half_int< T >::twice ( ) const -> T
inlineconstexpr

Return the doubled representation.

Returns
Stored doubled value.

The documentation for this class was generated from the following file: