A
aclarke
Greetings!
I have been doing Perl for about 3 years now and have run across this
puzzling behavior in the code I wrote. Why doesn't the program print 0
as the value from the comparison? The debugger
shows the values as the same!
-----------------------------------------------------------------
use strict;
# A BigEpoch is a pair of numbers that are a restricted attempt to
# get larger fixed point arithmetic than is built into Perl. It
consists
# of a high part and a low part. Normally the high part is an
# integer and the low part is a fraction (the idea is that the high
part
# will represent epoch seconds and the low part milli or microseconds).
# An example value is [30.0, 0.12345], which represents the value
# 30.12345
# A BigEpoch can get sloppy due to calculations; it needs to be
normalized
# so that the high number is integral and the low is less than one (a
# fraction).
#
sub BigEpochNormalize
{
my ($bigepoch) = @_;
my $int0 = int($$bigepoch[0]);
my $int1 = int($$bigepoch[1]);
my $frac0 = $$bigepoch[0] - $int0;
my $frac1 = $$bigepoch[1] - $int1;
$int0 += $int1;
$frac0 += $frac1;
$int0 += int($frac0);
$frac0 -= int($frac0);
# need to handling borrowing in either direction
if ($int0 > 0 && $frac0 < 0)
{
$int0 -= 1;
$frac0 += 1;
}
elsif ($int0 < 0 && $frac0 > 0)
{
$int0 += 1;
$frac0 -= 1;
}
$$bigepoch[0] = $int0;
$$bigepoch[1] = $frac0;
return $bigepoch;
}
# This is a cmp function that works against BigEpochs
#
sub BigEpochCmp
{
my ($bigepoch1,$bigepoch2) = @_;
my $part1 = ($$bigepoch1[0] <=> $$bigepoch2[0]);
return $part1 if ($part1);
# high part is equals, so compare the low part
my $part2 = ($$bigepoch1[1] <=> $$bigepoch2[1]);
return $part2;
}
my $input = [ 1.1, 0.1 ];
my $output = BigEpochNormalize ($input);
my $expected = [ 1.0, 0.2 ];
print "The value below should be 0 (meaning equals)\n";
print "Cmp is ", BigEpochCmp($output, $expected), "\n";
I have been doing Perl for about 3 years now and have run across this
puzzling behavior in the code I wrote. Why doesn't the program print 0
as the value from the comparison? The debugger
shows the values as the same!
-----------------------------------------------------------------
use strict;
# A BigEpoch is a pair of numbers that are a restricted attempt to
# get larger fixed point arithmetic than is built into Perl. It
consists
# of a high part and a low part. Normally the high part is an
# integer and the low part is a fraction (the idea is that the high
part
# will represent epoch seconds and the low part milli or microseconds).
# An example value is [30.0, 0.12345], which represents the value
# 30.12345
# A BigEpoch can get sloppy due to calculations; it needs to be
normalized
# so that the high number is integral and the low is less than one (a
# fraction).
#
sub BigEpochNormalize
{
my ($bigepoch) = @_;
my $int0 = int($$bigepoch[0]);
my $int1 = int($$bigepoch[1]);
my $frac0 = $$bigepoch[0] - $int0;
my $frac1 = $$bigepoch[1] - $int1;
$int0 += $int1;
$frac0 += $frac1;
$int0 += int($frac0);
$frac0 -= int($frac0);
# need to handling borrowing in either direction
if ($int0 > 0 && $frac0 < 0)
{
$int0 -= 1;
$frac0 += 1;
}
elsif ($int0 < 0 && $frac0 > 0)
{
$int0 += 1;
$frac0 -= 1;
}
$$bigepoch[0] = $int0;
$$bigepoch[1] = $frac0;
return $bigepoch;
}
# This is a cmp function that works against BigEpochs
#
sub BigEpochCmp
{
my ($bigepoch1,$bigepoch2) = @_;
my $part1 = ($$bigepoch1[0] <=> $$bigepoch2[0]);
return $part1 if ($part1);
# high part is equals, so compare the low part
my $part2 = ($$bigepoch1[1] <=> $$bigepoch2[1]);
return $part2;
}
my $input = [ 1.1, 0.1 ];
my $output = BigEpochNormalize ($input);
my $expected = [ 1.0, 0.2 ];
print "The value below should be 0 (meaning equals)\n";
print "Cmp is ", BigEpochCmp($output, $expected), "\n";