So you want .0001 of the magnitude of some number. That wasn't specified and different than what you agreed to.
Well, you have to start by finding the magnitude of the number. The amount by which you'll multiply is based on the magnitude.
use strict;
use warnings;
sub magnitude {
my ($n, $p) = @_;
$p ||= 16; # Num of significant digits. 16 max for doubles
return 0 + ( sprintf('%.*e', $p-1, $n) =~ /e(.*)/ )[0];
}
sub movement {
my ($old, $new) = @_;
my $m = magnitude($old);
return 0 + sprintf '%.0f', ($new-$old) * 10**(4-$m);
}
printf("%+.0f\n", movement(@ARGV));
>perl movement.pl 1.0025 1.003
+5
>perl movement.pl 10.025 10.03
+5
You haven't defined what you want to happen when the number are of different magnitude.
>perl movement.pl 98000 99000
+1000
>perl movement.pl 99000 100000
+1000
>perl movement.pl 100000 99000
-100
>perl movement.pl 99000 98000
-1000
Another edge case is an input of zero. You haven't defined when you want to happen in that circumstance either.
|