On Dec 27, 2005, at 12:58, Alon Marx wrote:
Is there a constant or a definition for a maximum number in perl? And
the same question for a minimum value?
You have some constants available through the %Config hash in Config.pm:
% perl -MConfig -le 'print "$_ = $Config{$_}" for grep /size$/, keys %
Config'
charsize = 1
d_chsize =
doublesize = 8
fpossize = 8
gidsize = 4
i16size = 2
i32size = 4
i64size = 8
i8size = 1
intsize = 4
ivsize = 4
longdblsize = 16
longlongsize = 8
longsize = 4
...
So in this machine an int is 4 bytes (ivsize).
Nevertheless, Perl internally may switch to some floating-point type
if it needs to:
% perl -MDevel::Peek -wle '$a = 1 << 31; Dump($a); $a *= 2; Dump($a)'
SV = IV(0x180cac8) at 0x1800f4c
REFCNT = 1
FLAGS = (IOK,pIOK,IsUV)
UV = 2147483648
SV = PVNV(0x1803850) at 0x1800f4c
REFCNT = 1
FLAGS = (NOK,pNOK)
IV = -2147483648
NV = 4294967296
PV = 0
and in consequence the number may look like an integer but not be
accurate.
So this limit is well-defined in the sense of %Config, but at the
level of Perl code the actual "type" of an integer depends on the
implementation of the interpreter, and hence that limit has to be
used carefully. The pragma integer adds some control:
% perl -Minteger -MDevel::Peek -wle '$a = 1 << 31; Dump($a); $a *= 2;
Dump($a)'
SV = IV(0x180cad0) at 0x180061c
REFCNT = 1
FLAGS = (IOK,pIOK)
IV = -2147483648
SV = IV(0x180cad0) at 0x180061c
REFCNT = 1
FLAGS = (IOK,pIOK)
IV = 0
And, lastly, the pragma bigint removes the limit for integers in
practice.
-- fxn
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>