### Boost Numeric Converter Example in C++ Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/converter___function_object This C++ example demonstrates the usage of `boost::numeric::converter` for converting a double to an int. It shows direct conversion, usage as a function object, and exception handling for overflow cases. ```cpp #include #include int main() { typedef boost::numeric::converter Double2Int ; int x = Double2Int::convert(2.0); assert ( x == 2 ); int y = Double2Int()(3.14); // As a function object. assert ( y == 3 ) ; // The default rounding is trunc. try { double m = boost::numeric::bounds::highest(); int z = Double2Int::convert(m); // By default throws positive_overflow() } catch ( boost::numeric::positive_overflow const& ) { } return 0; } ``` -------------------------------- ### Boost Numeric Conversion Traits Examples Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/conversion_traits___traits_class Demonstrates the usage of boost::numeric::conversion_traits in C++ with various type combinations, including trivial, subranged, and doubly subranged conversions. It utilizes assertions to verify the properties derived by the traits class. ```cpp #include #include #include int main() { // A trivial conversion. typedef boost::numeric::conversion_traits Short2Short_Traits ; assert ( Short2Short_Traits::trivial::value ) ; // A subranged conversion. typedef boost::numeric::conversion_traits UInt2Double_Traits ; assert ( UInt2Double_Traits::int_float_mixture::value == boost::numeric::integral_to_float ) ; assert ( UInt2Double_Traits::sign_mixture::value == boost::numeric::unsigned_to_signed ) ; assert ( !UInt2Double_Traits::subranged::value ) ; assert ( typeid(UInt2Double_Traits::supertype) == typeid(double) ) ; assert ( typeid(UInt2Double_Traits::subtype) == typeid(unsigned int) ) ; // A doubly subranged conversion. assert ( (boost::numeric::conversion_traits::subranged::value) ); assert ( (boost::numeric::conversion_traits::subranged::value) ); return 0; } ``` -------------------------------- ### numeric_cast Type Conversion Examples Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/improved_numeric_cast__ Demonstrates practical usage of numeric_cast with various numeric type conversions including safe conversions, overflow detection with exception handling for positive and negative overflow scenarios, and conversions between integral and floating-point types. Shows proper exception catching for bad_numeric_cast, negative_overflow, and positive_overflow exceptions. ```cpp #include #include int main() { using boost::numeric_cast; using boost::numeric::bad_numeric_cast; using boost::numeric::positive_overflow; using boost::numeric::negative_overflow; try { int i=42; short s=numeric_cast(i); // This conversion succeeds (is in range) } catch(negative_overflow& e) { std::cout << e.what(); } catch(positive_overflow& e) { std::cout << e.what(); } try { float f=-42.1234; // This will cause a boost::numeric::negative_overflow exception to be thrown unsigned int i=numeric_cast(f); } catch(bad_numeric_cast& e) { std::cout << e.what(); } double d= f + numeric_cast(123); // int -> double unsigned long l=std::numeric_limits::max(); try { // This will cause a boost::numeric::positive_overflow exception to be thrown // NOTE: *operations* on unsigned integral types cannot cause overflow // but *conversions* to a signed type ARE range checked by numeric_cast. unsigned char c=numeric_cast(l); } catch(positive_overflow& e) { std::cout << e.what(); } return 0; } ``` -------------------------------- ### Use boost::numeric::bounds with float type - C++ Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/bounds___traits_class Practical example demonstrating how to use boost::numeric::bounds<> to retrieve numeric limits for floating point types. Shows the equivalence between bounds<> methods and std::numeric_limits for maximum, minimum, and smallest positive values. Requires inclusion of boost/numeric/conversion/bounds.hpp and boost/limits.hpp headers. ```cpp #include #include #include int main() { std::cout << "numeric::bounds versus numeric_limits example.\n"; std::cout << "The maximum value for float:\n"; std::cout << boost::numeric::bounds::highest() << "\n"; std::cout << std::numeric_limits::max() << "\n"; std::cout << "The minimum value for float:\n"; std::cout << boost::numeric::bounds::lowest() << "\n"; std::cout << -std::numeric_limits::max() << "\n"; std::cout << "The smallest positive value for float:\n"; std::cout << boost::numeric::bounds::smallest() << "\n"; std::cout << std::numeric_limits::min() << "\n"; return 0; } ``` -------------------------------- ### Arithmetic Floating-Point Numeric Type Example (C++) Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/definitions Illustrates a standard C++ `double` type, which is both an arithmetic and a floating-point numeric type. It shows the assignment of a predefined constant `M_PI` to a `double` variable, representing a floating-point numeric value. ```cpp // A 'floating' numeric type (double) which is also an arithmetic type (built-in), // with a float numeric value. double pi = M_PI ; ``` -------------------------------- ### C++ Signed and Unsigned Integer Type Assignment Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/definitions Demonstrates the relationship between signed and unsigned integer types in C++. When a signed integer is assigned to an unsigned integer of the same base type, they share the same value representation (bit pattern) but differ in interpretation. This example shows a signed int with value -3 assigned to an unsigned int, preserving the bit pattern. ```cpp int i = -3 ; // suppose value representation is: 10011 (sign bit + 4 magnitude bits) unsigned int u = i ; // u is required to have the same 10011 as its value representation. ``` -------------------------------- ### Numeric Type with Whole Value Example (C++) Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/definitions Shows an example where a floating-point numeric type (`double`) is used to store a whole number value. This highlights that the type classification ('floating' or 'integer') is distinct from the specific value it holds. Numeric values are typed, meaning `double two = 2.0;` represents a floating-point value, even though its magnitude is an integer. ```cpp // A 'floating' numeric type with a whole numeric value. // NOTE: numeric values are typed valued, hence, they are, for instance, // integer or floating, despite the value itself being whole or including // a fractional part. double two = 2.0 ; ``` -------------------------------- ### Implement Boost Numeric Converter Logic Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/converter___function_object This C++ function demonstrates the internal logic for converting numeric types, including range validation, rounding for float-to-int conversions, and the final low-level conversion. It relies on policies for specific implementations. ```cpp result_type converter<>::convert ( argument_type s ) { validate_range(s); // Implemented by the internal range checking logic // (which also calls the OverflowHandler policy) // or externally supplied by the UserRangeChecker policy. s = nearbyint(s); // Externally supplied by the Float2IntRounder policy. // NOTE: This is actually called only for float to int conversions. return low_level_convert(s); // Externally supplied by the RawConverter policy. } ``` -------------------------------- ### Using std::floor in Float2IntRounder Policy Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/type_requirements_and_user_defined_types_support Demonstrates how the default Float2IntRounder policy in Boost C++ Libraries uses `std::floor`. It highlights that for built-in arithmetic types, standard functions are used directly, while user-defined types require overloaded versions for the default policies to work correctly. ```cpp using std::floor ; return floor(s); ``` -------------------------------- ### RawConverter Policy Interface Template Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/numeric_converter_policy_classes Defines the required interface for custom raw converter policy classes that perform actual type conversions from source to target. The policy must provide a low_level_convert() static member function that accepts an argument of the Traits::argument_type and returns a value of Traits::result_type. ```C++ template struct YourRawConverterPolicy { typedef typename Traits::result_type result_type ; typedef typename Traits::argument_type argument_type ; static result_type low_level_convert ( argument_type s ) { return ; } } ; ``` -------------------------------- ### Range Checking Logic for int-to-int Conversions Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/converter___function_object This section details the range checking logic for conversions between signed and unsigned integer types. It specifies conditions for subranged and non-subranged types, including how source values are compared against the target type's limits. ```text int_to_int |--> sig_to_sig |--> subranged |--> ( s >= S(LowestT) ) && ( s <= S(HighestT) ) | |--> not subranged |--> NONE | |--> unsig_to_unsig |--> subranged |--> ( s >= S(LowestT) ) && ( s <= S(HighestT) ) | |--> not subranged |--> NONE | |--> sig_to_unsig |--> pos subranged |--> ( s >= S(0) ) && ( s <= S(HighestT) ) | |--> not pos subranged |--> ( s >= S(0) ) | |--> unsig_to_sig |--> subranged |--> ( s <= S(HighestT) ) | |--> not subranged |--> NONE ``` -------------------------------- ### Range Checking Logic for int-to-float Conversions Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/converter___function_object This section describes the range checking logic for conversions from integer types to floating-point types. It outlines conditions based on different rounding methods, specifying how source values are compared against target limits with potential adjustments. ```text int_to_float |--> NONE ``` -------------------------------- ### Range Checking Logic for float-to-int Conversions Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/converter___function_object This section details the range checking logic for conversions from floating-point types to integer types. It specifies different conditions based on rounding strategies (round to zero, round to even nearest, round to infinity, round to negative infinity) and how source values are compared to target limits. ```text float_to_int |--> round_to_zero |--> ( s > S(LowestT)-S(1) ) && ( s < S(HighestT)+S(1) ) |--> round_to_even_nearest |--> ( s >= S(LowestT)-S(0.5) ) && ( s < S(HighestT)+S(0.5) ) |--> round_to_infinity |--> ( s > S(LowestT)-S(1) ) && ( s <= S(HighestT) ) |--> round_to_neg_infinity |--> ( s >= S(LowestT) ) && ( s < S(HighestT)+S(1) ) ``` -------------------------------- ### raw_numeric_converter RawConverter Policy - C++ Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/numeric_converter_policy_classes Provides the default raw converter implementation using static_cast<> for type conversions. This is part of the boost::numeric namespace and serves as the standard policy for converting source types to target types in numeric conversions. ```C++ namespace boost { namespace numeric { template struct raw_numeric_converter { typedef typename Traits::result_type result_type ; typedef typename Traits::argument_type argument_type ; static result_type low_level_convert ( argument_type s ) { return static_cast(s) ; } } ; } } ``` -------------------------------- ### converter<> Function Object Definition (C++) Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/converter___function_object Defines the boost::numeric::converter<> function object template. This object encapsulates numeric conversion logic and can be customized with various policies for traits, overflow handling, rounding, and range checking. It exposes static and member functions for performing and validating conversions. ```cpp namespace boost { namespace numeric { template, class OverflowHandler = def_overflow_handler, class Float2IntRounder = Trunc< typename Traits::source_type >, class RawConverter = raw_converter, class UserRangeChecker = UseInternalRangeChecker > struct converter { typedef Traits traits ; typedef typename Traits::source_type source_type ; typedef typename Traits::argument_type argument_type ; typedef typename Traits::result_type result_type ; static result_type convert ( argument_type s ) ; result_type operator() ( argument_type s ) const ; // Internal member functions: static range_check_result out_of_range ( argument_type s ) ; static void validate_range ( argument_type s ) ; static result_type low_level_convert ( argument_type s ) ; static source_type nearbyint ( argument_type s ) ; } ; } } // namespace numeric, boost ``` -------------------------------- ### Range Checking Logic for float-to-float Conversions Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/converter___function_object This section explains the range checking logic for conversions between floating-point types. It covers cases for subranged and non-subranged types, defining the conditions under which a conversion is considered within the valid range. ```text float_to_float |--> subranged |--> ( s >= S(LowestT) ) && ( s <= S(HighestT) ) |--> not subranged |--> NONE ``` -------------------------------- ### Boost Numeric Overflow Handler Policy Interface and Implementations Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/numeric_converter_policy_classes Specifies the interface for stateless function objects used to handle overflow conditions detected during numeric conversion. It includes the default handler (`def_overflow_handler`) which throws exceptions, and `silent_overflow_handler` which suppresses them. Both are stateless and must provide an `operator()` that accepts a `boost::range_check_result`. ```C++ namespace boost { namespace numeric { struct def_overflow_handler { void operator() ( range_check_result r ) // throw bad_numeric_conversion derived { if ( r == cNegOverflow ) throw negative_overflow() ; else if ( r == cPosOverflow ) throw positive_overflow() ; } } ; struct silent_overflow_handler { void operator() ( range_check_result ) // no-throw {} } ; } } ``` -------------------------------- ### Float2IntRounder Policy Interface Template Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/numeric_converter_policy_classes Defines the required interface for custom float-to-integral rounding policy classes. The policy must provide a nearbyint() static member function and specify the rounding style using an mpl::integral_c type. This is a stateless template class that converters inherit from to gain rounding behavior. ```C++ template struct YourFloat2IntRounderPolicy { typedef S source_type ; typedef {S or S const&} argument_type ; static source_type nearbyint ( argument_type s ) { ... } typedef mpl::integral_c round_style ; } ; ``` -------------------------------- ### Define User-Defined Integer Numeric Type (C++) Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/definitions Demonstrates the definition of a user-defined numeric type `MyInt` that behaves like an integer. It shows how to define a constructor to accept a built-in type and a method to convert back to a built-in type. It also includes a placeholder for specializing `std::numeric_limits` for this custom type, a requirement for a type to be considered numeric. ```cpp // A numeric type which is not an arithmetic type (is user-defined) // and which is intended to represent integer numbers (i.e., an 'integer' numeric type) class MyInt { MyInt ( long long v ) ; long long to_builtin(); } ; namespace std { template<> numeric_limits { ... } ; } // An integer numeric type with an integer numeric value. MyInt i(1234); ``` -------------------------------- ### Boost Raw Converter for Double (C++) Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/type_requirements_and_user_defined_types_support Implements `raw_converter` specializations to handle low-level conversions to and from the custom `Double` type. The first specialization defines `low_level_convert` from `Double` to a target type `T` using `static_cast`. The second specialization defines the conversion from a source type `S` to `Double` by constructing a `Double` object. ```cpp //! Define the conversion from the custom type to built-in types and vice-versa. template struct raw_converter< conversion_traits< T, Double > > { static T low_level_convert ( const Double& n ) { return static_cast( n.v ); } }; template struct raw_converter< conversion_traits< Double, S > > { static Double low_level_convert ( const S& n ) { return Double(n); } }; }}//namespace boost::numeric; ``` -------------------------------- ### Trunc Float2IntRounder Policy - C++ Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/numeric_converter_policy_classes Truncates floating-point values toward zero by using floor() for non-negative values and ceil() for negative values. This rounder policy specifies std::round_toward_zero as its rounding style and is part of the boost::numeric namespace. ```C++ namespace boost { namespace numeric { template struct Trunc { static source_type nearbyint ( argument_type s ) { using std::floor ; using std::ceil ; return s >= static_cast(0) ? floor(s) : ceil(s) ; } typedef mpl::integral_c round_style ; } ; } } ``` -------------------------------- ### Define numeric_cast_traits for UDTs Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/type_requirements_and_user_defined_types_support This code defines the `numeric_cast_traits` specializations required for a User-Defined Type (UDT) to be compatible with `numeric_cast`. It specifies policies for overflow handling, range checking, and rounding, which are essential for type conversions. Users need to provide concrete implementations for `YourOverflowHandlerPolicy`, `YourRangeCheckerPolicy`, and `YourFloat2IntRounderPolicy`. ```cpp namespace boost { namespace numeric { template struct numeric_cast_traits { typedef conversion_traits conv_traits; //! The following are required: typedef YourOverflowHandlerPolicy overflow_policy; typedef YourRangeCheckerPolicy range_checking_policy; typedef YourFloat2IntRounderPolicy rounding_policy; }; template struct numeric_cast_traits { typedef conversion_traits conv_traits; //! The following are required: typedef YourOverflowHandlerPolicy overflow_policy; typedef YourRangeCheckerPolicy range_checking_policy; typedef YourFloat2IntRounderPolicy rounding_policy; }; }}//namespace boost::numeric; ``` -------------------------------- ### Floor Float2IntRounder Policy - C++ Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/numeric_converter_policy_classes Rounds floating-point values downward to the nearest integer using std::floor(). This rounder policy specifies std::round_toward_neg_infinity as its rounding style and is part of the boost::numeric namespace. ```C++ namespace boost { namespace numeric { template struct Floor { static source_type nearbyint ( argument_type s ) { using std::floor ; return floor(s) ; } typedef mpl::integral_c round_style ; } ; } } ``` -------------------------------- ### Specialized Trunc Rounding Policy for Double (C++) Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/type_requirements_and_user_defined_types_support Provides a specialization of the `Trunc` rounding policy for a custom `Double` type. This implementation overrides the default behavior to use `std::floor` on the underlying value (`s.v`) and returns a `Double` object. It defines the `round_style` as `std::round_toward_zero`. ```cpp //! Define a rounding policy and specialize on the custom type. template struct Trunc: boost::numeric::Trunc{}; template<> struct Trunc { typedef Double source_type; typedef Double const& argument_type; static source_type nearbyint ( argument_type s ) { #if !defined(BOOST_NO_STDC_NAMESPACE) using std::floor; #endif return Double( floor(s.v) ); } typedef boost::mpl::integral_c< std::float_round_style, std::round_toward_zero> round_style; }; ``` -------------------------------- ### UserRangeChecker Policy Interface Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/numeric_converter_policy_classes Defines the required interface for a custom range checker policy. It must include `argument_type`, `out_of_range()`, and `validate_range()` static member functions. The `validate_range()` function typically uses the `OverflowHandler` policy. ```C++ template struct YourRangeCheckerPolicy { typedef typename Traits::argument_type argument_type ; // Determines if the value 's' fits in the range of the Target type. static range_check_result out_of_range ( argument_type s ) ; // Checks whether the value 's' is out_of_range() // and passes the result of the check to the OverflowHandler policy. static void validate_range ( argument_type s ) { OverflowHandler()( out_of_range(s) ) ; } } ; ``` -------------------------------- ### Boost Numeric Cast Traits for Double (C++) Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/type_requirements_and_user_defined_types_support Defines `numeric_cast_traits` specializations for conversions involving the custom `Double` type. The first specialization handles conversions from `Double` to other types `S`, setting `overflow_policy`, `range_checking_policy`, and `rounding_policy`. The second specialization handles conversions from `S` to `Double`, similarly configuring the policies. ```cpp namespace boost { namespace numeric { //! Define the numeric_cast_traits specializations on the custom type. template struct numeric_cast_traits { typedef custom::overflow_handler overflow_policy; typedef custom::range_checker < boost::numeric::conversion_traits , overflow_policy > range_checking_policy; typedef boost::numeric::Trunc rounding_policy; }; template struct numeric_cast_traits { typedef custom::overflow_handler overflow_policy; typedef custom::range_checker < boost::numeric::conversion_traits , overflow_policy > range_checking_policy; typedef custom::Trunc rounding_policy; }; ``` -------------------------------- ### Boost Numeric Conversion Traits Definition Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/conversion_traits___traits_class Defines the conversion_traits struct in C++ for analyzing numeric conversion properties between source and target types. It uses MPL integral constants and boolean types to categorize the conversion's characteristics. ```cpp namespace boost { namespace numeric { template struct conversion_traits { mpl::integral_c int_float_mixture ; mpl::integral_c sign_mixture; mpl::integral_c udt_builtin_mixture ; mpl::bool_<...> subranged ; mpl::bool_<...> trivial ; typedef T target_type ; typedef S source_type ; typedef ... argument_type ; typedef ... result_type ; typedef ... supertype ; typedef ... subtype ; } ; } } // namespace numeric, namespace boost ``` -------------------------------- ### Specialized Ceil Rounding Policy for Double (C++) Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/type_requirements_and_user_defined_types_support Provides a specialization of the `Ceil` rounding policy for a custom `Double` type. This implementation overrides the default behavior to use `std::ceil` on the underlying value (`s.v`) and returns a `Double` object. It also defines the `round_style` as `std::round_toward_infinity`. ```cpp //! Define a rounding policy and specialize on the custom type. template struct Ceil : boost::numeric::Ceil{}; template<> struct Ceil { typedef Double source_type; typedef Double const& argument_type; static source_type nearbyint ( argument_type s ) { #if !defined(BOOST_NO_STDC_NAMESPACE) using std::ceil ; #endif return Double( ceil(s.v) ); } typedef boost::mpl::integral_c< std::float_round_style, std::round_toward_infinity> round_style; }; ``` -------------------------------- ### Boost Numeric Exception Classes for Overflow Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/numeric_converter_policy_classes Defines custom exception classes used by Boost.Numeric to signal conversion errors. `bad_numeric_cast` serves as a base class, with `negative_overflow` and `positive_overflow` providing specific details about the type of overflow encountered. These exceptions are derived from `std::bad_cast`. ```C++ namespace boost { namespace numeric { class bad_numeric_cast : public std::bad_cast { public: virtual const char *what() const // throw() { return "bad numeric conversion: overflow"; } }; class negative_overflow : public bad_numeric_cast { public: virtual const char *what() const // throw() { return "bad numeric conversion: negative overflow"; } }; class positive_overflow : public bad_numeric_cast { public: virtual const char *what() const // throw() { return "bad numeric conversion: positive overflow"; } }; } } ``` -------------------------------- ### Ceil Float2IntRounder Policy - C++ Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/numeric_converter_policy_classes Rounds floating-point values upward to the nearest integer using std::ceil(). This rounder policy specifies std::round_toward_infinity as its rounding style and is part of the boost::numeric namespace. ```C++ namespace boost { namespace numeric { template struct Ceil { static source_type nearbyint ( argument_type s ) { using std::ceil ; return ceil(s) ; } typedef mpl::integral_c round_style ; } ; } } ``` -------------------------------- ### Define bounds<> traits class template - C++ Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/bounds___traits_class Template definition for the bounds<> traits class that provides static member functions for retrieving numeric type boundaries. The class uses compile-time type selection to provide consistent semantics across integral and floating point types without runtime overhead. ```cpp template struct bounds { static N lowest () { return implementation_defined; } static N highest () { return implementation_defined; } static N smallest() { return implementation_defined; } }; ``` -------------------------------- ### RoundEven Float2IntRounder Policy - C++ Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/numeric_converter_policy_classes Rounds floating-point values to the nearest integer, with implementation-defined tie-breaking behavior. This rounder policy specifies std::round_to_nearest as its rounding style and is part of the boost::numeric namespace. ```C++ namespace boost { namespace numeric { template struct RoundEven { static source_type nearbyint ( argument_type s ) { return impl-defined-value ; } typedef mpl::integral_c round_style ; } ; } } ``` -------------------------------- ### Define a Custom UDT 'Double' for numeric_cast Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/type_requirements_and_user_defined_types_support This C++ code defines a custom User-Defined Type (UDT) named 'Double' designed for use with `numeric_cast`. It inherits from `boost::ordered_field_operators` to provide comparison and arithmetic operators. The UDT wraps a `double` value and includes constructors, assignment operators, and various comparison and arithmetic operations, making it suitable for type conversions. ```cpp //! Define a simple custom number struct Double : boost::ordered_field_operators < Double , boost::ordered_field_operators2< Double, long double , boost::ordered_field_operators2< Double, double , boost::ordered_field_operators2< Double, float , boost::ordered_field_operators2< Double, int , boost::ordered_field_operators2< Double, unsigned int , boost::ordered_field_operators2< Double, long , boost::ordered_field_operators2< Double, unsigned long , boost::ordered_field_operators2< Double, long long , boost::ordered_field_operators2< Double, unsigned long long , boost::ordered_field_operators2< Double, char , boost::ordered_field_operators2< Double, unsigned char , boost::ordered_field_operators2< Double, short , boost::ordered_field_operators2< Double, unsigned short > > > > > > > > > > > > > > { Double() : v(0) {} template explicit Double( T v ) : v(static_cast(v)) {} template Double& operator= ( T t ) { v = static_cast(t); return *this; } bool operator < ( const Double& rhs ) const { return v < rhs.v; } template bool operator < ( T rhs ) const { return v < static_cast(rhs); } bool operator > ( const Double& rhs ) const { return v > rhs.v; } template bool operator > ( T rhs ) const { return v > static_cast(rhs); } bool operator ==( const Double& rhs ) const { return v == rhs.v; } template bool operator == ( T rhs ) const { return v == static_cast(rhs); } bool operator !() const { return v == 0; } Double operator -() const { return Double(-v); } Double& operator +=( const Double& t ) { v += t.v; return *this; } template Double& operator +=( T t ) { v += static_cast(t); return *this; } Double& operator -=( const Double& t ) { v -= t.v; return *this; } template Double& operator -=( T t ) { v -= static_cast(t); return *this; } Double& operator *= ( const Double& factor ) { v *= factor.v; return *this; } template Double& operator *=( T t ) { v *= static_cast(t); return *this; } Double& operator /= (const Double& divisor) { v /= divisor.v; return *this; } template Double& operator /=( T t ) { v /= static_cast(t); return (*this); } double v; }; //! Define numeric_limits for the custom type. namespace std { template<> class numeric_limits : public numeric_limits { public: //! Limit our Double to a range of +/- 100.0 static Double (min)() { return Double(1.e-2); } static Double (max)() { return Double(1.e2); } static Double epsilon() { return Double( std::numeric_limits::epsilon() ); } }; } //! Define range checking and overflow policies. namespace custom { ``` -------------------------------- ### sign_mixture_enum - Sign Category Enumeration Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/conversion_traits___traits_class Enumeration that classifies the combination of signed and unsigned types involved in a conversion. Defines four sign categories: unsigned-to-unsigned, signed-to-signed, signed-to-unsigned, and unsigned-to-signed. Used as the basis for the sign_mixture<> template struct. ```cpp namespace boost { namespace numeric { enum sign_mixture_enum { unsigned_to_unsigned ,signed_to_signed ,signed_to_unsigned ,unsigned_to_signed } ; } } // namespace boost::numeric ``` -------------------------------- ### udt_builtin_mixture<> - Template Traits Class for Type Origin Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/conversion_traits___traits_class MPL Integral Constant template struct that classifies source and target types as either user-defined or built-in. The ::value member is of enumeration type udt_builtin_mixture_enum and indicates the type-origin combination of types S and T, enabling compile-time dispatch based on whether types are built-in or user-defined. ```cpp namespace boost { namespace numeric { template struct udt_builtin_mixture : mpl::integral_c {} ; } } // namespace boost::numeric ``` -------------------------------- ### sign_mixture<> - Template Traits Class for Sign Category Source: https://www.boost.org/doc/libs/latest/libs/numeric/conversion/doc/html/boost_numericconversion/conversion_traits___traits_class MPL Integral Constant template struct that classifies source and target types as either signed or unsigned. The ::value member is of enumeration type sign_mixture_enum and indicates the sign combination of types S and T, enabling compile-time dispatch based on signedness. ```cpp namespace boost { namespace numeric { template struct sign_mixture : mpl::integral_c {} ; } } // namespace boost::numeric ```