[comp.lang.perl] Arithmetic functions

tchrist@convex.com (Tom Christiansen) (02/23/91)

From the keyboard of tneff@bfmny0.BFM.COM (Tom Neff):
:>   min(LIST)	- numerically lowest of LIST
:>   max(LIST)	- numerically highest of LIST
:
:I would not restrict this to numeric context.
:
:	min("def","ghi","abc","jkl")
:
:might profitably return "abc".

What's wrong with:	


    $s  = (sort(("def","ghi","abc","jkl")))[0];
or
    ($s) = sort(("def","ghi","abc","jkl"));

and

    $n    = (sort bynum ((3,5,7,1,-4)))[0];
or
    ($n)  = sort bynum ((3,5,7,1,-4));


What's the motivation for making these and the others built-ins?
Efficient numeric operations in perl?  They're certainly pretty 
trivial to implement with using existing facilities.  I remember
once wanting an abs function, but I contented myself with:

    sub abs { ($_[0] < 0) ? -$_[0] : $_[0]; } 

I'm kind of dubious that the speedups you'd get by building these things
in would be enough to make complex numeric calculations in perl very much
more worthwhile than they are now (that is, not particularly).

--tom
-- 
"UNIX was not designed to stop you from doing stupid things, because
 that would also stop you from doing clever things." -- Doug Gwyn

 Tom Christiansen                tchrist@convex.com      convex!tchrist

sharon@jpl-devvax.JPL.NASA.GOV (Sharon Hopkins) (02/26/91)

In article <1991Feb23.001155.3630@convex.com> tchrist@convex.COM (Tom Christiansen) writes:
>...I remember once wanting an abs function, but I contented myself with:
>
>    sub abs { ($_[0] < 0) ? -$_[0] : $_[0]; } 
>

I guess I won't post my article asking for abs(EXPR) after all.  What is it
with this newsgroup, that all my questions get answered before I manage to
ask them?  :-)

Sharon Hopkins
sharon@jpl-devvax.Jpl.Nasa.Gov

rbj@uunet.UU.NET (Root Boy Jim) (02/26/91)

sharon@jpl-devvax.JPL.NASA.GOV (Sharon Hopkins) writes:
>I guess I won't post my article asking for abs(EXPR) after all.  What is it
>with this newsgroup, that all my questions get answered before I manage to
>ask them?  :-)

Considering where you are, why post questions at all?

Just walk down the hall!

-- 
		[rbj@uunet 1] stty sane
		unknown mode: sane

jv@mh.nl (Johan Vromans) (02/26/91)

In article <1991Feb23.001155.3630@convex.com> tchrist@convex.com (Tom Christiansen) writes:
>  What's wrong with:	
> 
>      $s  = (sort(("def","ghi","abc","jkl")))[0];

..etc..

Nothing wrong, except that you have to spell it out (or define them)
over and over and over...

And, of course:

>  Efficient numeric operations in perl?

Precisely.

I don't even think of using sort to get the max or min of two numbers.

	Johan
-- 
Johan Vromans				       jv@mh.nl via internet backbones
Multihouse Automatisering bv		       uucp: ..!{uunet,hp4nl}!mh.nl!jv
Doesburgweg 7, 2803 PL Gouda, The Netherlands  phone/fax: +31 1820 62911/62500
------------------------ "Arms are made for hugging" -------------------------

sharon@jpl-devvax.JPL.NASA.GOV (Sharon Hopkins) (02/27/91)

In article <124124@uunet.UU.NET> rbj@uunet.UU.NET (Root Boy Jim) writes:
>sharon@jpl-devvax.JPL.NASA.GOV (Sharon Hopkins) writes:
>>I guess I won't post my article asking for abs(EXPR) after all.  What is it
>>with this newsgroup, that all my questions get answered before I manage to
>>ask them?  :-)
>
>Considering where you are, why post questions at all?
>
>Just walk down the hall!

Larry's tired of getting pestered all the time while he's trying to
catch up on his e-mail...   :-)

I do have one question, though:  Is this a reasonable place to post
perl poetry?  We haven't got a comp.lang.perl.poems yet, or even a
rec.arts.poems.perl group, and I'm not sure rec.arts.poems is the place.

Sharon Hopkins
sharon@jpl-devvax.Jpl.Nasa.Gov

alanm@cognos.UUCP (Alan Myrvold) (02/28/91)

In article <1991Feb26.080757.21645@pronto.mh.nl> Johan Vromans <jv@mh.nl> 
writes:
>I don't even think of using sort to get the max or min of two numbers.

So what would be the preferred way of getting the min or max of a list of
numbers, short of adding it to Perl?  How about :

sub min { local ($v) = (shift @_); grep(($_ < $v) && ($v = $_),@_); $v; }
sub max { local ($v) = (shift @_); grep(($_ > $v) && ($v = $_),@_); $v; }


                                                              - Alan

---
Alan Myrvold          3755 Riverside Dr.     uunet!mitel!cunews!cognos!alanm
Cognos Incorporated   P.O. Box 9707          alanm%cognos.uucp@ccs.carleton.ca
(613) 738-1440 x5530  Ottawa, Ontario       
                      CANADA  K1G 3Z4       

maf@thor (Martin Foord) (02/28/91)

In article <11595@jpl-devvax.JPL.NASA.GOV> sharon@jpl-devvax.JPL.NASA.GOV (Sharon Hopkins) writes:
>Larry's tired of getting pestered all the time while he's trying to
>catch up on his e-mail...   :-)

It's a hard life when fame and fortune come your way ...
Maybe he shouldn't have cast his perl before us swine.

Martin.

alanm@cognos.UUCP (Alan Myrvold) (02/28/91)

In article <588@sunny.ucdavis.edu> poage@sunny.ucdavis.edu (Tom Poage) writes:
>Not too much too add ...
>
>	sub floor {
(floor and ceil deleted)

And of course :

# Round($x,$p) : returns $x rounded to $p decimal places 
# if $p is omitted, $p = 0 is used
# round to nearest, if halfway round to +infinity
# if $p < 0, the last $p digits of the result are 0
sub round { 
   local ($x,$p) = @_;
   local ($ten_p) = 10**$p;

   $x = .5 + $x*$ten_p;
   $x-- if $x < 0 && $x != int $x;
   $x = int($x) / $ten_p;
   ($p > 0) ? sprintf("%.15g",$x) : $x;
}

                                                        - Alan

---
Alan Myrvold          3755 Riverside Dr.     uunet!mitel!cunews!cognos!alanm
Cognos Incorporated   P.O. Box 9707          alanm%cognos.uucp@ccs.carleton.ca
(613) 738-1440 x5530  Ottawa, Ontario       
                      CANADA  K1G 3Z4       

sharon@jpl-devvax.JPL.NASA.GOV (Sharon Hopkins) (02/28/91)

In article <1991Feb27.221505.4520@dbsm.oz.au> maf@thor (Martin Foord) writes:
>In article <11595@jpl-devvax.JPL.NASA.GOV> sharon@jpl-devvax.JPL.NASA.GOV (Sharon Hopkins) writes:
>>Larry's tired of getting pestered all the time while he's trying to
>>catch up on his e-mail...   :-)
>
>It's a hard life when fame and fortune come your way ...
>Maybe he shouldn't have cast his perl before us swine.

Allow me to rephrase myself:  Larry's tired of getting pestered by *me* all
the time while he's trying to catch up on his e-mail, which usually contains
much more interesting questions.   (That, and getting perl 4.0 out...  :-)

Sharon Hopkins
sharon@jpl-devvax.Jpl.Nasa.Gov

rbj@uunet.UU.NET (Root Boy Jim) (02/28/91)

In article <1991Feb27.221505.4520@dbsm.oz.au> maf@thor (Martin Foord) writes:
>In article <11595@jpl-devvax.JPL.NASA.GOV> sharon@jpl-devvax.JPL.NASA.GOV (Sharon Hopkins) writes:
>>Larry's tired of getting pestered all the time while he's trying to
>>catch up on his e-mail...   :-)

Forgot to ask: how's that stir-fry program :-)

>It's a hard life when fame and fortune come your way ...
>Maybe he shouldn't have cast his perl before us swine.

Oh no. Not this thread again. Oh well, it gives me an excuse to say:

	Hey Larry, where'd you get those glasses?
-- 
		[rbj@uunet 1] stty sane
		unknown mode: sane

ronald@robobar.co.uk (Ronald S H Khoo) (03/01/91)

sharon@jpl-devvax.JPL.NASA.GOV (Sharon Hopkins) writes:

> I do have one question, though:  Is this a reasonable place to post
> perl poetry?  We haven't got a comp.lang.perl.poems yet, or even a
> rec.arts.poems.perl group, and I'm not sure rec.arts.poems is the place.

remember, this isn't *really* comp.lang.perl, as Dan B pointed out
a while back, this is the Perl User Group, where all perl groupies
can do their perlish things, whatever they may be.  So, yeah, please
do post your perlish poems here, only one thing I ask, please don't
forget the #!/usr/bin/perl, so that those of us who still read news
in rn can do |perl -x.  Thanks.

I don't think a subgroup just for poems is a good idea.  perl poems
are source code and should be archived here together with all the
rest of the source.  IMHO, anyway.
-- 
Ronald Khoo <ronald@robobar.co.uk> +44 81 991 1142 (O) +44 71 229 7741 (H)

allbery@NCoast.ORG (Brandon S. Allbery KB8JRR) (03/02/91)

As quoted from <11579@jpl-devvax.JPL.NASA.GOV> by sharon@jpl-devvax.JPL.NASA.GOV (Sharon Hopkins):
+---------------
| In article <1991Feb23.001155.3630@convex.com> tchrist@convex.COM (Tom Christiansen) writes:
| >...I remember once wanting an abs function, but I contented myself with:
| >    sub abs { ($_[0] < 0) ? -$_[0] : $_[0]; } 
| 
| I guess I won't post my article asking for abs(EXPR) after all.  What is it
| with this newsgroup, that all my questions get answered before I manage to
| ask them?  :-)
+---------------

There've been times I've wanted sin, cos, tan, and ln.  Some of them may even
be in there already (I can't remember the whole manual :-) since some of
Larry's friends at JPL may want them as well... possibly for the same reason.
(I was tempted to request a copy of "kybble" as well, since I have some video
bits to edit.)

++Brandon
-- 
Me: Brandon S. Allbery			    VHF/UHF: KB8JRR on 220, 2m, 440
Internet: allbery@NCoast.ORG		    Packet: KB8JRR @ WA8BXN
America OnLine: KB8JRR			    AMPR: KB8JRR.AmPR.ORG [44.70.4.88]
uunet!usenet.ins.cwru.edu!ncoast!allbery    Delphi: ALLBERY