{"record":{"id":"2a5cc4b82fd59ab5","repo":"TheAlgorithms/Python","slug":"vol-cube-only-accepts-non-negative-values","errorCode":null,"errorMessage":"vol_cube() only accepts non-negative values","messagePattern":"vol_cube\\(\\) only accepts non-negative values","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/volume.py","lineNumber":31,"sourceCode":"def vol_cube(side_length: float) -> float:\n    \"\"\"\n    Calculate the Volume of a Cube.\n\n    >>> vol_cube(1)\n    1.0\n    >>> vol_cube(3)\n    27.0\n    >>> vol_cube(0)\n    0.0\n    >>> vol_cube(1.6)\n    4.096000000000001\n    >>> vol_cube(-1)\n    Traceback (most recent call last):\n        ...\n    ValueError: vol_cube() only accepts non-negative values\n    \"\"\"\n    if side_length < 0:\n        raise ValueError(\"vol_cube() only accepts non-negative values\")\n    return pow(side_length, 3)\n\n\ndef vol_spherical_cap(height: float, radius: float) -> float:\n    \"\"\"\n    Calculate the volume of the spherical cap.\n\n    >>> vol_spherical_cap(1, 2)\n    5.235987755982988\n    >>> vol_spherical_cap(1.6, 2.6)\n    16.621119532592402\n    >>> vol_spherical_cap(0, 0)\n    0.0\n    >>> vol_spherical_cap(-1, 2)\n    Traceback (most recent call last):\n        ...\n    ValueError: vol_spherical_cap() only accepts non-negative values\n    >>> vol_spherical_cap(1, -2)","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/volume.py#L13-L49","documentation":"Raised by vol_cube(side_length) in maths/volume.py when side_length is negative. The cube volume is side_length**3, physically meaningless for negative lengths, so the guard rejects them; 0 is explicitly allowed (returns 0.0) and floats are accepted.","triggerScenarios":"Calling vol_cube(-1) with any negative value. There is no type check — strings will fail later with a TypeError from the comparison/pow, not with this message.","commonSituations":"Geometry inputs parsed from user data or measurements where a minus sign slipped in; symmetric-error propagation (e.g. delta = a - b) producing negative 'lengths'.","solutions":["Validate measurement sign at ingestion: lengths must be >= 0","If the value may be a magnitude error, take abs() only when that is physically justified — otherwise reject the input","Pre-check with your own guard: if side < 0: reject before calling"],"exampleFix":"// before\nv = vol_cube(-1)  # ValueError\n\n// after\nv = vol_cube(abs(-1)) if magnitude_only else vol_cube(1)","handlingStrategy":"validation","validationCode":"if side_length < 0:\n    raise ValueError(f'invalid cube side {side_length}')\nv = vol_cube(side_length)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate measurement signs at ingestion","Only abs() a negative length when the sign is known to be a data-entry slip","Zero is legal and returns 0.0"],"tags":["math","geometry","valueerror","input-validation","volume"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}