Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I determine the maximum range for perl's range iterator?

I can exceed perl's range iteration bounds like so, with or without -Mbigint:

$» perl -E 'say $^V; say for (0..shift)' 1e19
v5.16.2
Range iterator outside integer range at -e line 1.

How can I determine this upper limit, without simply trying until I exceed it?

like image 456
pilcrow Avatar asked Mar 25 '23 05:03

pilcrow


1 Answers

It's an IV.

>> similarly works on integers, so you can use

my $max_iv = -1 >> 1;
my $min_iv = -(-1 >> 1) - 1;

They can also be derived from the size of an IV.

my $max_iv = (1 << ($iv_bits-1)) - 1;
my $min_iv = -(1 << ($iv_bits-1));

The size of an IV can be obtained using

use Config qw( %Config );
my $iv_bits = 8 * $Config{ivsize};

or

my $iv_bits = 8 * length pack 'j', 0;
like image 190
ikegami Avatar answered Mar 26 '23 19:03

ikegami