DecimalAnnex

main

A native IEEE 754 decimal floating-point type for Swift
xwu/DecimalAnnex

DecimalAnnex

This module provides Decimal128, a 128-bit decimal floating-point type with 34 significant decimal digits defined by the IEEE 754 specification.

import DecimalAnnex

let x: Decimal128 = 1/10
let y: Decimal128 = 2/10

x + y == 3/10  // true

Decimal128 supports signed zeros, subnormal values, infinities, and NaNs, along with familiar operations for classification, comparison, and rounding. It conforms to the standard library FloatingPoint protocol, making it usable in generic floating-point algorithms.

The implementation is written in native Swift and has no dependency on Foundation.

Representation

Decimal floating-point values have a quantum: the power of ten associated with their integer coefficient. This distinguishes representations such as 1.0 and 1.00 that compare equal to each other and hash identically. For arithmetic operations, IEEE 754 "preferred exponent" rules determine the representation of their results; use quantized(by:) to select a quantum explicitly.

To avoid loss of precision and preserve meaningful trailing zeros, Decimal128 doesn't conform to ExpressibleByFloatLiteral, but values can be created using decimal float literals by calling a macro.

let a = #Decimal128(1.0)
let b = #Decimal128(1.00)

print(a)                // 1.0
print(b)                // 1.00
a == b                  // true
a.quantum == b.quantum  // false

The macro will warn if creating a value from the literal argument requires inexact rounding; if the rounded value is finite, a fix-it is offered.

Rounding direction and status flags

Operations that may round their result can take a FloatingPointRoundingRule argument for non-default rounding direction. Callers can also provide an inout value to accumulate IEEE floating-point arithmetic status flags. The flags distinguish invalid operations, division by zero, overflow, underflow, and inexact results.

var flags: DecimalFloatingPointStatusFlags = []
_ = (1 as Decimal128).divided(by: 3, rounding: .toNearestOrEven, &flags)

flags.contains(.inexact)  // true

Description

  • Swift Tools 6.4.0
View More Packages from this Author

Dependencies

Last updated: Sat Nov 14 2026 14:12:52 GMT-1000 (Hawaii-Aleutian Standard Time)