I have this code:
sub range {
my ($start, $end, $step) = @_;
if($step == 0) {
die("Step size cannot be 0!")
}
if($start > $end) {
($start, $end) = ($end, $start);
}
my @range = ();
for (my $i = $start; $i <= $end; $i += $step) {
push @range, $i;
}
return @range;
}
When running
my @range = range(-3, -2.7, 0.01);
I get this list:
...
$VAR23 = '-2.78';
$VAR24 = '-2.77';
$VAR25 = '-2.76000000000001';
$VAR26 = '-2.75000000000001';
$VAR27 = '-2.74000000000001';
$VAR28 = '-2.73000000000001';
$VAR29 = '-2.72000000000001';
$VAR30 = '-2.71000000000001';
$VAR31 = '-2.70000000000001';
Why does this happen?
I have perl v5.24.1 on a 4.9.0-7-amd64 #1 SMP Debian 4.9.107-1 machine. Adding the bignum module does not change the fact that the variables calculated are wrong.
Also, this does not happen when doing sometheng like "-2.7 - 0.01".