Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Personally I think $float = unpack 'f', pack 'L', unpack 'N', $data; is a better solution. It is conforming to the standard, ie, it acknowledges that the input data is in network long format. It is more portable, although you assume that float is 32 bits long. Consider the packing and unpacking process -

packing float as network long: +-----+ +-----------+ +------------+ |float|--->|native long|--->|network long| +-----+ +-----------+ +------------+ and unpacking network long back to float +------------+ +-----------+ +-----+ |network long|--->|native long|--->|float| +------------+ +-----------+ +-----+
where perl takes care of translation between native long (big or little endian) and network long (always big endian), without you having to worry explicitly what is the underlying architecture.

The $float = unpack "f", pack "N", unpack "V", $data; and the $num = unpack "f", reverse $packed_num; solutions both assume that you are on a little endian machine, making you dependent to the underlying architecture.

Update: I have written the following test program to test this solution-
use strict; use IO::File; savefloat(); # <--- this is run from Sun E10000 readfloat(); # <--- this is run from Windows XP sub savefloat { my $original_float = 1234.5678; my $network_long = pack 'N', unpack 'L', pack 'f', $original_float; my $f = new IO::File "NetworkLong.txt", "w"; print $f $network_long, "\n"; } sub readfloat { my $f = new IO::File "NetworkLong.txt", "r"; chomp(my $network_long = <$f>); my $float = unpack 'f', pack 'L', unpack 'N', $network_long; printf "%0.4f\n", $float; }
Two separate tests were conducted -

Test 1
On SUN: float->network_long->disk On WindowsXP: disk->network_long->float
Test 2
On WindowsXP: float->network_long->disk On SUN: disk->network_long->float
I got the same result in both test 1 and test 2. This proves that the particular packing method worked cross-platform on both Sun and WindowsXP, which are big endian and little endian platforms respectively.


In reply to Re: Network IEEE 754 to Native Floats: which pack/unpack ops better? by Roger
in thread Network IEEE 754 to Native Floats: which pack/unpack ops better? by shenme

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-04-19 08:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found