Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Floating-point reals, hexadecimal and decimals


From   Joly.Patrick@ic.gc.ca
To   statalist@hsphsun2.harvard.edu
Subject   st: Floating-point reals, hexadecimal and decimals
Date   Wed, 2 Jul 2003 18:21:47 -0400

This is a Stata and Perl question.  I am generating a Perl module to read
Stata data files and I need some clarification with regard to the conversion
of floating-point numbers into decimal and hexadecimal values.
Specifically, I am able to convert values from binary to decimal (and
vice-versa) but I am having trouble with the hexadecimal to binary
conversion.

Lets take single-precision floats for instance.  When I read 4-bytes of data
from a Stata .dta file which I know represent a missing value (i.e. the
4-bytes are represented by 00 00 00 7f in hexadecimal (on a LOHI system))
as,

    read DTA, my ($val), 4;
    $val = unpack("f", $val);

This results in the following mappings:

Stata value (bytes read)          maps to (Perl variable $val)
------------------------          ----------------------------
    .   (0000007f)               '1.70141183460469e+038'
    .a  (0008007f)               '1.70182721835338e+038'


The reverse (decimal -> binary) conversion also works when I write a .dta
file thusly,

    # de-reference object data
    my $val = $self->{DATA}{"$vlist[$k]"}{CELLS}[$n];  
    syswrite DTA, pack("f", $val), 4;

So far, I am delighted, everything works as expected.

The puzzling thing is when I try to write a missing value by 'packing' it
from its hexadecimal representation, according to the following table
(pasted from -help .dta-)

   V            value                HILO             LOHI
   ---------------------------------------------------------------
<snip>
   .    +1.000000X+7f           7f000000          0000007f
   .a   +1.001000X+7f           7f000800          0008007f
   .b   +1.002000X+7f           7f001000          0010007f
   .z   +1.01a000X+7f           7f00d000          00d0007f
   ---------------------------------------------------------------

I don't get the expected results, e.g. if I try either

    syswrite DTA, pack("f", 0x0008007f), 4;
or
    syswrite DTA, pack("f", 0x7f000800), 4;

what gets written to DTA is F0070049 and 1000FE4E respectively, not the
0008007F I expected .

Would anyone happen to know why I am having trouble with the hex -> binary
conversion?  Thanks in advance.


Patrick Joly
joly.patrick@ic.gc.ca
pat.joly@utoronto.ca
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index