473,396 Members | 1,767 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,396 software developers and data experts.

Need a library to write/read floats and doubles in IEEE754

Hello,

Does anyone know a (preferably open-source) multi-platform C or C++
library that would be able to write and read C/C++ doubles and floats
to/from streambuf, char array or similar device in IEEE 754 with
reasonably optimal precision and performance?

The purpose is to exchange serialized doubles and floats between C/C++
and Java programs where Java serialization rules are used.

I tried to search for it on the Internet but, surprisingly, could not
find any mature code for this seemingly common purpose.

Thanks in advance,
-Pavel
Mar 6 '08 #1
6 2892
On 2008-03-05 23:01:51 -0500, Pavel
<dot_com_yahoo@paultolk_reverse.yourselfsaid:
>
Does anyone know a (preferably open-source) multi-platform C or C++
library that would be able to write and read C/C++ doubles and floats
to/from streambuf, char array or similar device in IEEE 754 with
reasonably optimal precision and performance?
http://netlib2.cs.utk.edu/fp/dtoa.c

The general idea is that you convert floating point values to a base 10
text representation with as many digits after the decimal point as are
needed to distinguish the value being written from the two neighboring
values. Sometimes you need very few ("2.0"), and occasioinally you need
quite a few. Caution: it's hackerhead C code, and requires a fair
amount of study to understand what it's doing. There's also a paper
somewhere (I've lost track of it) that gives the theoretical
underpinnings.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Mar 6 '08 #2
On Mar 7, 6:17 am, Pavel <dot_com_yahoo@paultolk_reverse.yourself>
wrote:
James Kanze wrote:
I did not want to write myself and was looking for a library
because I am lazy and some corner cases should be worked out
(native representation does not necessarily represent all same
set of numbers as IEEE 754 representations).
If you have to deal with a native representation that is not
IEEE, then you have to define behavior for corner cases. An IBM
double, for example, has more precision (but less range) than an
IEEE double: I suppose when outputting you round (the code I
posted truncates!), but what happens when you input something
that it too large.

I've heard that there may be similar cases even between IEEE
representations, at least where NaN's are involved---a non
trapping NaN may trap elsewhere, or something like that.
It's quite a bit of coding and testing so I wanted to find if
someone else has already done that before getting into it. I
guess I will start with this code of yours and then try to
work my way using standard C++. Certainly #ifdef - guarded
multi-platform version would do faster for each supported
architecture but hopefully the performance of the standard C++
code will suffice for my purpose.
Well, I'd very definitely not invest the effort in supporting
machines without IEEE until I had to. (The three I know of
today are all mainframes: one uses base 16, one uses base 8, and
the one that uses base 2 has 72 bit doubles. On the latter two,
you'll need special handling for ints as well, because they
don't use 2's complement either: there's one with 48 bit signed
magnitude, with 8 bits required to be 0, and the other uses 36
bit 1's complement. Porting networked software to those should
be quite amusing---except that both have Intel based front-ends
to handle the networking.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Mar 7 '08 #3
On Mar 5, 11:01 pm, Pavel <dot_com_yahoo@paultolk_reverse.yourself>
wrote:
Hello,

Does anyone know a (preferably open-source) multi-platform C or C++
library that would be able to write and read C/C++ doubles and floats
to/from streambuf, char array or similar device in IEEE 754 with
reasonably optimal precision and performance?

The purpose is to exchange serialized doubles and floats between C/C++
and Java programs where Java serialization rules are used.

I tried to search for it on the Internet but, surprisingly, could not
find any mature code for this seemingly common purpose.
Would this help ?
http://tpl.sourceforge.net/

Its an open source serialization library written in C.
Mar 7 '08 #4
James Kanze wrote:
On Mar 7, 6:17 am, Pavel <dot_com_yahoo@paultolk_reverse.yourself>
wrote:
>James Kanze wrote:
>I did not want to write myself and was looking for a library
because I am lazy and some corner cases should be worked out
(native representation does not necessarily represent all same
set of numbers as IEEE 754 representations).

If you have to deal with a native representation that is not
IEEE, then you have to define behavior for corner cases.
True, that's why I hoped someone did it before me in a reasonable way :-).
An IBM
double, for example, has more precision (but less range) than an
IEEE double: I suppose when outputting you round (the code I
posted truncates!), but what happens when you input something
that it too large.
On input will have to check whether IEEE representation exceeds
implementation-specific limits from <climitand <cfloat>; if it does,
throw domain_error or do something similar; also, for output need to
check whether the number to be written is within the respective (single-
or double-) IEEE limit. Long story short, lots of work to do..
I've heard that there may be similar cases even between IEEE
representations, at least where NaN's are involved---a non
trapping NaN may trap elsewhere, or something like that.
True
Well, I'd very definitely not invest the effort in supporting
machines without IEEE until I had to. (The three I know of
today are all mainframes: one uses base 16, one uses base 8, and
the one that uses base 2 has 72 bit doubles. On the latter two,
you'll need special handling for ints as well, because they
don't use 2's complement either: there's one with 48 bit signed
magnitude, with 8 bits required to be 0, and the other uses 36
bit 1's complement. Porting networked software to those should
be quite amusing---except that both have Intel based front-ends
to handle the networking.)
You are right about those -- I do not need them (not right now, anyway);
the main problem even with mostly IEEE-ish architecture that you still
have to detect it (even if only to make sure the preprocessor balks on
unsupported architectures) and for that you need to detect the compiler
first -- both make and version -- because the macros that define the
architectures are implementation-specific and sometimes change between
versions -- and for that you have to first make a decision what versions
of compilers to support. I will need to support 4 compiler makes at
least; as I said, lots of work, at least 90% of which was already done
by others multiple times, better than I ever will.

Anyway, time to stop whining and start coding. Wish me luck.

-Pavel
Mar 8 '08 #5
On 8 mar, 05:53, Pavel <dot_com_yahoo@paultolk_reverse.yourself>
wrote:
James Kanze wrote:
On Mar 7, 6:17 am, Pavel <dot_com_yahoo@paultolk_reverse.yourself>
wrote:
James Kanze wrote:
I did not want to write myself and was looking for a library
because I am lazy and some corner cases should be worked out
(native representation does not necessarily represent all same
set of numbers as IEEE 754 representations).
If you have to deal with a native representation that is not
IEEE, then you have to define behavior for corner cases.
True, that's why I hoped someone did it before me in a
reasonable way :-).
The problem isn't just the doing. The problem is specifying
what you mean by "a reasonable way".
An IBM
double, for example, has more precision (but less range) than an
IEEE double: I suppose when outputting you round (the code I
posted truncates!), but what happens when you input something
that it too large.
On input will have to check whether IEEE representation
exceeds implementation-specific limits from <climitand
<cfloat>; if it does, throw domain_error or do something
similar; also, for output need to check whether the number to
be written is within the respective (single- or double-) IEEE
limit. Long story short, lots of work to do..
If the absolute value you read is too big for your internal
representation, it's obviously an error (setting failbit would
be the usual behavior). If the absolute value is smaller that
min(), but not zero, however: is it an error, or do you use 0.0?
What if the value you read has more precision than you can
represent? Is it an error, or do you round.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Mar 8 '08 #6
James Kanze wrote:
>>If you have to deal with a native representation that is not
IEEE, then you have to define behavior for corner cases.
>True, that's why I hoped someone did it before me in a
reasonable way :-).

The problem isn't just the doing. The problem is specifying
what you mean by "a reasonable way".
I meant defining, not implementing. I was answering to your statement above.
If the absolute value you read is too big for your internal
representation, it's obviously an error (setting failbit would
be the usual behavior).
I have already decided on a non-stream-related API, so there will be
nothing to set the failbit on. For signaling errors, I am leaning to
simple return codes but did not completely rejected the idea of throwing
std::domain_error yet.

If the absolute value is smaller that
min(), but not zero, however: is it an error, or do you use 0.0?
What if the value you read has more precision than you can
represent? Is it an error, or do you round.
Will round. It is not my purpose to reject as many inputs as possible;
it is the other way around.

Regards
-Pavel
Mar 8 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: SzokU | last post by:
Hi All! I have problem... I want to write/read value from param tag in applet, but I don't know how... This is exaple code:...
5
by: Confused User | last post by:
I am working on device that utilizes a Motorola 68HC16 microcontroller. I am using an old unsupported piece of crap Whitesmith's / Intermetrics / Tasking compiler. The embedded compiler business...
4
by: Zachary Hilbun | last post by:
I need to write an ASP that requires a user to give a User Name and Password to run it. Whenever I've used this on the Web it displays a standard dialog and then offers to save the User Name and...
29
by: jens Jensen | last post by:
Hello, I got this "breath taking" task to write a an http server to which "xml data" will be posted to and will answer with xml data. The logic behind the xml processing is not a matter here. ...
13
by: Pradeep Vasudevan | last post by:
hai i am a student and need to write a simple web crawler using python and need some guidance of how to start.. i need to crawl web pages using BFS and also DFS... one using stacks and other...
2
by: Wanjun Yu | last post by:
What is the class name for open/write/read a text file? I can't find it in the reference book. Thanks! WJ
2
kamill
by: kamill | last post by:
i need to write content of one text file into another text file. My code is working ,if i choose both files from same directory where my program reside..BUT,its not working if i select files from...
11
by: cj | last post by:
I have used web services before WSDL files and such but never written one. I need some basic starting info. 1. Can I write a web service in a windows app? or are all web services asp web...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.