473,326 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

IBM integer and double formats

I'm poking at writing data out to a SAS XPORT file (transport file).
Each record must be 80 bytes long, ASCII. Integers should be "IBM-
style integers" and floats should be "IBM-style doubles." Now I have
some idea what that means from reading a C source file documented by
the SAS institute, but before I go over the deep end trying to write
my own routines, does anyone know of an already-done means of writing
integers and floats out to their IBM mainframe equivalents?

Thanks,
John
Nov 10 '08 #1
8 3614
On Nov 10, 7:20*pm, "john.goodleaf" <john.goodl...@gmail.comwrote:
does anyone know of an already-done means of writing
integers and floats out to their IBM mainframe equivalents?
I don't know of anything in Python that does this. There was
a thread a while ago that may be relevant:

http://mail.python.org/pipermail/pyt...ch/255361.html

and Googling 'IBM VAX double conversion' produces some tools that
convert between IEEE, IBM and VAX double formats. I'm not quite
sure what you're after here: I assume that you're working on
some type of modern non-IBM (and therefore presumably IEEE
754-based) hardware, and you want to be able to convert IEEE
doubles to IBM style doubles. Is that right?

It shouldn't be too difficult to write routines, making use
of math.ldexp and math.frexp. The fun part would be deciding
how to handle NaNs and infinities when encoding to IBM format,
and how to handle out-of-range floats coming back (if I recall
correctly, the IBM format allows a wider range of exponents
than IEEE).

Mark
Nov 10 '08 #2
On Nov 10, 8:16*pm, Mark Dickinson <dicki...@gmail.comwrote:
and how to handle out-of-range floats coming back (if I recall
correctly, the IBM format allows a wider range of exponents
than IEEE).
Whoops---wrong way around. It looks like it's IEEE that has the
larger exponent range. DBL_MAX is around 2.**252 for IBM and
2.**1024 for IEEE.

Sorry.

Mark
Nov 10 '08 #3
On Nov 10, 7:20*pm, "john.goodleaf" <john.goodl...@gmail.comwrote:
my own routines, does anyone know of an already-done means of writing
integers and floats out to their IBM mainframe equivalents?
Here's a quick attempt at converting doubles using Python.
It uses the isnan and isinf functions that are in Python 2.6;
if you don't have Python 2.6 or don't care about IEEE specials
then just delete the relevant lines.

Mark

def IBMtoIEEE(x):
"""Convert a Python float (assumed stored as an IEEE 754 double)
to IBM hexadecimal float format.

NaNs and infinities raise ValueError. IEEE values that are too
large to be stored in IBM format raise OverflowError. Values that
are too small to be represented exactly in IEEE format are rounded
to the nearest IBM value, using round-half-to-even.

The result is returned as a hex string.

"""
if isnan(x) or isinf(x):
raise ValueError("cannot convert infinity or nan to IBM
format")
if not x:
s, m, e = 0, 0, 0
else:
s = 0 if x 0.0 else 1
m, e = frexp(x)
m, e = int(abs(m) * 2**(56 - -e % 4)), (e + -e % 4)//4 + 64
if e >= 128:
raise OverflowError("value too large to represent in IBM
format")
elif e < 0:
h = 2**(4*-e - 1)
m = m // (2*h) + (1 if m & h and m & (3*h-1) else 0)
e = 0
return "%x" % (s*2**63 + e*2**56 + m)
Nov 10 '08 #4
On Nov 11, 9:00*am, Mark Dickinson <dicki...@gmail.comwrote:
On Nov 10, 7:20*pm, "john.goodleaf" <john.goodl...@gmail.comwrote:
my own routines, does anyone know of an already-done means of writing
integers and floats out to their IBM mainframe equivalents?

Here's a quick attempt at converting doubles using Python.
It uses the isnan and isinf functions that are in Python 2.6;
if you don't have Python 2.6 or don't care about IEEE specials
then just delete the relevant lines.

Mark

def IBMtoIEEE(x):
* * """Convert a Python float (assumed stored as an IEEE 754 double)
* * to IBM hexadecimal float format.
Call me crazy if you like, but I'd name that function IEEEtoIBM.
return "%x" % (s*2**63 + e*2**56 + m)
That's a hexadecimal representation in lowercase with no leading
zeroes ... variable length and lowercase doesn't seem very IBM to me.

The extremely ugly C code in
http://support.sas.com/techsup/technote/ts140.html
seems to indicate an 8-byte bigendian binary representation. Note that
page contains a tohex() function which is not used AFAICT.

Perhaps this would do the job:
return struct.pack('>Q', s*2**63 + e*2**56 + m)

Cheers,
John
Nov 10 '08 #5
On Nov 10, 11:49*pm, John Machin <sjmac...@lexicon.netwrote:
Call me crazy if you like, but I'd name that function IEEEtoIBM.
But it's topsy-turvy day! Didn't you get the memo?

Oh, all right. IEEEtoIBM it is.
That's a hexadecimal representation in lowercase with no leading
zeroes ... variable length and lowercase doesn't seem very IBM to me.
True. Replace "%x" with "%016X" for fixed-length uppercase. Or as you
say, bytes output is probably more natural. I was guessing that the
OP
wants to write the converted float out to an ASCII file, in hex.

Mark
Nov 11 '08 #6
On Nov 11, 8:15*pm, Mark Dickinson <dicki...@gmail.comwrote:
On Nov 10, 11:49*pm, John Machin <sjmac...@lexicon.netwrote:
Call me crazy if you like, but I'd name that function IEEEtoIBM.

But it's topsy-turvy day! *Didn't you get the memo?

Oh, all right. *IEEEtoIBM it is.
That's a hexadecimal representation in lowercase with no leading
zeroes ... variable length and lowercase doesn't seem very IBM to me.

True. *Replace "%x" with "%016X" for fixed-length uppercase. Or as you
say, bytes output is probably more natural. *I was guessing that the
OP
wants to write the converted float out to an ASCII file, in hex.
Sheesh. It's an *IBM mainframe* file. It would need to be in EBCDIC,
not ASCII. But why guess? He said he wanted to write it out in SAS
XPORT format.

Nov 11 '08 #7
On Nov 11, 11:36*am, John Machin <sjmac...@lexicon.netwrote:
wants to write the converted float out to an ASCII file, in hex.

Sheesh. It's an *IBM mainframe* file. It would need to be in EBCDIC,
not ASCII. But why guess? He said he wanted to write it out in SAS
XPORT format.
Which is stored in ASCII, no? From the link you gave earlier, 3rd
line of the introduction:

"All character data are stored in ASCII, regardless of the
operating system."

Mark
Nov 11 '08 #8
On Nov 11, 12:07*pm, Mark Dickinson <dicki...@gmail.comwrote:
"All character data are stored in ASCII, regardless of the
operating system."
But character data is not the same thing as numeric data. Okay---
you win again, John.
Sheesh. [...]
Apologies for annoying you.

Mark

Nov 11 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Ashutosh Iddya | last post by:
Hi , I am performing an integer count of a particular operation in my program. After a sufficiently large value an overflow occurs. At the moment I have gone around the problem by declaring it...
19
by: shanx__=|;- | last post by:
hi i need some help regarding use of very very long integer datatype in 'c'.. i need it to store result of large number's factorial.. if someone can healp it would be a delight..
10
by: Bryan Parkoff | last post by:
The guideline says to use %f in printf() function using the keyword float and double. For example float a = 1.2345; double b = 5.166666667; printf("%.2f\n %f\n", a, b);
43
by: Steven T. Hatton | last post by:
http://public.research.att.com/~bs/bs_faq2.html#int-to-string Is there no C library function that will take an int and convert it to its ascii representation? The example Bjarne shows in his faq...
8
by: aarklon | last post by:
hi all, in http://www.c-faq.com/cpp/ifendian.html it is said that integer formats used in pre processor #if expressions are not the same as those will be used at run time. can any one...
60
by: Erick-> | last post by:
hi all... I've readed some lines about the difference between float and double data types... but, in the real world, which is the best? when should we use float or double?? thanks Erick
26
by: Pietro Cerutti | last post by:
Hi group, I always thought that applying a binary operator such as ==, !=, <= or well defined. Now, I'm passing a program through splint and it says: Dangerous equality comparison involving...
21
by: Aman JIANG | last post by:
hi I need to do this (convert double to string) fast, safe and portable. Is there any way to do this ? Except the ways following: 1. C++ I/O stream, stringstream (and boost::lexical_cast) 2....
39
by: rembremading | last post by:
Hi all! The following piece of code has (for me) completely unexpected behaviour. (I compile it with gcc-Version 4.0.3) Something goes wrong with the integer to float conversion. Maybe somebody...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.