Home: Perl Programming Help: Beginner:
Rounding Up!



BikerHQ
Deleted

Mar 3, 2001, 12:41 PM


Views: 1859
Rounding Up!

Quick and easy question. How can I round UP a variable that contains a non-integer number?

For instance, if x=2.134, I need to round it up to 3.

Thanks peeps,
BHQ.



Lawrence
Novice

Mar 3, 2001, 3:35 PM


Views: 1853
Re: Rounding Up!

It's a bit dodgy, but you could just try:

$variable = int ($variable) + 1;




BikerHQ
Deleted

Mar 3, 2001, 3:42 PM


Views: 1852
Re: Rounding Up!

Thanks. Unfortunately, I think the INT function only takes the integer from the fractional number. So some cases will result in rounding down!

Nice try tho!



BikerHQ
Deleted

Mar 3, 2001, 3:43 PM


Views: 1852
Re: Rounding Up!

Ummm.... just read that. I think the problem is forcing rounding up! Ahhh.. Whatever. Either way, it won't work quite the way it's meant to!



Jasmine
Administrator / Moderator

Mar 3, 2001, 4:19 PM


Views: 1852
Re: Rounding Up!

Sounds like the easiest way to do this is to compare the actual number to the integer value of the number.


Code
my @nums = qw/2 2.000 2.000000001 3.6 3 4.0/; 

# note: I used qw here so 2.000 didn't convert to 2 in this
# example

for my $num ( @nums ){

my $integer = int ( $num );

my $roundup = ( $integer != $num ) ? ++$integer : $integer ;

print " Original: $num
Rounded: $roundup \n \n";
}

This yields:


Code
 Original: 2 
Rounded: 2

Original: 2.000
Rounded: 2

Original: 2.000000001
Rounded: 3

Original: 3.6
Rounded: 4

Original: 3
Rounded: 3

Original: 4.0
Rounded: 4

Hope this helps!



Lawrence
Novice

Mar 3, 2001, 4:26 PM


Views: 1850
Re: Rounding Up!

Well the only problem with mine is that if you have an integer, it will round up to the next one. So use Jasmine's, or:

if (int $variable != $variable) {
$variable = int ($variable) + 1;
}




Jasmine
Administrator / Moderator

Mar 3, 2001, 4:55 PM


Views: 1850
Re: Rounding Up!

Actually, use this variation of Lawrence's -- it's faster than both of our attempts Smile


Code
$variable = int ($variable) + 1 if int $variable != $variable;

If you're curious about how to measure the speed of code snippets, here's what I used to test these.


Code
use Benchmark; 

$count = 500_000;

$variable = 2.000001;

timethese($count, {
'3int_1cond' => sub {
$variable = ( int $variable != $variable ) ? int $variable + 1 : int $variable;
# yeah, I worked on my attempt :)
},
'2int' => sub {
if (int $variable != $variable) {
$variable = int ($variable) + 1;
}
},
'2int_1line' => sub {
$variable = int ($variable) + 1 if int $variable != $variable;
},
});

Gave the following results:


Code
Benchmark: timing 500000 iterations of 2int, 2int_1line, 3int_1cond... 
2int: 2 wallclock secs ( 1.48 usr + 0.00 sys = 1.48 CPU) @ 338983.05/s (n=500000)
2int_1line: 0 wallclock secs ( 0.60 usr + 0.00 sys = 0.60 CPU) @ 833333.33/s (n=500000)
3int_1cond: 1 wallclock secs ( 1.44 usr + 0.00 sys = 1.44 CPU) @ 346740.64/s (n=500000)



BikerHQ
Deleted

Mar 4, 2001, 2:04 AM


Views: 1842
Re: Rounding Up!

Many thanks to you both. It's strange actually. Would have thought Perl would provide something for rounding up and down by default. It's got far more obscure things than that already!

Thanks again. I am truely a crappy Perl guy! (but I'm learning).



Jean
User


Mar 4, 2001, 8:29 AM


Views: 1839
Re: Rounding Up!

Actually, Perl has something Tongue.
Check out POSIX::ceil function :


Code
use POSIX; 
use strict;

my @nums = qw/2 2.000 2.000000001 3.6 3 4.0/;

for my $num ( @nums ) {
printf("Original: %s Rounded: %s \n", $num, ceil($num));
}

I've used Jasmine's example to show the results.

Jean Spector
QA Engineer @ Extent Technologies, Ltd.
mage@lycosmail.com


SirAnvil
Deleted

Mar 7, 2001, 1:21 PM


Views: 1829
Re: Rounding Up!

I'll second the ciel function. I use that and it works perfect.

SirAnvil
www.christiangamers.org