{"record":{"id":"1e72866cf24ae83c","repo":"nautechsystems/nautilus_trader","slug":"raw-value-exceeds-quantityraw-range-in-quantity-f","errorCode":null,"errorMessage":"Raw value exceeds QuantityRaw range in Quantity::from_mantissa_exponent","messagePattern":"Raw value exceeds QuantityRaw range in Quantity::from_mantissa_exponent","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"crates/model/src/types/quantity.rs","lineNumber":537,"sourceCode":"    /// operations, making it ideal for exchange data that arrives as mantissa/exponent pairs.\n    ///\n    /// # Panics\n    ///\n    /// Panics if the resulting raw value exceeds [`QUANTITY_RAW_MAX`].\n    #[must_use]\n    pub fn from_mantissa_exponent(mantissa: u64, exponent: i8, precision: u8) -> Self {\n        check_fixed_precision(precision).expect_display(FAILED);\n\n        if mantissa == 0 {\n            return Self { raw: 0, precision };\n        }\n\n        let raw_i128 = mantissa_exponent_to_fixed_i128(i128::from(mantissa), exponent, precision)\n            .expect(\"Overflow in Quantity::from_mantissa_exponent\");\n\n        let raw: QuantityRaw = raw_i128\n            .try_into()\n            .expect(\"Raw value exceeds QuantityRaw range in Quantity::from_mantissa_exponent\");\n        assert!(\n            raw <= QUANTITY_RAW_MAX,\n            \"`raw` value {raw} exceeded QUANTITY_RAW_MAX={QUANTITY_RAW_MAX} for Quantity\"\n        );\n\n        Self { raw, precision }\n    }\n\n    /// Checked variant of [`Quantity::from_mantissa_exponent`].\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the precision is invalid or the resulting raw value\n    /// exceeds [`QUANTITY_RAW_MAX`].\n    pub fn from_mantissa_exponent_checked(\n        mantissa: u64,\n        exponent: i8,\n        precision: u8,","sourceCodeStart":519,"sourceCodeEnd":555,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/model/src/types/quantity.rs#L519-L555","documentation":"After the i128 conversion succeeds, from_mantissa_exponent narrows the i128 raw into QuantityRaw via try_into with expect; if the value exceeds QuantityRaw's range (negative or above QUANTITY_RAW_MAX), it panics. The subsequent assert documents the QUANTITY_RAW_MAX bound that must hold.","triggerScenarios":"Quantity::from_mantissa_exponent whose scaled raw fits i128 but exceeds QUANTITY_RAW_MAX, or is negative (negative mantissa), which try_into to the unsigned QuantityRaw rejects.","commonSituations":"Passing negative mantissas (signed deltas) where only unsigned quantities are allowed; high-precision builds with quantities beyond the raw bound; importing historical data with sentinel/placeholder max values.","solutions":["Ensure the mantissa is non-negative and the scaled value <= QUANTITY_RAW_MAX before calling.","Clamp or reject quantities exceeding the instrument's max quantity during ingestion.","Compute in Decimal first, validate against QUANTITY_RAW_MAX, then construct.","Use the high-precision build (wider QuantityRaw) if legitimate quantity ranges need it."],"exampleFix":"// before\nlet qty = Quantity::from_mantissa_exponent(-5_i64, 0, 8); // panics: negative raw into unsigned QuantityRaw\n// after\nlet qty = Quantity::from_mantissa_exponent(5_i64, 0, 8); // 5.00000000","handlingStrategy":"validation","validationCode":"// Rust\nfn quantity_raw_fits(mantissa: i64, exponent: i8, precision: u8) -> bool {\n    mantissa >= 0 && mantissa_exponent_to_fixed_i128(i128::from(mantissa), exponent, precision)\n        .map(|raw| raw >= 0 && raw <= i128::from(QUANTITY_RAW_MAX))\n        .unwrap_or(false)\n}","typeGuard":"fn to_quantity_raw(raw: i128) -> Option<QuantityRaw> {\n    QuantityRaw::try_from(raw).ok().filter(|r| *r <= QUANTITY_RAW_MAX)\n}","tryCatchPattern":null,"preventionTips":["Reject negative mantissas; Quantity is unsigned in normal builds.","Range-check against QUANTITY_RAW_MAX before constructing from raw math.","Filter sentinel/placeholder max values from imported data.","Prefer Decimal-based construction with explicit validation over raw mantissa/exponent calls."],"tags":["rust","arithmetic-overflow","panic","quantity"],"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"}