Re: float incompatibility between Windows and UNIX?

Discussion in 'C++' started by Alan Sung, Aug 5, 2003.

  1. Alan Sung

    Alan Sung Guest

    Both are using IEEE double floating point format. Windows, Intel 32-bit
    chips use little endian. Sun and IBM Unix boxes typically use big endian.
    You need some byte swapping if you want to be able to transfer binary
    floating point in a file.

    -al sung
    Rapid Realm Technology, Inc.
    Hopkinton, MA

    "Victor Bazarov" <> wrote in message
    news:...
    > "Geert Verdoolaege" <> wrote...
    > > I have an application that runs under MS Windows that writes some values

    > of
    > > type double to a file (using a TFileStream in Borland C++ Builder). This
    > > file is then sent via FTP to a UNIX machine (actually AIX), where I'm

    > trying
    > > to read it as a file of double using fread.
    > >
    > > On both systems a double is 8 bytes long, but still the values read on

    the
    > > AIX don't match the original ones from the Windows machine. What can be
    > > wrong here?

    >
    > It could be the endian-ness, it could be the floating point value
    > format difference. It could be an error in your code. Most likely
    > the former two. The solution is to send data in text form. There
    > are no guaranteed binary compatibilities between C++ programs.
    >
    > Victor
    >
    >
    Alan Sung, Aug 5, 2003
    #1
    1. Advertising

  2. > Both are using IEEE double floating point format. Windows, Intel 32-bit
    > chips use little endian. Sun and IBM Unix boxes typically use big endian.
    > You need some byte swapping if you want to be able to transfer binary
    > floating point in a file.


    ok, I swapped the bytes and everything comes out ok now. Except for a number
    that is zero, which is converted to some very small number (something of the
    order E-322), but not quite zero. Of course this is not a big problem
    because 1E-322 is as good as zero, but why was it written as 0.00..E+000 on
    the little endian machine, while on the IBM it's something else?

    Thanks,
    Geert
    Geert Verdoolaege, Aug 5, 2003
    #2
    1. Advertising

  3. "Geert Verdoolaege" <> wrote...
    > > Both are using IEEE double floating point format. Windows, Intel 32-bit
    > > chips use little endian. Sun and IBM Unix boxes typically use big

    endian.
    > > You need some byte swapping if you want to be able to transfer binary
    > > floating point in a file.

    >
    > ok, I swapped the bytes and everything comes out ok now. Except for a

    number
    > that is zero, which is converted to some very small number (something of

    the
    > order E-322), but not quite zero. Of course this is not a big problem
    > because 1E-322 is as good as zero, but why was it written as 0.00..E+000

    on
    > the little endian machine, while on the IBM it's something else?


    AFAIK, IEEE floating point values are defined that their 0 is
    "all bits cleared". Flipping its bytes around should give the
    same number (bit pattern). I say, print out the value before you
    pass it onto the other machine, make sure it's "all zeroes".

    Victor
    Victor Bazarov, Aug 6, 2003
    #3
  4. Alan Sung

    visage Guest

    "Victor Bazarov" <> wrote in message news:<me%Xa.74955$uu5.8272@sccrnsc04>...
    > "Geert Verdoolaege" <> wrote...
    > > > Both are using IEEE double floating point format. Windows, Intel 32-bit
    > > > chips use little endian. Sun and IBM Unix boxes typically use big

    > endian.
    > > > You need some byte swapping if you want to be able to transfer binary
    > > > floating point in a file.

    > >
    > > ok, I swapped the bytes and everything comes out ok now. Except for a

    > number
    > > that is zero, which is converted to some very small number (something of

    > the
    > > order E-322), but not quite zero. Of course this is not a big problem
    > > because 1E-322 is as good as zero, but why was it written as 0.00..E+000

    > on
    > > the little endian machine, while on the IBM it's something else?

    >
    > AFAIK, IEEE floating point values are defined that their 0 is
    > "all bits cleared". Flipping its bytes around should give the
    > same number (bit pattern). I say, print out the value before you
    > pass it onto the other machine, make sure it's "all zeroes".
    >
    > Victor


    Yes...thats true

    Another issue is that, for certain bit patterns, the swapped pattern
    is not a valid float value. Certainly on windows, if you have a
    function called like this:

    float swapped_float = SwapAFloat(unswapped_float) ;

    Where SwapAFloat is a function that rearranges the bits, then upon
    assignment of the result to swapped_float, windows steps in and
    assigns the value NaN (not a number) to swapped_float, due to the
    return value not being a valid representation of a float. In this case
    you need to pass swapped_float as a reference parameter.
    visage, Aug 6, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bd
    Replies:
    0
    Views:
    608
  2. David Lees
    Replies:
    1
    Views:
    273
    David Lees
    Mar 7, 2004
  3. akineko
    Replies:
    1
    Views:
    335
    Eric Brunel
    Aug 27, 2008
  4. sintral
    Replies:
    9
    Views:
    4,315
    Ben Bacarisse
    Dec 7, 2008
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,509
    James Kanze
    Oct 8, 2009
Loading...

Share This Page