def decimal_comparison(fact1, fact2, cmp):
    """Rounds both numerical facts to the least accurate precision of both facts and calls the given cmp function with the rounded decimal values."""
    # When comparing two numeric fact values in a rule, the comparison needs to take into account different decimals. Numbers are compared based on the lowest decimal value rounded per XBRL specification. For example, the number 532,000,000 with decimals of -6 is considered to be equivalent to 532,300,000 with a decimals value of -5. In this case the 532,300,000 is rounded to a million and then compared to the value of 532,000,000. (Note that XBRL specifies "round half to nearest even" so 532,500,000 with decimals -6 rounds to 532,000,000, and 532,500,001 rounds to 533,000,000.)
    decimals = min(fact1.inferred_decimals, fact2.inferred_decimals)
    if decimals == float('inf'):
        return cmp(fact1.numeric_value, fact2.numeric_value)
    return cmp(fact1.round_numeric_value(decimals), fact2.round_numeric_value(decimals), decimals)