{"record":{"id":"1d89dd4e160909a1","repo":"TheAlgorithms/Python","slug":"invalid-value-x-for-fuzzy-set-self","errorCode":null,"errorMessage":"Invalid value {x} for fuzzy set {self}","messagePattern":"Invalid value (.+?) for fuzzy set (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"fuzzy_logic/fuzzy_operations.py","lineNumber":139,"sourceCode":"        >>> a.membership(0.1)\n        0.0\n        >>> a.membership(0.11)\n        0.09999999999999995\n        >>> a.membership(0.4)\n        0.0\n        >>> FuzzySet(\"A\", 0, 0.5, 1).membership(0.1)\n        0.2\n        >>> FuzzySet(\"B\", 0.2, 0.7, 1).membership(0.6)\n        0.8\n        \"\"\"\n        if x <= self.left_boundary or x >= self.right_boundary:\n            return 0.0\n        elif self.left_boundary < x <= self.peak:\n            return (x - self.left_boundary) / (self.peak - self.left_boundary)\n        elif self.peak < x < self.right_boundary:\n            return (self.right_boundary - x) / (self.right_boundary - self.peak)\n        msg = f\"Invalid value {x} for fuzzy set {self}\"\n        raise ValueError(msg)\n\n    def union(self, other) -> FuzzySet:\n        \"\"\"\n        Calculate the union of this fuzzy set with another fuzzy set.\n        Args:\n            other (FuzzySet): Another fuzzy set to union with.\n        Returns:\n            FuzzySet: A new fuzzy set representing the union.\n\n        >>> FuzzySet(\"a\", 0.1, 0.2, 0.3).union(FuzzySet(\"b\", 0.4, 0.5, 0.6))\n        FuzzySet(name='a U b', left_boundary=0.1, peak=0.6, right_boundary=0.35)\n        \"\"\"\n        return FuzzySet(\n            f\"{self.name} U {other.name}\",\n            min(self.left_boundary, other.left_boundary),\n            max(self.right_boundary, other.right_boundary),\n            (self.peak + other.peak) / 2,\n        )","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/fuzzy_logic/fuzzy_operations.py#L121-L157","documentation":"Raised by FuzzySet.membership() in fuzzy_logic/fuzzy_operations.py with the message 'Invalid value {x} for fuzzy set {self}'. The method returns 0.0 outside [left, right], a rising slope up to the peak, and a falling slope after — those branches cover every real number, so this raise is effectively dead code for ordinary inputs. The only realistic way to reach it is NaN, which fails all comparisons and falls through every branch.","triggerScenarios":"Calling membership(float('nan')) on any FuzzySet — NaN fails x <= left, x >= right, and both slope conditions, falling through to the raise. Any finite float returns 0.0 or a slope value instead.","commonSituations":"Feeding fuzzy controllers data from sensors or pipelines that produce NaN (missing readings, 0/0 features), numpy NaN leaking from array math, or un-sanitized user input parsed with float().","solutions":["Sanitize inputs: reject or replace NaN before calling membership().","Check upstream math for 0/0 or log of non-positives that generate NaN.","If maintaining this module, consider an explicit math.isnan check with a clearer error message."],"exampleFix":"# before\ndeg = FuzzySet('warm', 15, 22, 30).membership(sensor_temp)  # sensor_temp = nan\n\n# after\nimport math\nif math.isnan(sensor_temp):\n    degree = 0.0  # or raise a clear error\nelse:\n    degree = FuzzySet('warm', 15, 22, 30).membership(sensor_temp)","handlingStrategy":"validation","validationCode":"import math\nif math.isnan(x):\n    raise ValueError('NaN has no membership degree')\nFuzzySet('warm', 15, 22, 30).membership(x)","typeGuard":"def is_finite_number(v: object) -> bool:\n    return isinstance(v, (int, float)) and not isinstance(v, bool) and math.isfinite(v)","tryCatchPattern":"try:\n    deg = fs.membership(x)\nexcept ValueError as exc:\n    if 'Invalid value' in str(exc):\n        deg = 0.0  # out-of-domain / NaN treated as no membership\n    else:\n        raise","preventionTips":["Validate sensor inputs for NaN before fuzzy inference.","Know that this branch is unreachable for finite floats — hitting it is a NaN smell, not a range problem."],"tags":["fuzzy-logic","nan","dead-code","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}