469,888 Members | 1,236 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,888 developers. It's quick & easy.

x-platform binary IO of floating points

template <class flt> void write(FILE* pF, const flt& f)
{
// this will definitely _not_ write
// an x-platfrom binary file
fwrite(&f, sizeof(f), 1, pF);
}
int main()
{
FILE* pF = fopen("test", "wb");
write(pF, 1.234);
}
How to make this work on any processor architecture, so the file
created is portable?
--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

Jul 23 '05 #1
6 1300
Gernot Frisch schreef:
template <class flt> void write(FILE* pF, const flt& f)
{
// this will definitely _not_ write
// an x-platfrom binary file
fwrite(&f, sizeof(f), 1, pF);
}
int main()
{
FILE* pF = fopen("test", "wb");
write(pF, 1.234);
}
How to make this work on any processor architecture, so the file
created is portable?


x-platform binary is by definition impossible. How many bits are there
in a byte? There's a reason CHAR_BIT exists. Now, as long as you stick
to the usual characters in the textual representation of a float (ie,
no locale-specific non-ASCII chars) these textual representations are
guanranteed to have an equivalent on every C++ implementation. I.e.
there is a char '0' everywhere, even though the binary representation
may have different values (or even lengths). That's how text is
portable - there is a hidden conversion that is logically defined.

Now, you can easily define a similar binary conversion by defining
a binary file as a sequence of numbers in the range [0,255]. Every
C++ implementation can generate such a file, and read it. Of course,
when moving a file from one system to another a conversion might be
required.

This still doesn't buy you what you want. The set of representable
values differs between different float implementations. In particular,
an implementation with 64-bits floats can create a binary file which
cannot be parsed on a system with 32-bits floats.

HTH,
Michiel Salters

Jul 23 '05 #2
Gernot Frisch wrote:
How to make [writing binary floats] work on any processor architecture, so the file
created is portable?


Do you really mean _any_ architecture? Would you be happy with
something that works on any system with IEEE 754 floating point? In
that case I think you only need to worry about endianness, and the
standard htonl and ntohl will work for floats (not sure about doubles).
Otherwise, just convert to text. Oh, unless you want to read it
on a non-ASCII machine...

--Phil.
Jul 23 '05 #3

"Phil Endecott" <ph*******@chezphil.org> schrieb im Newsbeitrag
news:Xd******************@newsfe2-gui.ntli.net...
Gernot Frisch wrote:
How to make [writing binary floats] work on any processor
architecture, so the file created is portable?


Do you really mean _any_ architecture? Would you be happy with
something that works on any system with IEEE 754 floating point? In
that case I think you only need to worry about endianness, and the
standard htonl and ntohl will work for floats (not sure about
doubles). Otherwise, just convert to text. Oh, unless you want to
read it on a non-ASCII machine...


Well, in particular these platforms:
x86, ARM (for Windows Mobile Devices and Playstation), Alpha (the MAC
computers) would do. Do all these have IEEE 754 floats?

I'll better take a jouney on google with IEEE 754.

-Gernot
Jul 23 '05 #4
Gernot Frisch wrote:
"Phil Endecott" schrieb
Gernot Frisch wrote:
How to make [writing binary floats] work on any processor
architecture, so the file created is portable?


Do you really mean _any_ architecture? Would you be happy with
something that works on any system with IEEE 754 floating point?


Well, in particular these platforms:
x86, ARM (for Windows Mobile Devices and Playstation), Alpha (the MAC
computers) would do. Do all these have IEEE 754 floats?


Hmm, "Alpha MAC"s. I think this tells us all something about Apple's
PowerPC to Intel transition: people didn't even know it had a PowerPC in
it in the first place, just that it "wasn't Intel". You do mean Apple
Mac, don't you?

Anyway, yes, these do all use IEEE floating point formats.

--Phil.
Jul 23 '05 #5
On Mon, 20 Jun 2005 12:41:35 +0400, Gernot Frisch <Me@Privacy.net> wrote:
template <class flt> void write(FILE* pF, const flt& f)
{
// this will definitely _not_ write
// an x-platfrom binary file
fwrite(&f, sizeof(f), 1, pF);
}
int main()
{
FILE* pF = fopen("test", "wb");
write(pF, 1.234);
}
How to make this work on any processor architecture, so the file
created is portable?


Use some serialization library, like XDR. See <rpc/xdr.h> header.

--
Maxim Yegorushkin
Jul 23 '05 #6
"Phil Endecott" <ph*******@chezphil.org> wrote in message
news:Xd******************@newsfe2-gui.ntli.net...
Do you really mean _any_ architecture? Would you be happy with something
that works on any system with IEEE 754 floating point? In that case I
think you only need to worry about endianness, and the standard htonl and
ntohl will work for floats (not sure about doubles).


IEEE allows the bits of a floating-point number to be stored in any
sequence, so controlling for endianness isn't sufficient to guarantee
portability.
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by gilgantic | last post: by
1 post views Thread by Noam Raphael | last post: by
1 post views Thread by Anders K. Jacobsen [DK] | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.