Sadly this will fail for any movement of greater than 9 points. This problem is insoluble without a definition of what constitutes a point. Consider the upper number pair and some possible variations that could exist with trailing zero truncation:
1.5553 - 1.5552 # 1 point
1.5553 - 1.55 # 53 points (your sub will fail with this)
1.56 - 1.55 # 1 point? 100 points? (insoluble from data alone)
You either have to decide what is a point, or guess and accept that there will be errors:
sub points {
my ($was,$is,$exp) = @_;
unless ($exp) {
$was =~ m/\.(\d+)$/;
my $x = $1 ? length($1) : 0;
$is =~ m/\.(\d+)$/;
my $y = $1 ? length($1) : 0;
$exp = $x > $y ? $x : $y;
}
print "($exp) $was => $is\t";
$exp = 10**$exp;
my $dif = $is*$exp - $was*$exp;
# add correction factor to allow int to round correctly
# also ensures that FP "error" such as 4.999999 ends up as +5
$dif += $dif < 0 ? -0.5 : +0.5;
return sprintf "%+d", $dif;
}
print points(1.5553,1.5552), $/;
print points(1.55,1.5553 ), $/;
print points(1.55,1.56), $/;
print points(1.55,1.56,4), $/;
print points(0.9984,0.998), $/;
__DATA__
(4) 1.5553 => 1.5552 -1
(4) 1.55 => 1.5553 +53
(2) 1.55 => 1.56 +1
(4) 1.55 => 1.56 +100
(4) 0.9984 => 0.998 -4