################################################## # v2019.9.27 # Compares two large positive integers. # The integers can be binary, octal, # decimal, or hexadecimal. # # NOTE: Both numbers must be in the same base. # The numbers should not contain spaces, tabs, line breaks, # minus sign, decimal points, or anything other than digits! # Illegal characters can mess up the result. # # Returns: 0 if they are equal # 1 if the first one is greater # 2 if the second one is greater # # Special cases: # * When comparing zero against an empty string or # undefined value, the zero will be greater. # * When comparing an undefined value against # an empty string, they will be equal. # # Usage: INTEGER = CMP(STRING, STRING) # sub CMP { my $A = defined $_[0] ? uc($_[0]) : ''; my $B = defined $_[1] ? uc($_[1]) : ''; my $AL = length($A); my $BL = length($B); return 2 if ($AL < $BL); return 1 if ($AL > $BL); return 0 if ($A eq $B); # At this point, we know that both numbers have the # same length, and one of them is greater than the other. my $DIFF = 0; for (my $i = 0; $DIFF == 0 && $i < $AL; $i++) { $DIFF = vec($A, $i, 8) - vec($B, $i, 8); } return ($DIFF > 0) ? 1 : 2; }