CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Rounding Up!

 



BikerHQ
Deleted

Mar 3, 2001, 12:41 PM

Post #1 of 10 (1557 views)
Rounding Up! Can't Post

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

Post #2 of 10 (1551 views)
Re: Rounding Up! [In reply to] Can't Post

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

$variable = int ($variable) + 1;




BikerHQ
Deleted

Mar 3, 2001, 3:42 PM

Post #3 of 10 (1550 views)
Re: Rounding Up! [In reply to] Can't Post

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

Post #4 of 10 (1550 views)
Re: Rounding Up! [In reply to] Can't Post

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

Post #5 of 10 (1550 views)
Re: Rounding Up! [In reply to] Can't Post

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

Post #6 of 10 (1548 views)
Re: Rounding Up! [In reply to] Can't Post

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

Post #7 of 10 (1548 views)
Re: Rounding Up! [In reply to] Can't Post

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

Post #8 of 10 (1540 views)
Re: Rounding Up! [In reply to] Can't Post

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

Post #9 of 10 (1537 views)
Re: Rounding Up! [In reply to] Can't Post

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

Post #10 of 10 (1527 views)
Re: Rounding Up! [In reply to] Can't Post

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

SirAnvil
www.christiangamers.org

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives