Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Rounding up in perl (http://www.velocityreviews.com/forums/t909231-rounding-up-in-perl.html)

David Groff 12-17-2008 03:37 PM

Rounding up in perl
 


Is there more than one function for rounding a
number up in perl?
I tried the ceil() function but get an undefined subroutine
message.

Jürgen Exner 12-17-2008 04:18 PM

Re: Rounding up in perl
 
David Groff <david.groff@noaa.gov> wrote:
>
>
>Is there more than one function for rounding a
>number up in perl?
>I tried the ceil() function but get an undefined subroutine
>message.


See
perldoc -q round
"Does Perl have a round() function? What about ceil() and floor()?"

jue

Peter J. Holzer 12-17-2008 04:42 PM

Re: Rounding up in perl
 
On 2008-12-17 15:37, David Groff <david.groff@noaa.gov> wrote:
> Is there more than one function for rounding a
> number up in perl?


For rounding - yes. For rounding up - no.

> I tried the ceil() function but get an undefined subroutine
> message.


You forgot "use POSIX"?

hp

Marshall Dudley 12-17-2008 05:57 PM

Re: Rounding up in perl
 
David Groff wrote:
>
>
> Is there more than one function for rounding a
> number up in perl?
> I tried the ceil() function but get an undefined subroutine
> message.

Simply add 0.5 to the number and round, or convert to an integer then
add 1 or add 1 then convert to an integer.

Marshall

Jürgen Exner 12-17-2008 07:52 PM

Re: Rounding up in perl
 
Marshall Dudley <mdudley@king-cart.com> wrote:
>David Groff wrote:
>> I tried the ceil() function but get an undefined subroutine
>> message.

>[...] convert to an integer then
>add 1 or add 1 then convert to an integer.


Bad idea if the number is a natural number already.

jue

Scott Bryce 12-17-2008 08:08 PM

Re: Rounding up in perl
 
Marshall Dudley wrote:
> David Groff wrote:
>>
>>
>> Is there more than one function for rounding a
>> number up in perl?
>> I tried the ceil() function but get an undefined subroutine
>> message.

> Simply add 0.5 to the number and round, or convert to an integer then
> add 1 or add 1 then convert to an integer.



Neither of these methods is bullet proof. Accuracy of the results will
depend on the number and/or the method used to do the rounding.




use strict;
use warnings;

while (my $i = <DATA>)
{
chomp $i;
my $j = $i + 1;
my $k = $i + 0.5;
print "$i rounds to ", sprintf('%d', $j), ' or ', sprintf('%.0f', $k),
"\n";
}


__DATA__
2
3


----

2 rounds to 3 or 2
3 rounds to 4 or 4

sln@netherlands.com 12-18-2008 09:28 PM

Re: Rounding up in perl
 
On Wed, 17 Dec 2008 10:37:22 -0500, David Groff <david.groff@noaa.gov> wrote:

>
>
>Is there more than one function for rounding a
>number up in perl?
>I tried the ceil() function but get an undefined subroutine
>message.


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

# ===========================================
# Correct ceil/floor equavelent
# This floor takes the sign into acount
# when rounding.
# - - - - - - - -
use strict;
use warnings;

printf( "The floor of 2.8 is %f\n", _floor( 2.8 ) );
printf( "The floor of -2.8 is %f\n", _floor( -2.8 ) );

printf( "The ceil of 2.8 is %f\n", _ceil( 2.8 ) );
printf( "The ceil of -2.8 is %f\n", _ceil( -2.8 ) );

sub _ceil {return int(shift()+.5)}

sub _floor {my $y = shift(); return int(($y < 0) ? $y-.5 : $y)}

__END__

The floor of 2.8 is 2.000000
The floor of -2.8 is -3.000000
The ceil of 2.8 is 3.000000
The ceil of -2.8 is -2.000000


# ===========================================
# Correct ceil equavelent, incorrect floor
# Int() rounds down the Absolute value.
# So do not use this floor.
# - - - - - - - -
use strict;
use warnings;

printf( "The floor of 2.8 is %f\n", _floor( 2.8 ) );
printf( "The floor of -2.8 is %f\n", _floor( -2.8 ) );

printf( "The ceil of 2.8 is %f\n", _ceil( 2.8 ) );
printf( "The ceil of -2.8 is %f\n", _ceil( -2.8 ) );

sub _ceil {return int(shift()+.5)}

sub _floor {return int(shift())}

__END__

The floor of 2.8 is 2.000000
The floor of -2.8 is -2.000000
The ceil of 2.8 is 3.000000
The ceil of -2.8 is -2.000000


==========================================
C - reference code

// crt_floor.c
// This example displays the largest integers
// less than or equal to the floating-point values 2.8
// and -2.8. It then shows the smallest integers greater
// than or equal to 2.8 and -2.8.


#include <math.h>
#include <stdio.h>

int main( void )
{
double y;

y = floor( 2.8 );
printf( "The floor of 2.8 is %f\n", y );
y = floor( -2.8 );
printf( "The floor of -2.8 is %f\n", y );

y = ceil( 2.8 );
printf( "The ceil of 2.8 is %f\n", y );
y = ceil( -2.8 );
printf( "The ceil of -2.8 is %f\n", y );
}


Output

The floor of 2.8 is 2.000000
The floor of -2.8 is -3.000000
The ceil of 2.8 is 3.000000
The ceil of -2.8 is -2.000000



Tad J McClellan 12-19-2008 12:52 AM

Re: Rounding up in perl
 
sln@netherlands.com <sln@netherlands.com> wrote:

> taking into account that
> int() does not use the sign in its process of rounding.



That is because int() does not *have* a process of rounding.

int() does not do rounding.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

sln@netherlands.com 12-19-2008 03:03 AM

Re: Rounding up in perl
 
On Thu, 18 Dec 2008 18:52:30 -0600, Tad J McClellan <tadmc@seesig.invalid> wrote:

>sln@netherlands.com <sln@netherlands.com> wrote:
>
>> taking into account that
>> int() does not use the sign in its process of rounding.

>
>
>That is because int() does not *have* a process of rounding.
>
>int() does not do rounding.


Not only does int() round, but there is no way it can return
without rounding.

sln


perlfunc.html
===============================
Numeric functions

...., int, ...

int EXPR

int

Returns the integer portion of EXPR. If EXPR is omitted,
uses $_. You should not use this function for rounding:
one because it truncates towards 0, and two because machine
representations of floating point numbers can sometimes
produce counterintuitive results.



Uri Guttman 12-19-2008 03:09 AM

Re: Rounding up in perl
 
>>>>> "s" == sln <sln@netherlands.com> writes:

s> On Thu, 18 Dec 2008 18:52:30 -0600, Tad J McClellan <tadmc@seesig.invalid> wrote:

>> That is because int() does not *have* a process of rounding.
>> int() does not do rounding.


s> Not only does int() round, but there is no way it can return
s> without rounding.

and you determined that from what text in the docs you quote?


s> perlfunc.html
s> ===============================
s> Numeric functions

s> ..., int, ...

s> int EXPR

s> int

s> Returns the integer portion of EXPR. If EXPR is omitted,
s> uses $_. You should not use this function for rounding:
s> one because it truncates towards 0, and two because machine
s> representations of floating point numbers can sometimes
s> produce counterintuitive results.

and where does it say int() does rounding? it even says you should NOT
use it for rounding. truncation (which is what int() does) is not
rounding in any sense of the term rounding. your saying otherwise will
not make it so.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------


All times are GMT. The time now is 10:47 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.