Here is a Perl ceil/floor equivalent. The first code section
seems to correctly implement floor by taking into account that
int() does not use the sign in its process of rounding. The
second code section with floor, although intuitive is not correct.
For a full proof, all the values between 2.0 - 2.9 should be checked.
sln
This is amended code and a full proof using standard C ceil/floor
as a comparison. This output is the perl code equavelent.
The previous versions is erroneous and can't use +- .5 as int()
rounds to the nearest whole number.
As it is now +- 1.0 is used as well as taking boundry conditions
into consideration.
The _ceil()/_floor() are eqavelent now. An effort was made towards
speedy code if thats possible.
Thanks Peter J. Holzer for pointing out the error.
The good thing about this code is it works the same way every time.
Now, back to the show...
sln
==================================================
# ===========================================
# A Perl based Ceil/floor equavelent
# This floor takes the sign into acount
# when rounding.
# - - - - - - - -
use strict;
use warnings;
## test ceil-floor around 0 and between (+-) 2.0 - 3.0
## in .1 increments
my @Test = qw(
0.0 0.1 0.6
2.0 2.1 2.3 2.4 2.5
2.6 2.7 2.8 2.9 3.0
);
my ($y,$z);
for $y (@Test)
{
$z = _ceil( $y );
printf( "The ceil of %s is %f\n", $y, $z );
$z = _ceil( -$y );
printf( "The ceil of -%s is %f\n\n", $y, $z );
}
for $y (@Test)
{
$z = _floor( $y );
printf( "The floor of %s is %f\n", $y, $z );
$z = _floor( -$y );
printf( "The floor of -%s is %f\n\n", $y, $z );
}
sub _ceil {
my $z = int($_[0]);
return $z if ($_[0] == $z || $_[0]<0);
return int($_[0]+1.0);
}
sub _floor {
my $z = int($_[0]);
return $z if ($_[0] == $z || $_[0]>=0);
return int($_[0]-1.0);
}
__END__
The ceil of 0.0 is 0.000000
The ceil of -0.0 is 0.000000
The ceil of 0.1 is 1.000000
The ceil of -0.1 is 0.000000
The ceil of 0.6 is 1.000000
The ceil of -0.6 is 0.000000
The ceil of 2.0 is 2.000000
The ceil of -2.0 is -2.000000
The ceil of 2.1 is 3.000000
The ceil of -2.1 is -2.000000
The ceil of 2.3 is 3.000000
The ceil of -2.3 is -2.000000
The ceil of 2.4 is 3.000000
The ceil of -2.4 is -2.000000
The ceil of 2.5 is 3.000000
The ceil of -2.5 is -2.000000
The ceil of 2.6 is 3.000000
The ceil of -2.6 is -2.000000
The ceil of 2.7 is 3.000000
The ceil of -2.7 is -2.000000
The ceil of 2.8 is 3.000000
The ceil of -2.8 is -2.000000
The ceil of 2.9 is 3.000000
The ceil of -2.9 is -2.000000
The ceil of 3.0 is 3.000000
The ceil of -3.0 is -3.000000
The floor of 0.0 is 0.000000
The floor of -0.0 is 0.000000
The floor of 0.1 is 0.000000
The floor of -0.1 is -1.000000
The floor of 0.6 is 0.000000
The floor of -0.6 is -1.000000
The floor of 2.0 is 2.000000
The floor of -2.0 is -2.000000
The floor of 2.1 is 2.000000
The floor of -2.1 is -3.000000
The floor of 2.3 is 2.000000
The floor of -2.3 is -3.000000
The floor of 2.4 is 2.000000
The floor of -2.4 is -3.000000
The floor of 2.5 is 2.000000
The floor of -2.5 is -3.000000
The floor of 2.6 is 2.000000
The floor of -2.6 is -3.000000
The floor of 2.7 is 2.000000
The floor of -2.7 is -3.000000
The floor of 2.8 is 2.000000
The floor of -2.8 is -3.000000
The floor of 2.9 is 2.000000
The floor of -2.9 is -3.000000
The floor of 3.0 is 3.000000
The floor of -3.0 is -3.000000