Re: How to byte swap an IEEE Float?



Björn a écrit :
I need to read some float values from file that have been written in
big-endian byte order from a c-program. The simple swapping procedure
that I have just interchanges the byte order of type IEEE_Float_32 to
get little-endian. The problem is that for some values (eg. 33.229000)
it is a "NaN" when doing IEEE_Float_32'Read and I get a constraint
error (invalid data) from stream_io when the value is read. How do I
get around this?

I had to deal with byte swapping for floats and doubles between Intel and PPC. This was C++ and I discovered that swapping had to be done in a neutral form (byte array) before interpretation to float or double. Initialy I directly swapped floats and doubles, and for certain values some bits were changed.
So a function with such a signature should never be defined:
function Swap (X : Float) return Float;
It seems that all bit patterns can not be legal floats. I interpreted this as a normalisation of the float, but this is not sure at all.


So, to swap a float or a double X, do this :
1) convert X to a byte array Y (right size)
2) swap Y from host 1 format to network or archive format
3) send Y
4) read Y
5) swap Y from network or archive format to host 2 format
6) convert Y to X

I don't know if this is related to your problem, but it seems similar, and it may help.
It was C++, but those issues are language independent.


Damien Carbonne
.



Relevant Pages

  • Re: swap of two no.?????
    ... other is float using pointers in C. ... What he wants is that you swap the _bytes_ ... memcpy(temp, the_int, sizeof (int)); ... Make sure you use the float as soon as possible, ...
    (comp.lang.c)
  • Re: swap of two no.?????
    ... i want to know that how to swap 2 values,that is one is integer & ... other is float using pointers in C. ... My mom told me that ...
    (comp.lang.c)
  • Re: random number including 1 - i.e. [0,1]
    ... interval ) is closer to correct than before, ... will return a random float in the semi-open ... (Swap a and b if a> b.) ... sometimes a closed interval is worth noting in the docs. ...
    (comp.lang.python)
  • Re: swap of two no.?????
    ... i want to know that how to swap 2 values,that is one is integer & ... other is float using pointers in C. ... expand on what it is that you want? ...
    (comp.lang.c)