from math import fabs, isfinite def product(seq, start=1.0): total = start s = [] # values that would overflow the total s_side = False # true if s_i increases the magnitude of the product for x in seq: old_total = total total *= x underflow = not total and old_total and x if isfinite(total) and not underflow: continue # fast-path when there is no overflow or underflow total = old_total side = fabs(x) > 1.0 if not s or side == s_side: s.append(x) s_side = side continue t = [x, total] while s and t: # opposite sides: matter and antimatter x = s.pop() * t.pop() side = fabs(x) > 1.0 s.append(x) if side == s_side else t.append(x) if t: s = t s_side = not s_side total = s.pop() for x in s: total *= x return total if __name__ == '__main__': from itertools import permutations data = (3.25, 1.25e-199, 18.25, 1.5e-201, 1.75e+200, 1.25e+202) print({product(t) for t in permutations(data)})