C++ finite native floating-to-integral conversion
The source shall be a finite float, double or long double whose corresponding configured PPL_SUPPORTED_FLOAT, PPL_SUPPORTED_DOUBLE or PPL_SUPPORTED_LONG_DOUBLE is nonzero. Its mathematical value shall be the exact represented binary value, without a decimal approximation. A destination shall be one of the eleven native integral types or Checked_Number<T, Check_Overflow_Policy<T>> types in 🔗 1. Exact conversion between native and checked integral scalars. Native destinations shall use that overflow policy through their _r adapter and shall reserve no payload for infinity or NaN.
For an integral source value representable in the destination, assign_r shall assign that exact integer. Either sign of native floating-point zero shall produce integer zero. Directed ROUND_DOWN and ROUND_UP requests, with or without a strict flag, shall return V_EQ for these exact inputs. Delegated and no-rounding-needed classifications shall follow 🔗 3. Delegated floating-to-integral truncation and exactness requests.
The destination shall be a live assignable object and a distinct source shall remain unchanged. Callers shall prevent concurrent access to an object while another operation modifies it. No library initialization object, thread initialization or coefficient-constant stage shall be required. A directed request on a nonintegral source shall require the hardware rounding mode to be upward, as established by the rounding protocol in 🔗 4. Rounding-state exchange during an initialized thread lifetime; exact integral inputs shall not require a particular mode. Every call shall preserve the current hardware rounding direction. The _r interface shall return Result without policy-handler dispatch or automatic numeric overflow exceptions. Unavailable source types shall not be instantiated.
An integral scalar conversion depends on the represented source value and destination policy. Exact inputs do not require a floating approximation, while the nonintegral directed kernel has an explicit hardware-rounding prerequisite.
Public header: <ppl.hh>; effective PPL template instantiation for the admitted source and destination types:
Result assign_r(To& to, const From& from, Rounding_Dir dir);
For every configured native floating source and all native/checked destination types, check zero, both signs and representable integral endpoints using an independent significand/power-of-two oracle. Require directed V_EQ and exact values under every supported hardware mode, preserve sources and direction state, and compile unavailable configurations without source-type instantiations.
src/checked_int_inlines.hh; src/Checked_Number_defs.hh and src/Checked_Number_inlines.hh; tests/floatingtointegerspec1.cc; tests/numericpublic1.cc.
- PPL-LLR-000003 Conservative directed numerical bounds (Specifies)
- PPL-LLR-000004 Meaning of numerical conversion outcomes (Specifies)
- PPL-LLR-000301 Meaning of successful exact and approximate results (Specifies)
- PPL-LLR-000311 Public C++ access to supported facilities (Specifies)
- PPL-LLR-000442 Explicit numerical operation contracts (Specifies)
For a finite source value a and admitted source/destination types from 🔗 1. Supported finite native floating-to-integral scalar conversion, ROUND_DOWN shall assign ⌊a⌋ and ROUND_UP shall assign ⌈a⌉ whenever that requested integer is representable. A nonintegral source shall require the upward hardware rounding mode in 🔗 1. Supported finite native floating-to-integral scalar conversion. An optional ROUND_STRICT_RELATION flag shall not change the mathematical result.
A nonintegral downward result shall return V_GT and a nonintegral upward result shall return V_LT, with exact-to-computed orientation from 🔗 1. Numerical result classes and relation meanings. An integral source shall return V_EQ. A source just outside the destination's integer endpoints whose requested rounded integer is an endpoint shall produce that ordinary definite relation, without an integer-range-overflow annotation. This shall include upward rounding of a negative proper fraction to unsigned zero.
Out-of-range rounded integers shall follow 🔗 4. Floating-to-integral range detection and reusable destinations. Source, object-synchronization, lifetime and hardware-mode preservation shall follow 🔗 1. Supported finite native floating-to-integral scalar conversion.
A conversion selects the requested integer bound. The original floating value may lie beyond an integer endpoint even though the requested bound is a valid endpoint, so source range and rounded-result range must be distinguished.
Public header: <ppl.hh>; effective PPL template instantiation for the admitted source and destination types:
Result assign_r(To& to, const From& from, Rounding_Dir dir);
Use exact dyadic proper and improper fractions of both signs and verify floor/ceiling with an independent integer quotient/remainder oracle. Cover strict and nonstrict requests, interior integers and adjacent fractions at every destination endpoint, including inward rounding from outside and unsigned zero. Preserve upward hardware mode and distinct sources.
src/checked_int_inlines.hh; src/Checked_Number_defs.hh and src/Checked_Number_inlines.hh; tests/floatingtointegerspec1.cc.
For admitted source and destination types in 🔗 1. Supported finite native floating-to-integral scalar conversion, ROUND_IGNORE shall assign trunc(a) toward zero when that integer is representable. With no strict flag, the successful conversion shall return V_LGE for integral and nonintegral source values. No particular current hardware direction shall be required for this truncating request.
ROUND_NOT_NEEDED shall require an exact integral source value representable in the destination. For that valid request, the integer value shall be preserved under any current hardware rounding direction. Without a strict flag, its result shall be V_EQ in an assertion-enabled build and may be lazy V_LGE in a build defining NDEBUG, following the internal request translation in 🔗 7. Internal no-rounding-needed request and result checking.
With ROUND_STRICT_RELATION, a delegated request shall determine the definite relation: V_GT for a positive inexact source truncated toward zero, V_LT for a negative inexact source, and V_EQ for an integral source. A strict no-rounding-needed request shall retain its exact integral precondition and return V_EQ. Range failures under delegated requests shall follow 🔗 4. Floating-to-integral range detection and reusable destinations. Object validity, source preservation and hardware-direction preservation shall follow 🔗 1. Supported finite native floating-to-integral scalar conversion.
Truncation and relation precision are separate requests. A lazily classified exact payload must not be confused with a determined equality relation, and strict requests must resolve that distinction.
Public header: <ppl.hh>; effective PPL template instantiation for the admitted source and destination types:
Result assign_r(To& to, const From& from, Rounding_Dir dir);
Check signed dyadic truncation under ignore requests with documented V_LGE results, exact no-rounding-needed inputs in both build modes and all hardware states, and definite relations for each strict form. Include zero, proper fractions, endpoint-adjacent inputs, both destination forms and preservation of sources and hardware direction.
src/checked_int_inlines.hh; src/Checked_Number_defs.hh and src/Checked_Number_inlines.hh; tests/floatingtointegerspec1.cc; tests/numericpublic1.cc.
- PPL-LLR-000002 Detection of checked coefficient overflow (Specifies)
- PPL-LLR-000003 Conservative directed numerical bounds (Specifies)
- PPL-LLR-000004 Meaning of numerical conversion outcomes (Specifies)
- PPL-LLR-000301 Meaning of successful exact and approximate results (Specifies)
- PPL-LLR-000311 Public C++ access to supported facilities (Specifies)
- PPL-LLR-000442 Explicit numerical operation contracts (Specifies)
For the admitted types and valid hardware prerequisites in 🔗 1. Supported finite native floating-to-integral scalar conversion, let q be the requested floor, ceiling or truncated integer defined by 🔗 2. Directed finite floating-to-integral rounding within range and 🔗 3. Delegated floating-to-integral truncation and exactness requests. If q lies below the destination minimum, ROUND_UP shall store that minimum and return V_LT_INF; ROUND_DOWN or ROUND_IGNORE shall return V_GT_MINUS_INFINITY | V_UNREPRESENTABLE. If q lies above the maximum, ROUND_DOWN shall store that maximum and return V_GT_SUP; ROUND_UP or ROUND_IGNORE shall return V_LT_PLUS_INFINITY | V_UNREPRESENTABLE. An optional strict flag shall not change these range outcomes.
A requested integer within range shall follow the ordinary classifications of 🔗 2. Directed finite floating-to-integral rounding within range and 🔗 3. Delegated floating-to-integral truncation and exactness requests. Checking range shall not itself require an undefined native floating-to-integer cast, including when a floating source format cannot represent the destination's maximum integer exactly. A failed range request shall not report a wrapped integer as successful. An unrepresentable result shall leave the destination assignable and destructible without a payload-value guarantee. ROUND_NOT_NEEDED shall exclude out-of-range inputs. These result codes shall be returned directly without automatic policy exception dispatch.
Safe conversion must guard the actual requested integer range before any narrowing native cast. A finite source can exceed a destination bound even when the source format rounds that bound to a different floating value.
Public header: <ppl.hh>; effective PPL template instantiation for the admitted source and destination types:
Result assign_r(To& to, const From& from, Rounding_Dir dir);
Use unbounded dyadic oracles for every signed/unsigned endpoint and neighboring representable floating values. Include the largest source-representable integer inside each destination, the first outside it, large finite source maxima and negative values targeting unsigned storage. Require exact range codes, inward endpoints, outward flags, reusable outputs and no undefined-behavior sanitizer findings under valid hardware modes.
src/checked_int_inlines.hh; src/Checked_Number_defs.hh and src/Checked_Number_inlines.hh; tests/floatingtointegerspec1.cc.