{"record":{"id":"ec13d6dfa2f3288e","repo":"TheAlgorithms/Python","slug":"power-factor-must-be-a-valid-float-value-between","errorCode":null,"errorMessage":"power_factor must be a valid float value between -1 and 1.","messagePattern":"power_factor must be a valid float value between -1 and 1\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"electronics/real_and_reactive_power.py","lineNumber":21,"sourceCode":"\ndef real_power(apparent_power: float, power_factor: float) -> float:\n    \"\"\"\n    Calculate real power from apparent power and power factor.\n\n    Examples:\n    >>> real_power(100, 0.9)\n    90.0\n    >>> real_power(0, 0.8)\n    0.0\n    >>> real_power(100, -0.9)\n    -90.0\n    \"\"\"\n    if (\n        not isinstance(power_factor, (int, float))\n        or power_factor < -1\n        or power_factor > 1\n    ):\n        raise ValueError(\"power_factor must be a valid float value between -1 and 1.\")\n    return apparent_power * power_factor\n\n\ndef reactive_power(apparent_power: float, power_factor: float) -> float:\n    \"\"\"\n    Calculate reactive power from apparent power and power factor.\n\n    Examples:\n    >>> reactive_power(100, 0.9)\n    43.58898943540673\n    >>> reactive_power(0, 0.8)\n    0.0\n    >>> reactive_power(100, -0.9)\n    43.58898943540673\n    \"\"\"\n    if (\n        not isinstance(power_factor, (int, float))\n        or power_factor < -1","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/electronics/real_and_reactive_power.py#L3-L39","documentation":"Thrown by real_power() in electronics/real_and_reactive_power.py when power_factor is not an int/float or falls outside [-1, 1]. Real power P = S * pf is only defined for a cosine-like power factor in the closed range [-1, 1]; the isinstance check also means booleans pass (bool is an int subclass) but strings/None fail even if they look numeric.","triggerScenarios":"real_power(100, 1.5); real_power(100, '0.9') (string not isinstance of (int, float)); real_power(100, None); any pf where not isinstance(power_factor, (int,float)) or pf < -1 or pf > 1.","commonSituations":"Reading power factor from config/JSON as a string and passing it unconverted; forgetting that pf can be expressed as a percentage (90 instead of 0.9); calculations that drift slightly outside the range due to floating-point rounding (e.g. 1.0000000000000002).","solutions":["Pass power_factor as a float in [-1, 1] — e.g. real_power(100, 0.9) -> 90.0.","Convert numeric strings first: float(value); divide percentages by 100.","Clamp floating-point drift: max(-1.0, min(1.0, pf)) when the value is computed, not user-entered."],"exampleFix":"# before\nreal_power(100, '0.9')   # ValueError\nreal_power(100, 90)      # percentage passed raw -> ValueError\n\n# after\nreal_power(100, float('0.9'))          # 90.0\nreal_power(100, 90 / 100)              # 90.0","handlingStrategy":"type-guard","validationCode":"if not isinstance(power_factor, (int, float)) or not -1 <= power_factor <= 1:\n    raise ValueError('power_factor must be a number in [-1, 1]')","typeGuard":"def valid_power_factor(pf: object) -> bool:\n    return isinstance(pf, (int, float)) and not isinstance(pf, bool) and -1 <= pf <= 1","tryCatchPattern":"try:\n    p = real_power(s, pf)\nexcept ValueError as exc:\n    if 'power_factor' in str(exc):\n        pf = float(pf) if isinstance(pf, str) else max(-1.0, min(1.0, pf))\n        p = real_power(s, pf)\n    else:\n        raise","preventionTips":["Convert numeric strings with float() at the boundary.","Divide percentage pf by 100 before calling.","Clamp computed pf to [-1, 1] to absorb float drift."],"tags":["electronics","validation","power-factor","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}