Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > IBM 32-bit Floating Point Conversion

Reply
Thread Tools

IBM 32-bit Floating Point Conversion

 
 
magoo
Guest
Posts: n/a
 
      05-03-2004
Gentlemen,

Recently I had need to roll my own conversion routine for reading data
values from a SEGY file (seismic data values) which were in IBM 32-bit
floating point format.

I would like to post my routine in the hopes of perhaps helping someone
else and/or getting feedback on what someone else has done along these
lines.

I was greatly helped by the following article:
http://www.sis.slb.com/content/servi...2_n6_2003i.asp

which can be found by searching Google for:
"what is the difference between ibm 32 bit floating point and ieee"



Step 1: Get a data value from file

READ(IF, $trace_sample, 4)
$bit_string = unpack("B*", $trace_sample);

Step 2: Convert bits which are in IBM 32-bit floating point value

$trace_value = conv_bit_string_2_ibm32float($bit_string);

#================================================= =====================
#copied from "Perl Cookbook, recipe 2.4
sub bin2dec {
return unpack("N", pack("B32", substr("0" x 32 . shift, -32)));
}
#================================================= ======================
#================================================= ======================
sub conv_bit_string_2_ibm32float {
$bit_string = shift;

#================================================= ============
#DEBUG (make $bit_string = "11000010101100011001111110101111")
# (1st bit, sign bit = 1 (denotes negative number))
# (next seven bits, exponent = 66)
# (last 24 bits represent fraction = 0.69384283 in decimal)
# (whole bit string as IBM 32 bit FP = -177.62376 decimal)
#================================================= ============

# A value of 1 for sign bit denotes a negative number
# while a value of zero denotes a positive number
#================================================= ===
$first_digit = substr($bit_string, 0, 1);
if ( $first_digit eq "0" ) {
$sign_bit = 1;
} elsif ( $first_digit eq "1" ) {
$sign_bit = -1;
}

$bin_exponent = substr($bit_string, 1, 7);
$exponent = bin2dec($bin_exponent);

#================================================= ====================
#Computing fraction
#The following will do this:
# take last 24 bits of $bit_string such as "101100011001111110101111"
# for each bit starting from left to right, convert to decimal
# i.e. (1 * 2**-1) + (0 * 2**-2) + (1 * 2**-3) + (1 * 2**-4) ...
#================================================= ====================
$bin_fraction = substr($bit_string, 8, 24);
@bit_chars = unpack("A1" x length($bin_fraction), $bin_fraction);

$place_holder = -1;
$fraction = 0;
foreach $bit ( @bit_chars ) {
$fraction += $bit * (2 ** $place_holder);
$place_holder += -1;
}

$ibm_float = ($sign_bit ** 1) * (16 ** ($exponent - 64)) *
($fraction);
return sprintf("%.10f", $ibm_float);
}
#================================================= ======================

Regards,
Terry Michaels
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
floating point problem... floating indeed :( teeshift Ruby 2 12-01-2006 01:16 AM
converting floating point to fixed point H aka N VHDL 15 03-02-2006 02:26 PM
floating point to fixed point conversion riya1012@gmail.com C Programming 4 02-22-2006 05:56 PM
C code for converting IBM/370 floating point to IEEE 754? Benjamin Rutt C Programming 2 06-22-2005 04:13 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57