Skip to content

Add General Decimal Arithmetic ordering, adjacency, and exponent operations#150

Open
Btocode wants to merge 1 commit into
cockroachdb:masterfrom
Btocode:gda-ordering-ops
Open

Add General Decimal Arithmetic ordering, adjacency, and exponent operations#150
Btocode wants to merge 1 commit into
cockroachdb:masterfrom
Btocode:gda-ordering-ops

Conversation

@Btocode

@Btocode Btocode commented Jul 22, 2026

Copy link
Copy Markdown

Adds the following General Decimal Arithmetic (GDA) / IEEE 754 decimal operations that apd was missing:

  • Context.Max, Min, MaxAbs, MinAbs — numeric and by-magnitude minimum and maximum, with the specification's NaN and total-order tie-break rules.
  • Context.NextPlus, NextMinus, NextToward — the representable value adjacent to an operand in a given direction.
  • Context.LogB, ScaleB — the adjusted exponent, and scaling by a power of ten.
  • Context.CopySign — the magnitude of one operand with the sign of another.
  • Decimal.SameQuantum and Decimal.CmpTotalMag — companions to the existing CmpTotal.

The operations follow the GDA specification, including the condition flags they raise (InvalidOperation, DivisionByZero, Overflow, Underflow, Subnormal, Inexact, Rounded, Clamped). Implementation notes worth a reviewer's attention:

  • Max/Min treat a quiet NaN operand as absent (return the other operand); only a signaling NaN raises InvalidOperation. Ties (numerically equal operands) are broken by total order, so e.g. Max(1.0, 1.00) returns 1.0.
  • The next-* operations compute increments on non-negative magnitudes (via nextPlus(x) == -nextMinus(-x)) to avoid relying on directed rounding of negative subnormals, and clamp directed-rounding overflow to Nmax rather than infinity.

New table-driven tests cover signed zeros, subnormals near Etiny, values near Nmax, infinities, and quiet/signaling NaNs. The expected values were additionally cross-checked against the Python standard library's decimal module (which implements the same specification) over tens of thousands of randomized inputs.

…ations

Implement the following GDA / IEEE 754 decimal operations that were
missing from apd:

  - Context.Max, Min, MaxAbs, MinAbs: numeric and by-magnitude minimum
    and maximum, with the specification's NaN and total-order tie-break
    rules.
  - Context.NextPlus, NextMinus, NextToward: the representable value
    adjacent to an operand in a given direction.
  - Context.LogB, ScaleB: the adjusted exponent, and scaling by a power
    of ten.
  - Context.CopySign: the magnitude of one operand with the sign of
    another.
  - Decimal.SameQuantum and Decimal.CmpTotalMag: companions to the
    existing CmpTotal.

The operations follow the General Decimal Arithmetic specification,
including the condition flags they raise. New table-driven tests cover
signed zeros, subnormals near Etiny, values near Nmax, infinities, and
quiet and signaling NaNs; the expected values were cross-checked against
the Python standard library's decimal module.

Signed-off-by: S Afsan Rahmatullah <afsan.scorp@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant