Skip to content

Commit 00b14db

Browse files
committed
feat(determin): add Dfp::from_signed, DFP_CANONICAL_NAN, DQA CANONICAL_ZERO
Add missing API surface identified in DFP (RFC-0104) and DQA (RFC-0105) code reviews: - Dfp::from_signed(mantissa, exponent): creates DFP from signed mantissa, extracting sign automatically (RFC-0104 D2) - DFP_CANONICAL_NAN constant: canonical NaN for pattern-matching (RFC-0104 D3) - DQA CANONICAL_ZERO constant: canonical zero value=0, scale=0 (RFC-0105 D1) - Re-export CANONICAL_ZERO from crate root
1 parent 9104cf2 commit 00b14db

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

determin/src/dqa.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ pub struct Dqa {
109109
pub scale: u8,
110110
}
111111

112+
/// Canonical zero value (value=0, scale=0)
113+
pub const CANONICAL_ZERO: Dqa = Dqa { value: 0, scale: 0 };
114+
112115
impl Dqa {
113116
/// Create DQA from value and scale
114117
/// Returns Error::InvalidScale if scale > 18

determin/src/lib.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub use decimal::{
6060
MAX_DECIMAL_OP_COST, MAX_DECIMAL_SCALE, MIN_DECIMAL_MANTISSA,
6161
};
6262
pub use dmat::{DMat, DmatError, NumericScalar};
63-
pub use dqa::{dqa_abs, dqa_assign_to_column, dqa_cmp, dqa_negate, Dqa, DqaEncoding, DqaError};
63+
pub use dqa::{dqa_abs, dqa_assign_to_column, dqa_cmp, dqa_negate, CANONICAL_ZERO, Dqa, DqaEncoding, DqaError};
6464
pub use dvec::{
6565
dot_product, norm, normalize, squared_distance, vec_add, vec_mul, vec_scale, vec_sub, DVec,
6666
DvecError, DvecScalar,
@@ -189,6 +189,23 @@ impl Dfp {
189189
debug_assert!(self.mantissa % 2 == 1 || self.mantissa == 0);
190190
}
191191

192+
/// Create DFP from signed mantissa and exponent
193+
/// Extracts sign from the mantissa automatically
194+
pub fn from_signed(mantissa: i128, exponent: i32) -> Self {
195+
if mantissa == 0 {
196+
return Dfp::zero();
197+
}
198+
let sign = mantissa < 0;
199+
let mut dfp = Dfp {
200+
mantissa: mantissa.unsigned_abs(),
201+
exponent,
202+
class: DfpClass::Normal,
203+
sign,
204+
};
205+
dfp.normalize();
206+
dfp
207+
}
208+
192209
/// Create DFP from i64 (integer)
193210
pub fn from_i64(val: i64) -> Self {
194211
if val == 0 {
@@ -350,6 +367,14 @@ pub const DFP_MIN: Dfp = Dfp {
350367
exponent: DFP_MAX_EXPONENT,
351368
};
352369

370+
/// Canonical NaN value
371+
pub const DFP_CANONICAL_NAN: Dfp = Dfp {
372+
class: DfpClass::NaN,
373+
sign: false,
374+
mantissa: 0,
375+
exponent: 0,
376+
};
377+
353378
/// DFP encoding for serialization (24 bytes)
354379
/// Layout: [mantissa: 16 bytes, exponent: 4 bytes, class_sign: 4 bytes]
355380
#[derive(Clone, Copy, Debug, PartialEq, Eq)]

0 commit comments

Comments
 (0)