{"record":{"id":"0ec1f4c911afa923","repo":"nautechsystems/nautilus_trader","slug":"cannot-divide-by-zero","errorCode":null,"errorMessage":"Cannot divide by zero","messagePattern":"Cannot divide by zero","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/model/src/defi/tick_map/full_math.rs","lineNumber":183,"sourceCode":"        // Check if there's a remainder\n        if a.mul_mod(b, denominator).is_zero() {\n            Ok(result)\n        } else if result == U256::MAX {\n            anyhow::bail!(\"Result would overflow 256 bits\")\n        } else {\n            Ok(result + U256::from(1))\n        }\n    }\n\n    /// Calculates ceil(a÷b) with proper rounding up\n    /// Equivalent to Solidity's divRoundingUp function\n    ///\n    /// # Errors\n    ///\n    /// Returns error if `b` is zero or if the rounded quotient would overflow `U256`.\n    pub fn div_rounding_up(a: U256, b: U256) -> anyhow::Result<U256> {\n        if b.is_zero() {\n            anyhow::bail!(\"Cannot divide by zero\");\n        }\n\n        let quotient = a / b;\n        let remainder = a % b;\n\n        // Add 1 if there's a remainder (equivalent to gt(mod(x, y), 0) in assembly)\n        if remainder > U256::ZERO {\n            // Check for overflow before incrementing\n            if quotient == U256::MAX {\n                anyhow::bail!(\"Result would overflow 256 bits\");\n            }\n            Ok(quotient + U256::from(1))\n        } else {\n            Ok(quotient)\n        }\n    }\n\n    /// Computes the integer square root of a 256-bit unsigned integer using the Babylonian method","sourceCodeStart":165,"sourceCodeEnd":201,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/model/src/defi/tick_map/full_math.rs#L165-L201","documentation":"div_rounding_up received b == 0 while computing the ceiling of a÷b for U256 values modeled on Solidity's divRoundingUp; division by zero is mathematically undefined, so the helper bails before attempting the division.","triggerScenarios":"Calling div_rounding_up(a, U256::zero()) — e.g. a computed price, liquidity, or tick spacing that evaluated to zero from upstream data.","commonSituations":"Empty/uninitialized pool state feeding zero liquidity; config where a scaling factor defaults to 0; division by a value fetched from a bad RPC response.","solutions":["Check that the denominator is nonzero before calling and fix the source of the zero value","anyhow::ensure!(!b.is_zero(), ...) at the call site for a clear local error","Add validation when loading pool/config parameters so zero denominators fail early"],"exampleFix":"// before\nlet q = div_rounding_up(a, liquidity)?;\n// after\nanyhow::ensure!(!liquidity.is_zero(), \"liquidity must be > 0\");\nlet q = div_rounding_up(a, liquidity)?;","handlingStrategy":"validation","validationCode":"anyhow::ensure!(!denominator.is_zero(), \"denominator must be > 0\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate pool liquidity/tick spacing are nonzero when loading state","Default-zero config values should be rejected at startup","Check RPC-sourced values for zero before math"],"tags":["rust","math","u256","division"],"backgroundTag":"invalid-argument-value","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"}