1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
//! Financial-domain methods for `Decimal`.
//!
//! Trading operations that would otherwise be error-prone to implement
//! manually: midpoint, spread, tick rounding, basis points, percentage
//! calculations, and fused multiply-divide.
use crate::Decimal;
macro_rules! impl_decimal_financial {
($backing:ty) => {
impl<const D: u8> Decimal<$backing, D> {
// ========================================================
// Price operations
// ========================================================
/// Midpoint of two prices: `(self + other) / 2`.
///
/// Overflow-safe midpoint: `(self + other) / 2`.
///
/// Uses the bit-manipulation formula `(a & b) + ((a ^ b) >> 1)`
/// which is correct for all representable values without
/// intermediate overflow.
///
/// # Examples
///
/// ```
/// use nexus_decimal::Decimal;
/// type D64 = Decimal<i64, 8>;
///
/// let bid = D64::new(100, 0);
/// let ask = D64::new(101, 0);
/// assert_eq!(bid.midpoint(ask), D64::new(100, 50_000_000));
/// ```
#[inline(always)]
pub const fn midpoint(self, other: Self) -> Self {
// Overflow-safe integer average:
// avg(a, b) = (a & b) + ((a ^ b) >> 1)
// Correct for all values of the backing type, no overflow possible.
let a = self.value;
let b = other.value;
Self {
value: (a & b) + ((a ^ b) >> 1),
}
}
/// Spread between two prices: `self - other`.
///
/// Returns `None` if `self < other` (crossed market).
#[inline(always)]
pub const fn spread(self, other: Self) -> Option<Self> {
if self.value < other.value {
None
} else {
match self.value.checked_sub(other.value) {
Some(v) => Some(Self { value: v }),
None => None,
}
}
}
/// Round to nearest tick size.
///
/// `tick` must be positive. Rounds to the nearest multiple
/// of `tick` using banker's rounding on the remainder.
///
/// # Examples
///
/// ```
/// use nexus_decimal::Decimal;
/// type D64 = Decimal<i64, 8>;
///
/// let price = D64::new(1, 23_700_000); // 1.237
/// let tick = D64::new(0, 5_000_000); // 0.05
/// assert_eq!(price.round_to_tick(tick), Some(D64::new(1, 25_000_000))); // 1.25
/// ```
#[inline(always)]
pub const fn round_to_tick(self, tick: Self) -> Option<Self> {
assert!(tick.value > 0, "tick must be positive");
let remainder = self.value % tick.value;
let half_tick = tick.value / 2;
let base = self.value - remainder;
if remainder > half_tick {
match base.checked_add(tick.value) {
Some(v) => Some(Self { value: v }),
None => None,
}
} else if remainder < -half_tick {
match base.checked_sub(tick.value) {
Some(v) => Some(Self { value: v }),
None => None,
}
} else if remainder == half_tick || remainder == -half_tick {
let quotient = self.value / tick.value;
if quotient % 2 != 0 {
if remainder > 0 {
match base.checked_add(tick.value) {
Some(v) => Some(Self { value: v }),
None => None,
}
} else {
match base.checked_sub(tick.value) {
Some(v) => Some(Self { value: v }),
None => None,
}
}
} else {
Some(Self { value: base })
}
} else {
Some(Self { value: base })
}
}
/// Floor to tick: round down to nearest multiple of `tick`.
///
/// Returns `None` if the result would overflow.
#[inline(always)]
pub const fn floor_to_tick(self, tick: Self) -> Option<Self> {
assert!(tick.value > 0, "tick must be positive");
let remainder = self.value % tick.value;
if remainder >= 0 {
Some(Self {
value: self.value - remainder,
})
} else {
match (self.value - remainder).checked_sub(tick.value) {
Some(v) => Some(Self { value: v }),
None => None,
}
}
}
/// Ceil to tick: round up to nearest multiple of `tick`.
///
/// Returns `None` if the result would overflow.
#[inline(always)]
pub const fn ceil_to_tick(self, tick: Self) -> Option<Self> {
assert!(tick.value > 0, "tick must be positive");
let remainder = self.value % tick.value;
if remainder > 0 {
match (self.value - remainder).checked_add(tick.value) {
Some(v) => Some(Self { value: v }),
None => None,
}
} else if remainder < 0 {
Some(Self {
value: self.value - remainder,
})
} else {
Some(self)
}
}
// ========================================================
// Division shortcuts
// ========================================================
/// Divide by 2 using integer division. Truncates toward zero.
///
/// The compiler optimizes this to a shift + sign-bit adjustment.
#[inline(always)]
pub const fn halve(self) -> Self {
Self {
value: self.value / 2,
}
}
/// Divide by 10 using integer division.
#[inline(always)]
pub const fn div10(self) -> Self {
Self {
value: self.value / 10,
}
}
/// Divide by 100 using integer division.
#[inline(always)]
pub const fn div100(self) -> Self {
Self {
value: self.value / 100,
}
}
// ========================================================
// Comparison helpers
// ========================================================
/// Returns `true` if `self` is within `tolerance` of `other`.
///
/// Equivalent to `|self - other| <= tolerance`. Returns `false`
/// when the difference overflows (values with opposite signs
/// near `MAX`/`MIN`).
#[inline]
pub const fn approx_eq(self, other: Self, tolerance: Self) -> bool {
let (diff, overflow) = if self.value >= other.value {
self.value.overflowing_sub(other.value)
} else {
other.value.overflowing_sub(self.value)
};
!overflow && diff <= tolerance.value
}
/// Clamp to a price range `[min, max]`.
#[inline]
pub const fn clamp_price(self, min: Self, max: Self) -> Self {
if self.value < min.value {
min
} else if self.value > max.value {
max
} else {
self
}
}
}
};
}
impl_decimal_financial!(i32);
impl_decimal_financial!(i64);
impl_decimal_financial!(i128);
// ============================================================================
// Methods that need widening (per-backing-type, not in shared macro)
// ============================================================================
// --- i32: widen to i64 for percent/bps calculations ---
impl<const D: u8> Decimal<i32, D> {
/// Compute `self * percent / 100` via single truncating division.
///
/// `percent` is in percentage points: 50 means 50%.
#[inline]
pub const fn percent_of(self, percent: Self) -> Option<Self> {
let product = (self.value as i64) * (percent.value as i64);
let scale_100 = (Self::SCALE as i64) * 100;
let result = product / scale_100;
if result > i32::MAX as i64 || result < i32::MIN as i64 {
None
} else {
Some(Self {
value: result as i32,
})
}
}
/// Convert to basis points: `self * 10000`.
#[inline]
pub const fn to_bps(self) -> Option<Self> {
self.mul_int(10_000)
}
/// Create from basis points: `bps / 10000`.
#[inline]
pub const fn from_bps(bps: i32) -> Option<Self> {
let scaled = bps as i64 * Self::SCALE as i64 / 10_000;
if scaled > i32::MAX as i64 || scaled < i32::MIN as i64 {
None
} else {
Some(Self {
value: scaled as i32,
})
}
}
/// Fused multiply-divide: `(self * a) / b` with single rounding.
#[inline]
pub const fn mul_div(self, mul: Self, div: Self) -> Option<Self> {
if div.value == 0 {
return None;
}
let product = (self.value as i64) * (mul.value as i64);
let result = product / (div.value as i64);
if result > i32::MAX as i64 || result < i32::MIN as i64 {
None
} else {
Some(Self {
value: result as i32,
})
}
}
}
// --- i64: widen to i128 for percent/bps calculations ---
impl<const D: u8> Decimal<i64, D> {
/// Compute `self * percent / 100` via single truncating division.
///
/// `percent` is in percentage points: 50 means 50%.
#[inline]
pub const fn percent_of(self, percent: Self) -> Option<Self> {
let product = (self.value as i128) * (percent.value as i128);
let scale_100 = (Self::SCALE as i128) * 100;
let result = product / scale_100;
if result > i64::MAX as i128 || result < i64::MIN as i128 {
None
} else {
Some(Self {
value: result as i64,
})
}
}
/// Convert to basis points: `self * 10000`.
#[inline]
pub const fn to_bps(self) -> Option<Self> {
self.mul_int(10_000)
}
/// Create from basis points: `bps / 10000`.
#[inline]
pub const fn from_bps(bps: i64) -> Option<Self> {
let scaled = (bps as i128) * (Self::SCALE as i128);
let value = scaled / 10_000;
if value > i64::MAX as i128 || value < i64::MIN as i128 {
None
} else {
Some(Self {
value: value as i64,
})
}
}
/// Fused multiply-divide: `(self * a) / b` with single rounding.
///
/// Keeps the full i128 intermediate — single rounding at the end.
/// The primitive behind fee calculation, VWAP, cross-rates.
#[inline]
pub const fn mul_div(self, mul: Self, div: Self) -> Option<Self> {
if div.value == 0 {
return None;
}
let product = (self.value as i128) * (mul.value as i128);
let result = product / (div.value as i128);
if result > i64::MAX as i128 || result < i64::MIN as i128 {
None
} else {
Some(Self {
value: result as i64,
})
}
}
}
// --- i128: uses wide arithmetic for percent/bps ---
impl<const D: u8> Decimal<i128, D> {
/// Convert to basis points: `self * 10000`.
#[inline]
pub const fn to_bps(self) -> Option<Self> {
self.mul_int(10_000)
}
/// Create from basis points: `bps / 10000`.
#[inline]
pub const fn from_bps(bps: i128) -> Option<Self> {
match (bps).checked_mul(Self::SCALE) {
Some(scaled) => Some(Self {
value: scaled / 10_000,
}),
None => None,
}
}
/// Fused multiply-divide: `(self * a) / b` with single rounding.
///
/// For i128, delegates to checked_mul then checked_div.
/// Not truly fused (two rounding events) — a 256-bit intermediate
/// would be needed for true single-rounding on i128.
#[inline]
pub fn mul_div(self, mul: Self, div: Self) -> Option<Self> {
if div.value == 0 {
return None;
}
let product = self.checked_mul(mul)?;
product.checked_div(div)
}
}