{"record":{"id":"1bb159ed14d32f18","repo":"nautechsystems/nautilus_trader","slug":"overflow-when-scaling-f64-to-fixed-point-i128","errorCode":null,"errorMessage":"Overflow when scaling f64 to fixed-point i128","messagePattern":"Overflow when scaling f64 to fixed-point i128","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/model/src/types/fixed.rs","lineNumber":895,"sourceCode":"///\n/// # Panics\n///\n/// Panics if `precision` exceeds [`FIXED_PRECISION`], or if scaling the rounded value\n/// overflows the raw integer range.\n#[must_use]\n#[expect(\n    clippy::cast_precision_loss,\n    clippy::cast_possible_truncation,\n    reason = \"f64 to fixed-point conversion is inherently lossy; callers validate range and finiteness\"\n)]\npub fn f64_to_fixed_i128(value: f64, precision: u8) -> i128 {\n    check_fixed_precision(precision).expect_display(FAILED);\n    let pow1 = 10_i128.pow(u32::from(precision));\n    let pow2 = 10_i128.pow(u32::from(FIXED_PRECISION - precision));\n    let rounded = (value * pow1 as f64).round() as i128;\n    rounded\n        .checked_mul(pow2)\n        .expect(\"Overflow when scaling f64 to fixed-point i128\")\n}\n\n/// Converts an `f64` value to a raw fixed-point `u64` representation with a specified precision.\n///\n/// Callers are expected to validate that `value` is finite and non-negative; non-finite\n/// and negative values saturate at the integer bounds during the float-to-integer cast.\n///\n/// # Panics\n///\n/// Panics if `precision` exceeds [`FIXED_PRECISION`], or if scaling the rounded value\n/// overflows the raw integer range.\n#[must_use]\n#[expect(\n    clippy::cast_precision_loss,\n    clippy::cast_possible_truncation,\n    clippy::cast_sign_loss,\n    reason = \"f64 to fixed-point conversion is inherently lossy; callers validate range and finiteness\"\n)]","sourceCodeStart":877,"sourceCodeEnd":913,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/model/src/types/fixed.rs#L877-L913","documentation":"f64_to_fixed_i128 converts a float to raw fixed-point i128 by rounding at the requested precision then multiplying by 10^(FIXED_PRECISION - precision). The checked_mul panics with this message when the result exceeds i128 range. It enforces the fixed-point representable-range invariant.","triggerScenarios":"Calling f64_to_fixed_i128(value, precision) where |value| rounded and scaled exceeds i128::MAX/MIN — requires astronomically large values (~1.7e38 at precision 0) or the float cast producing i128::MAX from non-finite input.","commonSituations":"Feeding unvalidated float data (NaN/inf or huge numbers parsed from files/APIs) into the fixed-point conversion; fuzz or boundary tests.","solutions":["Validate the value is finite and within range before calling (the doc comment already requires callers to do this).","Clamp or reject out-of-range inputs at the data-ingestion boundary.","Use f64_to_fixed_u128 if the value is non-negative and extremely large (within u128 range)."],"exampleFix":"// before\nlet raw = f64_to_fixed_i128(value, precision); // panics if out of range\n// after\nif !value.is_finite() || value.abs() > i128::MAX as f64 / 10f64.powi(FIXED_PRECISION) {\n    return Err(...);\n}\nlet raw = f64_to_fixed_i128(value, precision);","handlingStrategy":"validation","validationCode":"const MAX: f64 = i128::MAX as f64 / 10f64.powi(FIXED_PRECISION);\nassert!(value.is_finite() && value.abs() <= MAX);\nlet raw = f64_to_fixed_i128(value, precision);","typeGuard":"fn fits_i128_fixed(value: f64) -> bool {\n    value.is_finite() && value.abs() <= i128::MAX as f64 / 10f64.powi(FIXED_PRECISION)\n}","tryCatchPattern":null,"preventionTips":["Reject non-finite floats before any fixed-point conversion.","Check magnitude against the scaled max before conversion.","Sanitize external numeric inputs at parse time."],"tags":["rust","overflow","fixed-point","arithmetic"],"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"}