{"record":{"id":"1458efd756093061","repo":"TheAlgorithms/Python","slug":"step-size-must-be-positive","errorCode":null,"errorMessage":"Step size must be positive.","messagePattern":"Step size must be positive\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/numerical_analysis/adams_bashforth.py","lineNumber":59,"sourceCode":"    Traceback (most recent call last):\n        ...\n    ValueError: Step size must be positive.\n    \"\"\"\n\n    func: Callable[[float, float], float]\n    x_initials: list[float]\n    y_initials: list[float]\n    step_size: float\n    x_final: float\n\n    def __post_init__(self) -> None:\n        if self.x_initials[-1] >= self.x_final:\n            raise ValueError(\n                \"The final value of x must be greater than the initial values of x.\"\n            )\n\n        if self.step_size <= 0:\n            raise ValueError(\"Step size must be positive.\")\n\n        if not all(\n            round(x1 - x0, 10) == self.step_size\n            for x0, x1 in zip(self.x_initials, self.x_initials[1:])\n        ):\n            raise ValueError(\"x-values must be equally spaced according to step size.\")\n\n    def step_2(self) -> np.ndarray:\n        \"\"\"\n        >>> def f(x, y):\n        ...     return x\n        >>> AdamsBashforth(f, [0, 0.2], [0, 0], 0.2, 1).step_2()\n        array([0.  , 0.  , 0.06, 0.16, 0.3 , 0.48])\n\n        >>> AdamsBashforth(f, [0, 0.2, 0.4], [0, 0, 0.04], 0.2, 1).step_2()\n        Traceback (most recent call last):\n            ...\n        ValueError: Insufficient initial points information.","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/numerical_analysis/adams_bashforth.py#L41-L77","documentation":"Raised by the AdamsBashforth dataclass __post_init__ in maths/numerical_analysis/adams_bashforth.py when step_size <= 0. The solver advances the ODE solution in increments of step_size; a zero or negative step is physically meaningless and would loop forever or march backwards, so the constructor rejects it before any stepping method runs.","triggerScenarios":"AdamsBashforth(f, [0, 0.2], [0, 0], 0, 1) or any negative step_size; also step_size computed as (x_final - x0)/n where n overflows to 0 or the numerator has the wrong sign.","commonSituations":"step_size derived from a division that yields 0 (e.g. int truncation), sign errors when integrating 'backwards' (this API does not support negative steps), or config defaults left at 0.","solutions":["Pass a positive step: AdamsBashforth(f, [0, 0.2], [0, 0], 0.2, 1.0).","Compute step_size = (x_final - x_initials[-1]) / n with n a positive int and verify it is > 0.","To integrate backwards, transform the ODE (substitute t -> -t) instead of using a negative step."],"exampleFix":"# before\nAdamsBashforth(f, [0, 0.2], [0, 0], 0, 1.0)\n\n# after\nAdamsBashforth(f, [0, 0.2], [0, 0], 0.2, 1.0)","handlingStrategy":"validation","validationCode":"if step_size <= 0:\n    raise ValueError('step_size must be > 0')\n# or derive: step_size = (x_final - x_initials[-1]) / n  (n positive int)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never allow int division to truncate step size to 0; use true division.","For backward integration, transform the ODE instead of negating the step."],"tags":["math","numerical-analysis","ode","valueerror","parameter-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}