{"record":{"id":"3318bd21bacd3f9a","repo":"nautechsystems/nautilus_trader","slug":"overflow-occurred-when-multiplying-quantity","errorCode":null,"errorMessage":"Overflow occurred when multiplying `Quantity`","messagePattern":"Overflow occurred when multiplying `Quantity`","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/model/src/types/quantity.rs","lineNumber":753,"sourceCode":"    }\n}\n\nimpl Mul for Quantity {\n    type Output = Self;\n    fn mul(self, rhs: Self) -> Self::Output {\n        let result_raw = if self.raw != QUANTITY_UNDEF\n            && rhs.raw != QUANTITY_UNDEF\n            && self.precision <= FIXED_PRECISION\n            && rhs.precision <= FIXED_PRECISION\n        {\n            checked_mul_div_fixed(self.raw, rhs.raw)\n        } else {\n            let scalar = QuantityRaw::try_from(raw_scale(self.precision.min(rhs.precision)))\n                .expect(\"Fixed-point scale fits QuantityRaw\");\n            checked_mul_div_raw(self.raw, rhs.raw, scalar)\n        }\n        .filter(|raw| *raw <= QUANTITY_RAW_MAX)\n        .expect(\"Overflow occurred when multiplying `Quantity`\");\n\n        Self {\n            raw: result_raw,\n            precision: self.precision.max(rhs.precision),\n        }\n    }\n}\n\nimpl Add<Decimal> for Quantity {\n    type Output = Decimal;\n    fn add(self, rhs: Decimal) -> Self::Output {\n        self.as_decimal() + rhs\n    }\n}\n\nimpl Sub<Decimal> for Quantity {\n    type Output = Decimal;\n    fn sub(self, rhs: Decimal) -> Self::Output {","sourceCodeStart":735,"sourceCodeEnd":771,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/model/src/types/quantity.rs#L735-L771","documentation":"After checked_mul_div_raw computes the product at the minimum precision, the raw result is filtered against QUANTITY_RAW_MAX; exceeding it panics. Quantity's internal representation (u64 raw) cannot hold the product at the requested precision, so multiplication overflows the type.","triggerScenarios":"Calling Quantity::mul on two large quantities (raw values whose product, scaled by raw_scale of the min precision) exceeds QUANTITY_RAW_MAX — e.g. multiplying two quantities with large raw components at high precision.","commonSituations":"Position/size math on very large notional values or very fine precision instruments; compounding multiplications without bounds checks; accidental unit mistakes (multiplying quantity by quantity instead of scaling).","solutions":["Validate operand magnitudes before multiplying; use checked_mul or is_valid checks on inputs.","Reduce precision of operands if the product must fit (fewer fractional digits means smaller raw).","Use f64 or a wider decimal type for intermediate math and convert back with Quantity::new_checked.","Reconsider the math — quantity * quantity rarely models a quantity; verify units."],"exampleFix":"// before\nlet total = qty * factor; // panics when raw exceeds QUANTITY_RAW_MAX\n// after\nlet total = qty.checked_mul(factor).unwrap_or_else(|| {\n    eprintln!(\"quantity multiplication would overflow\");\n    Quantity::zero(factor.precision)\n});","handlingStrategy":"validation","validationCode":"fn mul_fits(a: &Quantity, b: &Quantity) -> bool {\n    a.raw.checked_mul(b.raw).map(|r| r <= QUANTITY_RAW_MAX).unwrap_or(false)\n}","typeGuard":null,"tryCatchPattern":"let result = std::panic::catch_unwind(|| a * b)\n    .ok()\n    .and_then(|q| q.downcast_ref::<Quantity>().cloned());","preventionTips":["Prefer checked_mul / checked arithmetic for large quantities","Reconsider quantity*quantity semantics — verify units","Reduce precision if products of fine-precision quantities are expected"],"tags":["rust","arithmetic","overflow","fixed-point"],"backgroundTag":"value-out-of-range","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}