Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

(tye)Re2: BigInt usage

by tye (Sage)
on Feb 17, 2001 at 21:15 UTC ( [id://59126]=note: print w/replies, xml ) Need Help??


in reply to BigInt usage

Sorry, my previous reply was a bit of a quickie. I did some experiments and wanted to now more explicitly fill in some of the bits that you and I hinted at.

Your original code and my modified version here:

my @range= unpack "N*", pack "C*", split /[. ]+/, "@ARGV";
both work for any list of valid IP addresses in dotted-decimal notation, even those above 128.0.0.0.

The problem comes when you try to do:

$range[0]..$range[1]
with such large numbers. That causes Perl to squawk:
Range iterator outside integer range at ...
Which was news to me; thanks for teaching me something. (:

Your solution of using 0..$range[1]-$range[0] is quite good. You could also do:

for( $_= $range[0]; $_ <= $range[1]; $_++ )
Both should work for even 56-bit integers (I don't recall IPv6 address notation...). Though, you can't use the "N" format for pack()/unpack() for more than 32 bits, so you'd need to build up the number:
sub octets2num { my $val= 0; for( split /\./, shift(@_) ) { $val= 0x100*$val + $_; } return $val; } sub num2octects { my $val= shift(@_); my @bytes; while( 0 < $val ) { unshift @bytes, $val%0x100; $val= int( $val / 0x100 ); } return ! @bytes ? "0" : join ".", @bytes }
Note that using bit-wise operators (or use integer) above would break these. (I think using push instead of unshift and adding a reverse might be slightly more efficient.)

Update: I couldn't help golfing the first line of code a bit.

        - tye (just playing; that's how I usually learn)

Replies are listed 'Best First'.
Re: (tye) BigInt usage
by ryan (Pilgrim) on Feb 18, 2001 at 08:00 UTC
    Thanks, all is happy now. I left my first line 'ungolfed' as I actually have 7 other unrelated parameters on the command line after the two IPs.

    I thought I should answer my original question about BigInt initialisation that was rightfully dispelled.

    I was assuming Perl was as strongly typed as Java, and it is done a lot in Java to force data types to an outputable format from default output functions in some of the java system calls.

    Java would do something like this to force an integer to a string so it can pass it to initialise a BigInt: $bigint1.new("",$integer); In Perl there is no need, and the best thing is that BigInts can have ordinary arithmetic functions applied to them, most other BigInts I have seen in other languages have their own BigInt arithmetic functions which make life a mind bending hassle  $x=$bigint1.add($x,$y); for example.

    So..... my point?

    Numbers and strings are interchangeable (as written in many a Perl book) so you can just assign a numeric variable in the place of a string:

    use Math::BigInt; $std1 = 67*34; $bigint1 = Math::BigInt->new($std1); print "std: $std1\nbigint: $bigint1\n";
    Just wanted to contribute to the topic as we veered off it :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-24 03:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found