Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

return +0

by arunvelusamy (Monk)
on Nov 27, 2007 at 07:54 UTC ( [id://653168]=perlquestion: print w/replies, xml ) Need Help??

arunvelusamy has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

   I came accross a code,

return +0 if $Result1 == NaN;

What do "+0" mean?

Thanks in advance...

Replies are listed 'Best First'.
Re: return +0
by narainhere (Monk) on Nov 27, 2007 at 09:09 UTC
    The Perldoc here says

    Unary "+" has no effect whatsoever, even on strings. It is useful syntactically for separating a function name from a parenthesized expression that would otherwise be interpreted as the complete list of function arguments.


    and to illustrate this I did this
    use warnings; use strict; my $return=&call(); print "Return value :$return"; sub innercall($) { print "I am called\n"; return 'gzip'; } sub call() { return +(innercall('test')||'') =~ /gzip/; }
    The call
    return +(innercall('test')||'') =~ /gzip/;
    and
    return (innercall('test')||'') =~ /gzip/;
    exhibit same behavior!Hope this helps!!

    PS:Just in case you are not aware, the line
    return +($r->header_in('Accept-Encoding')||'') =~ /gzip/;
    would return either a '1' or ' ' depending on the match success and failure respectively

    The world is so big for any individual to conquer

Re: return +0
by jbert (Priest) on Nov 27, 2007 at 11:17 UTC
    In other contexts (which the use of NaN (not-a-number) suggests), +0 is a number. If it is defined in IEEE754 floating point arithmetic. (There is a -0 too).

    So 10 / +0 is "positive infinity" and 10 / -0 is "negative infinity".

    See http://en.wikipedia.org/wiki/Division_by_zero#Division_by_zero_in_computer_arithmetic for more gory details.

    But - as others have noted - this probably does nothing in perl. (Although - can you overload unary plus? If so, someone might be doing something Too Clever.)

      There is a -0 too

      Signed zeroes exist in the mpfr library - and are therefore accessible to perl via Math::MPFR:
      C:\_32>perl -MMath::MPFR -e "$z=Math::MPFR->new(0);$z/=-1;print $z" -0
      They don't seem to be accessible via either Math::Pari or Math::BigFloat
      C:\_32>perl -MMath::Pari -e "$x=PARI(0);$x/=-1;print $x" 0 C:\_32>perl -MMath::BigFloat -e "$z=Math::BigFloat->new(0);$z/=-1;prin +t $z" 0
      can you overload unary plus?

      I don't think so. Even if you could, returning '+0' would not use the overloaded '+' subroutine - overloading works only with blessed objects (and 0 is not a blessed object :-)

      Cheers,
      Rob
Re: return +0
by diotalevi (Canon) on Nov 27, 2007 at 08:06 UTC

    Unary + does nothing. It says so right in the documentation.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

      Provided, i found below code in Apache-Compress-1.005/Compress.pm

      I belive '+' has some importance.

      if (!defined($how_decide) || lc($how_decide) eq 'header') { return +($r->header_in('Accept-Encoding')||'') =~ /gzip/; } elsif (lc($how_decide) eq 'user-agent') {

      updated: Removed the duplicate "found" word in my comment.

        In that example the + tells perl that the parens are not enclosing the arguments to a function call, so it is parsed correctly.
        I belive '+' has some importance.

        For me, return values of +0, -0, and 0 are indistinguishable from each other - which means that the '+' has no significance at all:
        C:\_32>perl -MDevel::Peek -e "Dump(+0)" SV = IV(0x1fe2d28) at 0x343cbc REFCNT = 1 FLAGS = (PADBUSY,PADTMP,IOK,READONLY,pIOK) IV = 0 C:\_32>perl -MDevel::Peek -e "Dump(-0)" SV = IV(0x432d2c) at 0xd73cec REFCNT = 1 FLAGS = (PADBUSY,PADTMP,IOK,READONLY,pIOK) IV = 0 C:\_32>perl -MDevel::Peek -e "Dump(0)" SV = IV(0x2492d28) at 0x23cbc REFCNT = 1 FLAGS = (PADBUSY,PADTMP,IOK,READONLY,pIOK) IV = 0
        (Replace the double quotes with single quotes if you're on a *nix-type operating system.)

        Cheers,
        Rob

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://653168]
Approved by andreas1234567
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-03-28 18:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found