473,410 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

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 1402
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Kevin Knorpp | last post by:
Hello. I need to be able to extract the data from the attached file (or any file in the same format) so that I can work with the data in PHP. I'm fairly comfortable with using PHP with...
2
by: gilgantic | last post by:
How do you deal with floating points in J2ME? From my understanding MIDP 1.0 does not allow you to use float or double primitives. I want to be able to perform a calculations which give float or...
1
by: Noam Raphael | last post by:
Hello, repr(0.1) is '0.10000000000000001', in my Python 2.3. From reading the article "Floating Point Arithmetic: Issues and Limitations", I understand that it's the closest 17-digit decimal...
1
by: Anders K. Jacobsen [DK] | last post by:
Hi I have a MSSQL database with has alot of numeric(18,2). I use LLBLGenPro as O/R and it presents them as Decimals. Fine. When i databind these O/R objects to a datagrid it "sees" that it´s 2...
1
by: ankita pandya | last post by:
I've a problem in a program in c++. It's abt converting decimal no. to binary floating point form. I wish if anybody help me for this!!!!!
9
by: pereges | last post by:
Hi, I'm trying to write a macro for the relative difference function which is used to check the close enough floating point values. Is this correct way to write it ? : #define max(x, y) ((x)...
2
by: Rob Clewley | last post by:
Dear Pythonistas, How many times have we seen posts recently along the lines of "why is it that 0.1 appears as 0.10000000000000001 in python?" that lead to posters being sent to the definition...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.