{"record":{"id":"e49e1b11797f3d4e","repo":"nautechsystems/nautilus_trader","slug":"whole-raw-quantity-must-fit-in-decimal","errorCode":null,"errorMessage":"Whole raw quantity must fit in Decimal","messagePattern":"Whole raw quantity must fit in Decimal","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/model/src/types/quantity.rs","lineNumber":443,"sourceCode":"        // because our quantity constraints ensure the maximum raw value times the scaling\n        // factor cannot exceed i128::MAX (high-precision) or i64::MAX (standard-precision).\n        #[allow(\n            clippy::unnecessary_cast,\n            clippy::cast_lossless,\n            reason = \"cast is real when QuantityRaw is u64, no-op when u128\"\n        )]\n        scaled_raw_to_decimal(rescaled_raw as i128, self.precision)\n    }\n\n    /// Returns a raw fixed-point quantity as a `Decimal`.\n    #[must_use]\n    #[allow(\n        clippy::unnecessary_fallible_conversions,\n        reason = \"try_from is infallible when QuantityRaw is u64, fallible when u128\"\n    )]\n    pub(crate) fn raw_as_decimal(raw: QuantityRaw) -> Decimal {\n        let whole =\n            i128::try_from(raw / FIXED_SCALAR_RAW).expect(\"Whole raw quantity must fit in Decimal\");\n        let fractional = i128::try_from(raw % FIXED_SCALAR_RAW)\n            .expect(\"Fractional raw quantity must fit in Decimal\");\n\n        Decimal::from(whole) + Decimal::from_i128_with_scale(fractional, u32::from(FIXED_PRECISION))\n    }\n\n    /// Returns a formatted string representation of this instance.\n    #[must_use]\n    pub fn to_formatted_string(&self) -> String {\n        format!(\"{self}\").separate_with_underscores()\n    }\n\n    /// Creates a new [`Quantity`] from a `Decimal` value with specified precision.\n    ///\n    /// Uses pure integer arithmetic on the Decimal's mantissa and scale for fast conversion.\n    /// The value is rounded to the specified precision using banker's rounding (round half to even).\n    ///\n    /// # Errors","sourceCodeStart":425,"sourceCodeEnd":461,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/model/src/types/quantity.rs#L425-L461","documentation":"Quantity::raw_as_decimal splits a raw fixed-point quantity into whole and fractional parts for Decimal conversion; the whole part (raw / FIXED_SCALAR_RAW) is narrowed to i128 with try_from and expect. If the whole component cannot fit in i128 (only possible when QuantityRaw is u128 in high-precision builds), this panics.","triggerScenarios":"Calling internal raw_as_decimal (used by Quantity::as_decimal and Display) with a raw u128 quantity whose whole part exceeds i128::MAX; triggered indirectly by any Display/format/as_decimal call on such a Quantity.","commonSituations":"High-precision builds with astronomically large quantities produced by unbounded multiplication; aggregating quantities until the raw value passes i128 range; data-import bugs writing sentinel max values.","solutions":["Keep accumulated quantities below i128 range: validate raw <= i128::MAX * FIXED_SCALAR_RAW before further accumulation.","Cap quantities at domain level (max order/position sizes) so raw values stay far below the boundary.","Use a wider accumulation type (big-int) for aggregation and only construct Quantity after validation.","Report this as a bug if hit with ordinary values; in default u64 builds it is unreachable."],"exampleFix":"// before\nlet huge = Quantity::from_raw(u128::MAX / 2, precision);\nprintln!(\"{}\", huge); // panics inside raw_as_decimal\n// after\nlet qty = Quantity::from_raw(1_500_000_u64, precision); // comfortably representable\nprintln!(\"{}\", qty);","handlingStrategy":"validation","validationCode":"// Rust (high-precision u128 builds)\nfn whole_fits_i128(raw: u128, fixed_scalar_raw: u128) -> bool {\n    raw / fixed_scalar_raw <= i128::MAX as u128\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Cap quantities at domain level so raw values stay far below i128 range.","In high-precision builds, validate u128 raws before formatting/displaying quantities.","Avoid sentinel max values (u128::MAX) leaking in from imports or fixtures.","Report occurrences with ordinary values as bugs; unreachable in default u64 builds."],"tags":["rust","arithmetic-overflow","panic","quantity","decimal"],"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-14T00:17:10.932Z"}