Commits

Larry Hastings  committed 78de20b

Remove common factors from numerator and denominator before
dividing. (This makes it easier on the conversion to float.)

  • Participants
  • Parent commits 494195c

Comments (0)

Files changed (1)

 		if not remaining:
 			return 0
 		hand_size = len(self.other().cards) + int(bool(on_next_turn))
-		all_cards = len(self.hand.deck) + hand_size + 1  # +1 fixes off-by-ones for range
+		all_cards = len(self.hand.deck) + hand_size + 1  # the + 1 fixes off-by-ones for range
 		any_other_card = all_cards - remaining
-		numerators = functools.reduce(operator.mul, range(any_other_card - hand_size, any_other_card))
-		denominators = functools.reduce(operator.mul, range(all_cards - hand_size, all_cards))
-		return 1 - (numerators / denominators)
+		numerators = set(range(any_other_card - hand_size, any_other_card))
+		denominators = set(range(all_cards - hand_size, all_cards))
+		common = numerators & denominators
+		if common:
+			numerators -= common
+			denominators -= common
+		numerator = functools.reduce(operator.mul, numerators)
+		denominator = functools.reduce(operator.mul, denominators)
+		return 1.0 - (numerator / denominator)
 
 
 class Game: