Want a function that determines a double or float given its 80-bit IEEE 754 SANE (big endian) representation
z
z at z.com
Wed Aug 23 03:24:49 UTC 2023
On Tuesday, 22 August 2023 at 22:38:23 UTC, dan wrote:
> Hi,
>
> I'm parsing some files, each containing (among other things) 10
> bytes said to represent an IEEE 754 extended floating point
> number, in SANE (Standard Apple Numerical Environment) form, as
> SANE existed in the early 1990s (so, big endian).
>
> Note that the number actually stored will probably be a
> positive even integer less than 100,000, so a better format
> would have been to store a two-byte ushort rather than a
> 10-byte float. However the spec chose to have an encoded float
> there.
>
> I would like to have a function of the form
>
> public bool ubytes_to_double( ubytes[10] u, out double d )
> { /* stuff */ }
>
> which would set d to the value encoded provided that the value
> is a number and is sane, and otherwise just return false.
>
> So my plan is just to do this: examine the first 2 bytes to
> check the sign and see how big the number is, and if it is
> reasonable, convert the remaining 8 bytes to a fractional part,
> perhaps ignoring the last 2 or 3 as not being significant.
>
> But --- it seems like this kind of task may be something that d
> already does, maybe with some constructor of a double or
> something.
>
> Thanks in advance for any suggestions.
>
> dan
On 32bit x86 an endianness swap and pointer cast to `real` should
be enough.(seems to be the same format but i could be wrong.)
Else(afaik `real` on 64 bit x86 is just `double`?) you can always
isolate sign mantissa and exponent to three isolated `double`
values(cast from integer to `double`) and
recalculate(`sign*mantissa*(2^^exponent)` according to wikipedia)
the floating point number, since they mostly contain integers
precision loss probably won't be a problem.
More information about the Digitalmars-d-learn
mailing list