{"record":{"id":"d7b9febbee8f5e22","repo":"TheAlgorithms/Python","slug":"all-input-parameters-must-be-positive","errorCode":null,"errorMessage":"All input parameters must be positive","messagePattern":"All input parameters must be positive","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"physics/hubble_parameter.py","lineNumber":74,"sourceCode":"    >>> hubble_parameter(hubble_constant=68.3, radiation_density=1e-4,\n    ... matter_density=-0.3, dark_energy=0.7, redshift=1)\n    Traceback (most recent call last):\n    ...\n    ValueError: All input parameters must be positive\n\n    >>> hubble_parameter(hubble_constant=68.3, radiation_density=1e-4,\n    ... matter_density= 1.2, dark_energy=0.7, redshift=1)\n    Traceback (most recent call last):\n    ...\n    ValueError: Relative densities cannot be greater than one\n\n    >>> hubble_parameter(hubble_constant=68.3, radiation_density=1e-4,\n    ... matter_density= 0.3, dark_energy=0.7, redshift=0)\n    68.3\n    \"\"\"\n    parameters = [redshift, radiation_density, matter_density, dark_energy]\n    if any(p < 0 for p in parameters):\n        raise ValueError(\"All input parameters must be positive\")\n\n    if any(p > 1 for p in parameters[1:4]):\n        raise ValueError(\"Relative densities cannot be greater than one\")\n    else:\n        curvature = 1 - (matter_density + radiation_density + dark_energy)\n\n        e_2 = (\n            radiation_density * (redshift + 1) ** 4\n            + matter_density * (redshift + 1) ** 3\n            + curvature * (redshift + 1) ** 2\n            + dark_energy\n        )\n\n        hubble = hubble_constant * e_2 ** (1 / 2)\n        return hubble\n\n\nif __name__ == \"__main__\":","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/physics/hubble_parameter.py#L56-L92","documentation":"Raised by hubble_parameter() when any of redshift, radiation_density, matter_density, or dark_energy is negative. The function computes the Hubble parameter H(z) from density parameters that are physically non-negative fractions. Zero is allowed (the doctest shows redshift=0 returning hubble_constant unchanged).","triggerScenarios":"hubble_parameter(hubble_constant=68.3, radiation_density=-1e-4, matter_density=0.3, dark_energy=0.7, redshift=1), or a negative redshift such as redshift=-0.5 intended to model an earlier epoch.","commonSituations":"Using negative redshifts for past cosmological epochs (physically standard, rejected here); sign errors when converting between density conventions; data-entry mistakes in parameter tables.","solutions":["Clamp or validate all four parameters (redshift, radiation_density, matter_density, dark_energy) to >= 0 before the call.","If you need past-epoch (negative redshift) values, compute them outside this function or patch the guard, since the library intentionally forbids z < 0.","Catch ValueError and report which parameter was negative."],"exampleFix":"# before\nhubble_parameter(68.3, 1e-4, 0.3, 0.7, -0.5)  # ValueError\n\n# after\nhubble_parameter(68.3, 1e-4, 0.3, 0.7, 0.0)  # z >= 0 only","handlingStrategy":"validation","validationCode":"params = {'redshift': z, 'radiation_density': wr, 'matter_density': wm, 'dark_energy': wl}\nbad = [k for k, v in params.items() if v < 0]\nif bad:\n    raise ValueError(f'negative cosmology params: {bad}')\nhubble_parameter(hubble_constant, wr, wm, wl, z)","typeGuard":null,"tryCatchPattern":"try:\n    hubble_parameter(h0, wr, wm, wl, z)\nexcept ValueError as e:\n    # distinguish the two guards by message\n    print('cosmology input invalid:', e)","preventionTips":["Remember negative redshifts (past epochs) are intentionally unsupported.","Store density parameters as fractions in [0, 1] from the start.","Validate at the data-ingestion layer, not at the physics call site."],"tags":["physics","cosmology","input-validation","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}