Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

What is the distinction between IV and NV variables?

by Kc12349 (Monk)
on Sep 06, 2011 at 18:55 UTC ( #924447=perlquestion: print w/ replies, xml ) Need Help??
Kc12349 has asked for the wisdom of the Perl Monks concerning the following question:

Hello All,

Why is a NV and IV created in this case as opposed to just an IV? If instead $scalar2 is set to '0', only an IV is created.

Additionally, why is NV used to track numeric value for $scalar1 when it is modified and not IV? If $scalar2 is instead set to '0' initially, numeric value will be stored in IV.

Thanks for any enlightenment you can offer.

use Devel::Peek; my $scalar1 = ''; Dump($scalar1); my $temp1 = $scalar1+1; Dump($scalar1); $scalar1 = $scalar1+1; Dump($scalar1); SV = PV(0x36974) at 0x43ef74 REFCNT = 1 FLAGS = (PADMY,POK,pPOK) PV = 0x2b23ad4 ""\0 CUR = 0 LEN = 4 Argument "" isn't numeric in addition (+) at C:\Projects\test.pl line +17. SV = PVNV(0x2bbc254) at 0x43ef74 REFCNT = 1 FLAGS = (PADMY,POK,pIOK,pNOK,pPOK) IV = 0 NV = 0 PV = 0x2b23ad4 ""\0 CUR = 0 LEN = 4 SV = PVNV(0x2bbc254) at 0x43ef74 REFCNT = 1 FLAGS = (PADMY,NOK,pNOK) IV = 0 NV = 1 PV = 0x2b23ad4 ""\0 CUR = 0 LEN = 4 ### my $scalar2 = '0'; Dump($scalar2); my $temp2 = $scalar2+1; Dump($scalar2); $scalar2 = $scalar2+1; Dump($scalar2); SV = PV(0xa26974) at 0x2965d3c REFCNT = 1 FLAGS = (PADMY,POK,pPOK) PV = 0x2aa9cd4 "0"\0 CUR = 1 LEN = 4 SV = PVIV(0x43297c) at 0x2965d3c REFCNT = 1 FLAGS = (PADMY,IOK,POK,pIOK,pPOK) IV = 0 PV = 0x2aa9cd4 "0"\0 CUR = 1 LEN = 4 SV = PVIV(0x43297c) at 0x2965d3c REFCNT = 1 FLAGS = (PADMY,IOK,pIOK) IV = 1 PV = 0x2aa9cd4 "0"\0 CUR = 1 LEN = 4

Comment on What is the distinction between IV and NV variables?
Download Code
Re: What is the distinction between IV and NV variables?
by derby (Abbot) on Sep 06, 2011 at 20:45 UTC

    Perl tries to do the right thing and if it thinks the integer addition may cause an overflow, it will create the NV slots and use fp arithmetic. You can follow through the perl source (sv.c and/or pp_hot.c) but I believe what you're seeing is perl dropping the string '0' into the case of not causing an overflow and '' into the default possible overflow case.

    -derby
Re: What is the distinction between IV and NV variables?
by ikegami (Pope) on Sep 06, 2011 at 22:39 UTC

    Why is a NV and IV created in this case as opposed to just an IV?

    PVNV scalars always have a field for an IV/UV/RV. The PVNV in question doesn't have an IV, though. (IOK is false.)

    You actually want to know why a floating point is used (converting the PV scalar to a PVNV scalar) instead of an integer (which would convert the PV scalar to a PVIV scalar).

    Don't know.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2014-07-31 02:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (244 votes), past polls