Guidance Forums / Reginald Rexx / Reading floating point binary data

Search 搜索
Home Home

Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next First 1 Last
Message1. Reading floating point binary data
Posted by: Colin 2004-07-03 15:39:36
I'm using charin() and valuein() to read a binary file. Problem is that the file contains some floating point values.  e.g. '3f800000'x = 1.0, '3a83126f'x = 0.001 etc.

Does anyone have a function for handling this?
Message2. Re: Reading floating point binary data
Posted by: Jeff Glatt 2004-07-04 02:44:34 Last edited by: Jeff Glatt 2007-06-16 20:45:42 (Total edited 1 time)
That's a standard C "float" datatype in IEEE754 32-bit format. Here's a description of the format:

A "float" C datatype is stored in a 32-bit format consisting of a sign bit, an 8-bit excess-127 binary exponent, and a 23-bit mantissa. The mantissa represents a number between 1.0 and 2.0. Since the high-order bit of the mantissa is always 1, it is not stored in the number.

Because exponents are stored in an unsigned form, the exponent is biased by half its possible value. For type float, the bias is 127. You can compute the actual exponent value by subtracting the bias value from the exponent value.

There's an add-on DLL called floatconv.dll, which has a FloatConv function you pass the 4 bytes that you grab from the file via CHARIN (ie, not VALUEIN). (The only thing that may not make this work is if the file has the 4 bytes stored in little endian order, rather than big endian. But if that's the case, let me know. A simple mod can fix that).

Here's an example:
LIBRARY floatconv
SAY floatconv('3a83126f'x)

/* If you want engineering form (instead of scientific),
 * pass a 1 for the second arg
SAY floatconv('3a83126f'x, 1)
Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next First 1 Last
© Wed 2024-2-21  Guidance Laboratory Inc. Hits:0