Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: comparison of packed signed integers

by BrowserUk (Patriarch)
on Oct 30, 2011 at 11:24 UTC ( [id://934706]=note: print w/replies, xml ) Need Help??


in reply to comparison of packed signed integers

Updated: To reflect the reality check from johngg below.

Comparison of packed integer will only work if they are unsigned (or all positive or all negative) and packed in big-endian (Network) order:

$belo = pack 'N', 10000; $behi = pack 'N', 20000; $ben = pack 'N', 123 +45;; $belo lt $ben && $behi gt $ben and print 'Works';; Works

Not if they are packed in little-endian (VAX) order:

$lelo = pack 'V', 10000; $lehi = pack 'V', 20000; $len = pack 'V', 123 +45;; $lelo lt $len && $lehi gt $len and print 'Works';;

The same thing for packed signed integers (but only works if the numbers are all positive or all negative):

$bello = pack 'l>', 10000; $belhi = pack 'l>', 20000; $beln = pack 'l> +', 12345;; $bello lt $beln && $belhi gt $beln and print 'Works';; Works $lello = pack 'l<', 10000; $lelhi = pack 'l<', 20000; $leln = pack 'l< +', 12345;; $lello lt $leln && $lelhi gt $leln and print 'Works';;

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: comparison of packed signed integers
by johngg (Canon) on Oct 30, 2011 at 11:36 UTC

    I'm not sure that will work if negative numbers come into it.

    knoppix@Microknoppix:~$ perl -E ' > $pos = pack q{N}, 10000; > say unpack q{B*}, $pos; > $neg = pack q{N}, -10000; > say unpack q{B*}, $neg; > say $pos gt $neg ? q{Good} : q{Bad};' 00000000000000000010011100010000 11111111111111111101100011110000 Bad knoppix@Microknoppix:~$

    You'd probably have to expand the tests to cater for that.

    Cheers,

    JohnGG

      pack template 'N' is for unsigned integers. If you need to cater for signed integers, you need template 'l>'.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Yes, but the same holds true using 'l>'.

        knoppix@Microknoppix:~$ perl -E ' > $pos = pack q{l>}, 10000; > say unpack q{B*}, $pos; > $neg = pack q{l>}, -10000; > say unpack q{B*}, $neg; > say $pos gt $neg ? q{Good} : q{Bad};' 00000000000000000010011100010000 11111111111111111101100011110000 Bad knoppix@Microknoppix:~$

        I think that packing with 'N' or 'l>' will always produce the same results, it is unpacking where it makes a difference.

        knoppix@Microknoppix:~$ perl -E ' > for ( -2147483648, -1, 0, 1, 2147483647, 4294967295 ) > { > say qq{$_:}; > say unpack q{B*}, pack q{N}, $_; > say unpack q{B*}, pack q{l>}, $_; > say q{-} x 32; > }; > ' -2147483648: 10000000000000000000000000000000 10000000000000000000000000000000 -------------------------------- -1: 11111111111111111111111111111111 11111111111111111111111111111111 -------------------------------- 0: 00000000000000000000000000000000 00000000000000000000000000000000 -------------------------------- 1: 00000000000000000000000000000001 00000000000000000000000000000001 -------------------------------- 2147483647: 01111111111111111111111111111111 01111111111111111111111111111111 -------------------------------- 4294967295: 11111111111111111111111111111111 11111111111111111111111111111111 -------------------------------- knoppix@Microknoppix:~$

        However, I'm happy to be corrected if I'm wrong.

        Cheers,

        JohnGG

Re^2: comparison of packed signed integers
by gerleu (Novice) on Oct 30, 2011 at 12:06 UTC

    Hi BrowserUK and thank you for your fast answer :-)

    Here is my code for the file creation.

    open(OUT,'>'.$fil) or die $!; binmode OUT; for $i(1 .. 10000){ $lat=int(rand(20000))+440000; $lon=int(rand(10000))+20000; $typ=chr(31); $cod=pack('l>l>C',$lat,$lon,$typ); print OUT $cod; } close(OUT);
    And here is my code for the read and comparison:

    $lah=pack('l>',$lah); $lab=pack('l>',$lab); $loh=pack('l>',$loh); $lob=pack('l>',$lob); open(IN,"<ch"; @fil=stat(IN); $num=$fil[7]/9; binmode(IN); for(1 .. $num){ read(IN,$lat,4); read(IN,$lon,4); read(IN,$pyt,1); if(($lat lt $lah) && ($lat gt $lab) && ($lon lt $loh) && ($lon gt $lo +b)){ print "OK"; } }

    But it is never OK :-(

      Where are the values of $lah, $lab, $loh, $lob set?


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        They are set just above the given code and they compare correctly if I don't pack them and if I unpack the values in the file too !

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-23 22:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found