Syntactic Confectionery Delight 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
CUR = 0
LEN = 4
Argument "" isn't numeric in addition (+) at C:\Projects\test.pl line
+17.
SV = PVNV(0x2bbc254) at 0x43ef74
REFCNT = 1
IV = 0
NV = 0
CUR = 0
LEN = 4
SV = PVNV(0x2bbc254) at 0x43ef74
REFCNT = 1
IV = 0
NV = 1
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
PV = 0x2aa9cd4 "0"\0
CUR = 1
LEN = 4
SV = PVIV(0x43297c) at 0x2965d3c
REFCNT = 1
IV = 0
PV = 0x2aa9cd4 "0"\0
CUR = 1
LEN = 4
SV = PVIV(0x43297c) at 0x2965d3c
REFCNT = 1
IV = 1
PV = 0x2aa9cd4 "0"\0
CUR = 1
LEN = 4

Comment on What is the distinction between IV and NV variables?
Replies are listed 'Best First'.
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.

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 drinking their drinks and smoking their pipes about the Monastery: (11)
As of 2015-08-03 12:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?