Skip to content

Commit fc9c10c

Browse files
committed
fix: handle zero total weight in cog function with ValueError
1 parent 67fe25d commit fc9c10c

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

src/fuzzylogic/defuzz.py

+3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ def cog(target_weights: list[tuple[Set, float]]) -> float:
2020
• xᵢ is the corresponding value for the iᵗʰ element in the output domain.
2121
2222
"""
23+
2324
sum_weights = sum(weight for _, weight in target_weights)
25+
if sum_weights == 0:
26+
raise ValueError("Total weight is zero. Cannot compute center-of-gravity.")
2427
sum_weighted_cogs = sum(then_set.center_of_gravity() * weight for then_set, weight in target_weights)
2528
return sum_weighted_cogs / sum_weights
2629

0 commit comments

Comments
 (0)