Python : Convert IEEE to IBM Floating Point

When creating code to read a SEG-Y file, I get the **IEEE and IBM Floating Point** format data. **IBM floating point numbers** are represented by one bit for the sign (S), 7 bits for the exponent, and 24 bits for the fraction. The exponent is to the base 16 (not 2), and has a bias of 64. Because of the base 16, the fraction may have up to three leading zeros. In this manner, some of the exponential value may be assumed by the fraction, increasing its range, at the cost of some precision. Normalization to the leading bit is not possible. Floating point numbers have no complement (neither one’s nor two’s complement). They are in sign-magnitude format (the sign being bit 31). You get the more information with the table below :

IBM 32-Bit Floating Point Format | |||||||||||||||||||||||||||||||

S | Exponent | Fraction (Note: the most significant three bits [23, 22, and 21] might all be zero). |
|||||||||||||||||||||||||||||

31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |

With this information and searching in internet, I am create a **Python to Convert IEEE to IBM Floating Point**. You can copy this Python Convert IEEE to IBM Floating Point at below :

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
''' Created on July 11, 2012 @author: toto ''' import string import sys import struct import numpy as np import math # convert floating point to ibm def ieee2ibm(x, endian): # check input data if(x > 7.236998675585915e+75): return(0x7ffffff0) if(x < -7.236998675585915e+75): return(0xfffffff0) if(x == 0): return 0 if(x == np.NAN): #check input if NAN number return (0x7fffffff) #conversion log2 from matlab F, E = math.frexp(abs(x)) e = float (E/4.0); # exponent of base 16 ec = math.ceil(e); # adjust upwards to integer p = ec + 64; # offset exponent f = F * pow(2,(-4*(ec-e))); # correct mantissa for fractional part of exponent # convert to integer. Roundoff here can be as large as # 0.5/2^20 when mantissa is close to 1/16 so that # 3 bits of signifance are lost. f1 = round(f * 0x1000000); # format hex # put exponent in first byte of psi. tmpi = p * 0x1000000; if(tmpi<=0): psi = 0 elif(tmpi>=0xFFFFFFFF): psi = 0xFFFFFFFF else: psi = tmpi # put mantissa into last 3 bytes of phi if(f1<=0): phi = 0 elif(f1>=0xFFFFFFFF): phi = 0xFFFFFFFF else: phi = f1 # make bit representation # exponent and mantissa b = int(psi) | int(phi) # sign bit if(x<0): b = b + 0x80000000 #print b b = np.uint32(b) if(endian): #big endian cval = struct.pack(">i",b) else: #litte endian cval = struct.pack("<i",b) return (cval) #MAIN PROGRAM x = 100.3 fibm = ieee2ibm(x, 1) strinfo = 'x=%i' %x + ' --> IBM Value = %f' %fibm print(strinfo) |

Save this Python to **Convert IEEE to IBM Floating Point source** code as **ieee2ibm.py**. Try to running this **Python to Convert IEEE to IBM Floating Point** with command :

1 |
python ieee2ibm.py |

This is a **Python to Convert IEEE to IBM Floating Point source code**, are you have other method to **convert IEEE to IBM Floating Point **? Please share at here

My name is Toto Sugito. This is my notes when I try something. Maybe, this is **NOT THE BEST SOLUTION** for your problems. If you have other method or idea that better with my post, please share in this blog.** Thank for visiting my site**.