For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2007 > Rounding and Subs









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Rounding and Subs
Joseph L. Casale

2007-07-24, 6:59 pm

Hi All,
I have two questions, is it correct to execute subs like this:
Sub_1 (sub_2))? I am trying to pass the return value of sub_2 into sub_1.

Also, I have been looking at perldoc etc to limit the # of digits returned after a decimal, it looks difficult? Am I overlooking a simple method?

Thanks!
jlc



Chas Owens

2007-07-24, 6:59 pm

On 7/24/07, Joseph L. Casale <JCasale@activenetwerx.com> wrote:
> Hi All,
> I have two questions, is it correct to execute subs like this:
> Sub_1 (sub_2))? I am trying to pass the return value of sub_2 into sub_1.
>
> Also, I have been looking at perldoc etc to limit the # of digits returned
> after a decimal, it looks difficult? Am I overlooking a simple method?


Yes, a subroutine call is an expression, so it can be used as an
argument to another subroutine. There are several ways to round a
number, the easiest (especially if you don't care how it is rounded)
is to use sprintf or printf:

perl -e 'printf "%.2f\n", 100.12345'

You can find out more about the formating specifiers in perldoc -f sprintf
Rob Dixon

2007-07-24, 6:59 pm

Chas Owens wrote:
>
> On 7/24/07, Joseph L. Casale <JCasale@activenetwerx.com> wrote:
>
> Yes, a subroutine call is an expression, so it can be used as an
> argument to another subroutine. There are several ways to round a
> number, the easiest (especially if you don't care how it is rounded)
> is to use sprintf or printf:
>
> perl -e 'printf "%.2f\n", 100.12345'
>
> You can find out more about the formating specifiers in perldoc -f
> sprintf


If you really don't care how a number is rounded, then just replace
it by zero.

Rob


Paul Lalli

2007-07-24, 6:59 pm

On Jul 24, 12:18 pm, JCas...@ActiveNetwerx.com (Joseph L. Casale)
wrote:
> I have two questions, is it correct to execute subs like this:
> Sub_1 (sub_2))? I am trying to pass the return value of sub_2 into sub_1.


Sure. Just be aware that when calling the "inner" subroutine like
that, you're calling it in list context. More than likely, that won't
make a whole heck of a lot of difference. But if you're dealing with
a subroutine that does something different in list vs scalar context,
you need to be aware of what you're doing.

In other words:
sub_1(sub2());
is equivalent to:
my @values =3D sub2();
sub1(@values);
rather than
my $value =3D sub2();
sub1($value);

For example, let's say you were trying to pass your subroutine a
reversed string. A na=EFve approach would be:
sub1(reverse("foobar"));

You might be very surprised to learn that sub1() is being passed the
string "foobar", not "raboof", because reverse() is being called in a
list context, and so therefore reversed the one-element list
("foobar"), rather than the string "foobar".

> Also, I have been looking at perldoc etc to limit the # of digits returne=

d after a
> decimal, it looks difficult? Am I overlooking a simple method?


Yes, and you're not reading perldoc too well. :-P

$ perldoc -q round
Found in /opt2/Perl5_8_4/lib/perl5/5.8.4/pod/perlfaq4.pod
Does Perl have a round() function? What about ceil() and
floor()? Trig functions?

Remember that int() merely truncates toward 0. For rounding
to a certain number of digits, sprintf() or printf() is
usually the easiest route.

printf("%.3f", 3.1415926535); # prints 3.142

Paul Lalli

Chas Owens

2007-07-24, 6:59 pm

On 7/24/07, Rob Dixon <rob.dixon@350.com> wrote:
> Chas Owens wrote:

snip
snip[color=darkred]
> If you really don't care how a number is rounded, then just replace
> it by zero.

snip

By "care how" I mean Banker's Rounding vs Normal Rounding.
Chas Owens

2007-07-24, 6:59 pm

On 7/24/07, Chas Owens <chas.owens@gmail.com> wrote:
> On 7/24/07, Rob Dixon <rob.dixon@350.com> wrote:
> snip
> snip
> snip
>
> By "care how" I mean Banker's Rounding vs Normal Rounding.
>


BTW, these are only the two I use most often, there are bunches of
rounding algorithms that have different outputs. For a non-exhaustive
list see http://support.microsoft.com/kb/196652. Which one you use
depends heavily on what exactly you are trying to achieve with the
rounding; however, in most cases you don't care that much and just use
whatever algorithm sprintf implements on your system.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com