Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Perl (http://www.go4expert.com/articles/perl-tutorials/)
-   -   Ceil/Floor/Round in Perl (http://www.go4expert.com/articles/ceil-floor-round-perl-t19166/)

pradeep 25Aug2009 16:37

Ceil/Floor/Round in Perl
 

Ceil in Perl



Cieling is basically outputing the next largest integer for an given real number.
Code:

ceiling(x) is the smallest integer not less than x
Ways to do it in Perl, a) using our own logic

Code: Perl

my $num = 45.4;
my $ceil = int($num + 0.99);


b) using POSIX

Code: Perl

use POSIX qw/ceil/;

my $num = 45.4;
my $ceil = ceil($num);


Floor in Perl



Flooring is outputing the next smallest integer for an given real number.
Code:

floor(x) is the largest integer not greater than x
Ways to do it in Perl, a) using our own logic

Code: Perl

my $num = 45.4;
my $floor = int($num);


b) using POSIX

Code: Perl

use POSIX qw/floor/;

my $num = 45.4;
my $floor = floor($num);


Round in perl



Rounding is converting any floating-point number to the nearest integer.

Ways to do it in Perl, a) using our own logic

Code: Perl

my $num = 45.4;
my $round = int($num + 0.5);


b) using Math::Round

Code: Perl

use Math::Round qw/round/;

my $num = 45.4;
my $round = round($num);


It'll be really nice if everyone crontributes to this thread with the equivalent functions in the language they are comfortable with.

asha 26Aug2009 10:47

Re: Ceil/Floor/Round in Perl
 
Really nice, but the methods using modules is much more reliable I guess.

shabbir 2Sep2009 18:55

Re: Ceil/Floor/Round in Perl
 
Nomination for Article of the month - Aug 2009 Started.

sorin25 3Jan2011 17:02

Re: Ceil/Floor/Round in Perl
 
God dam, how the fuck are you morons still alive ?

PHP Code:

use POSIX qw/ceil/;
sub idiot_ceil { return int($_[0] + 0.99) }

for 
$i qw/0.0005 -1.4 4.00005/) {
     
local $,="\t"local $\="\n";
     print 
$iidiot_ceil($i), ceil($i), (idiot_ceil($i) != ceil($i)) ? " yet again, you are a moron" ".. you got this right genius";


If you morons have trouble running this, this is the output:
Code:

0.0005    0    1    yet again, you are a moron
-1.4    0    -1    yet again, you are a moron
4.00005    4    5    yet again, you are a moron

Fuck, and this is a top Google result. Stop publishing this crap before you infect the rest of the planet with your idiocy!

stardakev 9Jan2015 07:09

Re: Ceil/Floor/Round in Perl
 
When I don't want the overhead of the full Posix module to implement only the Ceil function, I use this implementation.

Usage $Result = &Ceil ($Original_Number, $Ceil_Value);

$Ceil_Value can be any positive, non zero integer.

Examples
If rounding up to the nearest dollar $Ceil_Value = 1

If rounding up to the nearest 5 cents $Ceil_Value = 0.05

Note, there is no error trapping.

Code:


sub Ceil {

        my $x = shift;

        my $r = shift;

        my $Len = &decLen($x);

        my $rLen = &decLen($r);

        $Len = $rLen if $rLen > $Len;

        $x = $x * 10**$Len;

        $r = $r * 10**$Len;

        my $ceil = ($x + ((-$x) % $r)) / 10**$Len;

        return $ceil;
}

sub decLen {    #returns how many digits after the decimal point

        my ($a, $c) = ($_[0] =~ /(\d*).(\d*)/);

        return length $c
}


stardakev 9Jan2015 07:18

Re: Ceil/Floor/Round in Perl
 
My bad

Quote:

$Ceil_Value can be any positive, non zero integer.
I meant $Ceil_Value can be any positive, non zero value.


All times are GMT +5.5. The time now is 03:27.