473,569 Members | 2,438 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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%c gl%ssic%ccom%c" , "ma", 58, 'g', 64, "ba", 46, 10);}

Jul 23 '05 #1
6 1410
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*******@chez phil.org> schrieb im Newsbeitrag
news:Xd******** **********@news fe2-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*******@chez phil.org> wrote in message
news:Xd******** **********@news fe2-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
3742
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 databases, arrays, etc. but have not worked with binary data files. http://www.performancecentral.net/C...3Loop/3Loop.php click on "Download...
2
8317
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 double values. Thanks
1
1607
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 representation of the binary floating-point number actually stored. However, I don't understand why this should be what repr of floats returns. repr...
1
917
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 decimals and print it like: 14.00 even when no numbers are supplied. The customer demand is now that when no the number is xx.00 it should be xx...
1
1708
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
284
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) (y) ? (x) : (y)) #define eq(a, b) max(fabs(a), fabs(b)) == 0.0 ? 0.0 : fabs(a - b) / (max(fabs(a), fabs(b)))
2
1883
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 of the IEEE 754 standard and the decimal.py module? I am teaching an introductory numerical analysis class this fall, and I realized that the best...
0
7618
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8138
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7679
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6287
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5223
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3657
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
946
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.