Parma Polyhedra Library requirements
C++ finite rational numerical operations

C++ finite rational numerical operations

1. Exact rational assignment from rational and integral sources
UID: PPL-SPEC-000091
STATUS: APPROVED
COMPONENT: NUMERICS
APPLICABILITY: IF_CPP, SCALAR_MPQ
VERIFICATION_METHODS: Test, Analysis, Inspection
LANGUAGE: CPP
VISIBILITY: Public
RELATIONS (Parent):
STATEMENT:

assign_r(to, from, dir) shall replace to by the exact value of a canonical rational source, mpz_class source or supported native integral source, and shall return V_EQ. Integral assignment shall produce canonical denominator one, including for zero, both signs and signed and unsigned native endpoints. Rational self-assignment shall preserve its value.

The native source types shall be char, signed char, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long, long long and unsigned long long. Their full representable ranges shall be admitted. The directions ROUND_DOWN, ROUND_UP, ROUND_IGNORE and ROUND_NOT_NEEDED, with an optional ROUND_STRICT_RELATION flag, shall not change this exact value or result classification.

Rational sources shall be finite canonical mpq_class objects: the denominator shall be positive, and numerator and denominator shall be coprime; zero shall be represented as 0/1. The destination shall be a live rational object. Distinct sources shall remain unchanged. Callers shall synchronize access overlapping modification of an argument object. No library initialization object, thread initialization or coefficient-constant stage shall be required. Available resources shall limit computation; no fixed coefficient-width bound shall be imposed on rational arithmetic itself.

RATIONALE:

Rational assignment must preserve every finite integral value independently of the configured bounded coefficient type. An exact classification applies to both copied fractions and integral embeddings.

INTERFACE:

Public header: <ppl.hh>; effective PPL template instantiations for mpq_class:

Result assign_r(mpq_class& to, const mpq_class& from, Rounding_Dir dir);
Result assign_r(mpq_class& to, const mpz_class& from, Rounding_Dir dir);

The same template shall admit each listed native integral source type in place of mpz_class.

VERIFICATION_CRITERIA:

Check exact status, canonical numerator/denominator and source preservation for all native source types, endpoint values, a multi-limb integer, rational self-copy and distinct copies. Repeat supported directions with and without strict relation, and reassign after earlier destination contents.

SOURCE:

src/checked_mpq_inlines.hh; src/Checked_Number_defs.hh and src/Checked_Number_inlines.hh; tests/rationalnumerics1.cc; tests/numericpublic1.cc.

PPL-SPEC-000092
2. Exact rational assignment of finite native floating-point values
UID: PPL-SPEC-000092
STATUS: APPROVED
COMPONENT: NUMERICS
APPLICABILITY: IF_CPP, SCALAR_MPQ, NATIVE_FP_AVAILABLE
VERIFICATION_METHODS: Test, Analysis, Inspection
LANGUAGE: CPP
VISIBILITY: Public
RELATIONS (Parent):
STATEMENT:

For a finite source of type float, double or long double whose corresponding PPL_SUPPORTED_FLOAT, PPL_SUPPORTED_DOUBLE or PPL_SUPPORTED_LONG_DOUBLE macro is nonzero, assign_r(to, from, dir) shall assign its exact mathematical value as a canonical rational and return V_EQ. This shall include signed zero, subnormal values where supported, and the finite endpoints of the source format. Either sign of floating-point zero shall produce rational 0/1. The result shall preserve the represented binary floating-point value, without substituting a nearby decimal interpretation.

ROUND_DOWN, ROUND_UP, ROUND_IGNORE and ROUND_NOT_NEEDED, with an optional ROUND_STRICT_RELATION flag, shall not change the assigned value or status. The current floating-point rounding direction shall not change the exact rational value. Nonfinite inputs and numeric policies for encoded special rationals shall be outside this finite-value contract.

The destination shall be a live rational object. Synchronization and resource prerequisites shall follow 🔗 1. Exact rational assignment from rational and integral sources. The floating-point source shall remain unchanged.

RATIONALE:

A finite binary floating-point value is an exact rational even when its short decimal spelling is not. Source-format range does not imply a bounded coefficient range restriction on the rational destination.

INTERFACE:

Public header: <ppl.hh>; effective PPL template instantiations for mpq_class:

Result assign_r(mpq_class& to, const float& from, Rounding_Dir dir);
Result assign_r(mpq_class& to, const double& from, Rounding_Dir dir);
Result assign_r(mpq_class& to, const long double& from, Rounding_Dir dir);
VERIFICATION_CRITERIA:

Check literal dyadic answers, both zeros, nonintegral values, subnormals, finite maxima and mantissa-boundary values for every source type. Establish expected rationals from integer significands and powers of two, independently of decimal printing. Repeat directions and supported hardware rounding states, preserving the source.

SOURCE:

src/checked_mpq_inlines.hh; src/checked_float_inlines.hh; src/Checked_Number_defs.hh and src/Checked_Number_inlines.hh; tests/rationalnumerics1.cc.

3. Exact finite rational addition, subtraction, multiplication and division
UID: PPL-SPEC-000093
STATUS: APPROVED
COMPONENT: NUMERICS
APPLICABILITY: IF_CPP, SCALAR_MPQ
VERIFICATION_METHODS: Test, Analysis, Inspection
LANGUAGE: CPP
VISIBILITY: Public
RELATIONS (Parent):
STATEMENT:

For source entry values a and b, add_assign_r, sub_assign_r and mul_assign_r shall assign a + b, a − b and ab, respectively. div_assign_r shall require b ≠ 0 and assign a ⁄ b. Each successful operation shall produce a canonical rational and return V_EQ. This shall include cancellation, zero numerators and arbitrarily large finite integral numerator or denominator values within available resources.

The two sources may be the same object. The directions ROUND_DOWN, ROUND_UP, ROUND_IGNORE and ROUND_NOT_NEEDED, with an optional ROUND_STRICT_RELATION flag, shall not change the exact value or status. A transparent rational policy shall not promise rejection of a zero divisor.

Canonical operands, synchronization and resource prerequisites shall follow 🔗 1. Exact rational assignment from rational and integral sources. The destination shall be a live rational object and may be any source; the operation shall use every operand's entry value. Distinct sources shall remain unchanged.

RATIONALE:

The rational arithmetic interface returns an explicit exactness classification while retaining mathematical values and canonical representation across aliases.

INTERFACE:

Public header: <ppl.hh>; effective PPL template instantiations for mpq_class:

Result add_assign_r(mpq_class& to, const mpq_class& x,
                    const mpq_class& y, Rounding_Dir dir);
Result sub_assign_r(mpq_class& to, const mpq_class& x,
                    const mpq_class& y, Rounding_Dir dir);
Result mul_assign_r(mpq_class& to, const mpq_class& x,
                    const mpq_class& y, Rounding_Dir dir);
Result div_assign_r(mpq_class& to, const mpq_class& x,
                    const mpq_class& y, Rounding_Dir dir);
VERIFICATION_CRITERIA:

Check literal reduced fractions and independent cross-product identities for both signs, zero and cancellation. Exercise distinct output, each input alias and all-same inputs, respecting nonzero division. Verify exact status and canonical output under every supported direction, with multi-limb inputs in all coefficient configurations.

SOURCE:

src/checked_mpq_inlines.hh; src/Checked_Number_defs.hh and src/Checked_Number_inlines.hh; tests/rationalnumerics1.cc; tests/numericpublic1.cc.

4. Rational sign assignments and integral-valued rounding operations
UID: PPL-SPEC-000094
STATUS: APPROVED
COMPONENT: NUMERICS
APPLICABILITY: IF_CPP, SCALAR_MPQ
VERIFICATION_METHODS: Test, Analysis, Inspection
LANGUAGE: CPP
VISIBILITY: Public
RELATIONS (Parent):
STATEMENT:

For source entry value a, neg_assign_r and abs_assign_r shall assign  − a and |a|. floor_assign_r, ceil_assign_r and trunc_assign_r shall assign, respectively, ⌊a⌋, ⌈a⌉ and trunc(a), where truncation is toward zero. The three integral-valued results shall have denominator one.

Each successful call shall return V_EQ for its specified operation's result, including rounding a nonintegral source to an integer. The status shall not assert that this rounded integer equals the original source. ROUND_DOWN, ROUND_UP, ROUND_IGNORE and ROUND_NOT_NEEDED, with an optional ROUND_STRICT_RELATION flag, shall leave the specified operation result and status unchanged.

Canonical operands, synchronization and resource prerequisites shall follow 🔗 1. Exact rational assignment from rational and integral sources. The destination shall be a live rational object and may be any source; the operation shall use every operand's entry value. Distinct sources shall remain unchanged.

RATIONALE:

An integer rounding operation can itself be computed exactly. Separating the operation from its source prevents misinterpreting its result classification as preservation of a nonintegral input.

INTERFACE:

Public header: <ppl.hh>; effective PPL template instantiations for mpq_class:

Result neg_assign_r(mpq_class& to, const mpq_class& from,
                    Rounding_Dir dir);
Result abs_assign_r(mpq_class& to, const mpq_class& from,
                    Rounding_Dir dir);
Result floor_assign_r(mpq_class& to, const mpq_class& from,
                      Rounding_Dir dir);
Result ceil_assign_r(mpq_class& to, const mpq_class& from,
                     Rounding_Dir dir);
Result trunc_assign_r(mpq_class& to, const mpq_class& from,
                      Rounding_Dir dir);
VERIFICATION_CRITERIA:

Check zero, both signs, integral values, proper fractions and values on either side of integral boundaries. Use literal floor, ceiling and truncation answers; check sign results, exact status, denominator one, source preservation and in-place operation for every admitted direction.

SOURCE:

src/checked_mpq_inlines.hh; src/Checked_Number_defs.hh and src/Checked_Number_inlines.hh; tests/rationalnumerics1.cc.

5. Truncated rational quotient and signed rational remainder
UID: PPL-SPEC-000095
STATUS: APPROVED
COMPONENT: NUMERICS
APPLICABILITY: IF_CPP, SCALAR_MPQ
VERIFICATION_METHODS: Test, Analysis, Inspection
LANGUAGE: CPP
VISIBILITY: Public
RELATIONS (Parent):
STATEMENT:

For rational source entry values a and nonzero b, idiv_assign_r(to, x, y, dir) shall assign q = trunc(a ⁄ b) as a canonical rational with denominator one. rem_assign_r(to, x, y, dir) shall assign r = a − btrunc(a ⁄ b). The remainder shall satisfy |r| < |b|; a nonzero remainder shall have the sign of a. Each successful call shall return V_EQ for its specified operation.

The two sources may be the same object. The directions ROUND_DOWN, ROUND_UP, ROUND_IGNORE and ROUND_NOT_NEEDED, with an optional ROUND_STRICT_RELATION flag, shall not change these truncation and remainder conventions or the returned exact status. A zero-divisor rejection guarantee shall not be inferred from this contract.

Canonical operands, synchronization and resource prerequisites shall follow 🔗 1. Exact rational assignment from rational and integral sources. The destination shall be a live rational object and may be any source; the operation shall use every operand's entry value. Distinct sources shall remain unchanged.

With a non-thread-safe build, calls to rem_assign_r shall not overlap PPL arithmetic in another thread. A thread-safe build shall provide independent temporary storage for each thread.

RATIONALE:

The rational remainder convention derives from truncating the ratio, rather than from independently rounding the dividend or divisor to integers.

INTERFACE:

Public header: <ppl.hh>; effective PPL template instantiations for mpq_class:

Result idiv_assign_r(mpq_class& to, const mpq_class& x,
                     const mpq_class& y, Rounding_Dir dir);
Result rem_assign_r(mpq_class& to, const mpq_class& x,
                    const mpq_class& y, Rounding_Dir dir);
VERIFICATION_CRITERIA:

Check literal quotient/remainder pairs for every sign combination, proper and improper rational ratios, exact division and zero dividends. Verify reconstruction and remainder bounds with independent integer numerator/denominator calculations. Exercise both input aliases and all-same nonzero calls, exact status and canonical representation.

SOURCE:

src/checked_mpq_inlines.hh; src/Checked_Number_defs.hh and src/Checked_Number_inlines.hh; tests/rationalnumerics1.cc.

6. Exact rational operations with powers of two
UID: PPL-SPEC-000096
STATUS: APPROVED
COMPONENT: NUMERICS
APPLICABILITY: IF_CPP, SCALAR_MPQ
VERIFICATION_METHODS: Test, Analysis, Inspection
LANGUAGE: CPP
VISIBILITY: Public
RELATIONS (Parent):
STATEMENT:

For source entry value a and unsigned int exponent e, let m = 2e. add_2exp_assign_r and sub_2exp_assign_r shall assign a + m and a − m. mul_2exp_assign_r and div_2exp_assign_r shall assign am and a ⁄ m.

umod_2exp_assign_r shall assign ru = a − m⌊a ⁄ m⌋, satisfying 0 ≤ ru < m. smod_2exp_assign_r shall assign rs = a − m⌊a ⁄ m + 1 ⁄ 2⌋, satisfying  − m ⁄ 2 ≤ rs < m ⁄ 2; the half-period tie shall select the negative endpoint. These remainder operations shall preserve fractional parts as required by their mathematical definitions.

Every successful call shall produce a canonical rational and return V_EQ. Exponent zero shall mean m = 1. The exponent range shall be subject to available resources; it shall not be capped by the configured coefficient width. ROUND_DOWN, ROUND_UP, ROUND_IGNORE and ROUND_NOT_NEEDED, with an optional ROUND_STRICT_RELATION flag, shall not change these exact results.

Canonical operands, synchronization and resource prerequisites shall follow 🔗 1. Exact rational assignment from rational and integral sources. The destination shall be a live rational object and may be any source; the operation shall use every operand's entry value. Distinct sources shall remain unchanged.

With a non-thread-safe build, calls to add_2exp_assign_r or sub_2exp_assign_r shall not overlap PPL arithmetic in another thread. A thread-safe build shall provide independent temporary storage for each thread.

RATIONALE:

Rational dyadic residues have distinct unsigned and centered conventions. Explicit half-open bounds distinguish them from truncated remainder and determine the tie at half a period.

INTERFACE:

Public header: <ppl.hh>; effective PPL template instantiations for mpq_class:

Result add_2exp_assign_r(mpq_class& to, const mpq_class& from,
                         unsigned int exp, Rounding_Dir dir);
Result sub_2exp_assign_r(mpq_class& to, const mpq_class& from,
                         unsigned int exp, Rounding_Dir dir);
Result mul_2exp_assign_r(mpq_class& to, const mpq_class& from,
                         unsigned int exp, Rounding_Dir dir);
Result div_2exp_assign_r(mpq_class& to, const mpq_class& from,
                         unsigned int exp, Rounding_Dir dir);
Result umod_2exp_assign_r(mpq_class& to, const mpq_class& from,
                          unsigned int exp, Rounding_Dir dir);
Result smod_2exp_assign_r(mpq_class& to, const mpq_class& from,
                          unsigned int exp, Rounding_Dir dir);
VERIFICATION_CRITERIA:

Check exponent zero, positive exponents beyond each bounded coefficient width, both signs, zero, proper fractions, exact multiples and each half-period boundary. Verify literal values, residue bounds and reconstruction, exact status, canonical representation and in-place use.

SOURCE:

src/checked_mpq_inlines.hh; src/Checked_Number_defs.hh and src/Checked_Number_inlines.hh; tests/rationalnumerics1.cc.

PPL-SPEC-000097
7. Exact rational product accumulation and subtraction
UID: PPL-SPEC-000097
STATUS: APPROVED
COMPONENT: NUMERICS
APPLICABILITY: IF_CPP, SCALAR_MPQ
VERIFICATION_METHODS: Test, Analysis, Inspection
LANGUAGE: CPP
VISIBILITY: Public
RELATIONS (Parent):
STATEMENT:

For destination and source entry values d, a and b, add_mul_assign_r shall assign d + ab and sub_mul_assign_r shall assign d − ab. Each successful call shall produce a canonical rational and return V_EQ. The destination may be either or both sources; the two sources may be the same object.

ROUND_DOWN, ROUND_UP, ROUND_IGNORE and ROUND_NOT_NEEDED, with an optional ROUND_STRICT_RELATION flag, shall not change the exact value or status. There shall be no bounded coefficient intermediate-range restriction on these rational operations.

Canonical operands, synchronization and resource prerequisites shall follow 🔗 1. Exact rational assignment from rational and integral sources. The destination shall be a live rational object and may be any source; the operation shall use every operand's entry value. Distinct sources shall remain unchanged.

RATIONALE:

Product accumulation must evaluate all three entry values consistently, including when a GMP expression references its own destination.

INTERFACE:

Public header: <ppl.hh>; effective PPL template instantiations for mpq_class:

Result add_mul_assign_r(mpq_class& to, const mpq_class& x,
                        const mpq_class& y, Rounding_Dir dir);
Result sub_mul_assign_r(mpq_class& to, const mpq_class& x,
                        const mpq_class& y, Rounding_Dir dir);
VERIFICATION_CRITERIA:

Check literal reduced answers for both operations, every source/destination alias partition, zero factors, mixed signs, cancellation and large numerator/denominator products. Verify exact status, canonical output and unchanged distinct sources across admitted directions.

SOURCE:

src/checked_mpq_inlines.hh; src/Checked_Number_defs.hh and src/Checked_Number_inlines.hh; tests/rationalnumerics1.cc.

PPL-SPEC-000098
8. Rational canonical-form inspection and coefficient component extraction
UID: PPL-SPEC-000098
STATUS: APPROVED
COMPONENT: NUMERICS
APPLICABILITY: IF_CPP, SCALAR_MPQ
VERIFICATION_METHODS: Test, Analysis, Inspection
LANGUAGE: CPP
VISIBILITY: Internal
RELATIONS (Parent):
STATEMENT:

is_canonical(x) shall return true exactly when x has positive denominator and coprime numerator and denominator. It shall admit a live mpq_class object with unreduced components or nonpositive denominator for inspection, preserve those components and return false when they violate the canonical conditions. It shall not normalize the inspected source.

For a finite canonical rational source with mathematical value n ⁄ d, numer_denom(from, numer, denom) shall assign its canonical numerator n and positive denominator d to the two configured coefficient outputs. The outputs shall be distinct and distinct from the source. Successful bounded-profile extraction shall require both components to be representable in the configured coefficient range. Native callers shall satisfy that range condition. Checked component overflow shall report std::overflow_error and retain the surviving-output guarantee of 🔗 5. Checked coefficient overflow and result-policy dispatch; an earlier output assignment need not be rolled back.

No library initialization object, thread initialization or coefficient- constant stage shall be required. Callers shall synchronize overlapping modifications of argument objects and serialize these temporary-storage operations across threads in a non-thread-safe build. A thread-safe build shall provide independent temporary storage for each thread. Available resources shall limit temporary rational storage.

RATIONALE:

Canonical-form inspection must be useful on unreduced data without mutating it. Component extraction crosses from unbounded rational storage to a possibly bounded coefficient profile and therefore has a separate range condition.

INTERFACE:

Internal interface and effective template instantiation in math_utilities_defs.hh within PPL:

bool is_canonical(const mpq_class& x);

void numer_denom(const mpq_class& from, Coefficient& numer,
                 Coefficient& denom);
VERIFICATION_CRITERIA:

Check reduced and unreduced positive denominators, both numerator signs, canonical and noncanonical zero, negative and zero denominators while preserving source components. Extract literal signed fractions, zero and unit-denominator values in every coefficient profile; use large components for mpz and checked overflow cases where supported.

SOURCE:

src/math_utilities_defs.hh and src/math_utilities_inlines.hh; tests/rationalnumerics1.cc.

PPL-SPEC-000099
9. Finite rational sign, comparison and numeric classification
UID: PPL-SPEC-000099
STATUS: APPROVED
COMPONENT: NUMERICS
APPLICABILITY: IF_CPP, SCALAR_MPQ
VERIFICATION_METHODS: Test, Analysis, Inspection
LANGUAGE: CPP
VISIBILITY: Public
RELATIONS (Parent):
STATEMENT:

For finite canonical rational values, sgn(x) shall return -1, 0 or 1 according to the sign of the numerator. cmp(x, y) shall return an int whose sign agrees with the mathematical ordering of the rationals; a nonzero magnitude shall not be prescribed.

is_integer(x) shall return true exactly when the canonical denominator is one. is_not_a_number(x), is_minus_infinity(x) and is_plus_infinity(x) shall each return false. These predicates shall return bool and shall preserve the operands. They shall not give a finite rational value an infinity merely because an associated bounded coefficient profile cannot hold its numerator or denominator. The admitted GMP special representations and their extended-integrality classification shall follow 🔗 2. GMP integer and rational special-value classification.

Canonical inputs, synchronization and resource prerequisites shall follow 🔗 1. Exact rational assignment from rational and integral sources.

RATIONALE:

Finite rational classification concerns mathematical value rather than the selected global coefficient range. Ordering can be exact without conversion to floating point.

INTERFACE:

Public header: <ppl.hh>; effective PPL template instantiations for mpq_class:

int sgn(const mpq_class& x);
int cmp(const mpq_class& x, const mpq_class& y);
bool is_integer(const mpq_class& x);
bool is_not_a_number(const mpq_class& x);
bool is_minus_infinity(const mpq_class& x);
bool is_plus_infinity(const mpq_class& x);
VERIFICATION_CRITERIA:

Check all sign and ordering outcomes, integral and nonintegral rationals, zero, equal distinct objects and fractions differing beyond floating-point precision. Require all finite special-value predicates to be false and preserve both operands, including large components in bounded coefficient builds.

SOURCE:

src/checked_mpq_inlines.hh; src/Checked_Number_defs.hh and src/Checked_Number_inlines.hh; tests/rationalnumerics1.cc.