473,738 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2910
On 2008-03-05 23:01:51 -0500, Pavel
<dot_com_yahoo@ paultolk_revers e.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_revers e.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 objektorientier ter Datenverarbeitu ng
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_revers e.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_revers e.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_revers e.yourself>
wrote:
James Kanze wrote:
On Mar 7, 6:17 am, Pavel <dot_com_yahoo@ paultolk_revers e.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 objektorientier ter Datenverarbeitu ng
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_err or 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
7289
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: ---------------------------------------------------------------------------- <applet code="example.class"> <param name="font" value="Arial"> <param name="width" value="80"> <param name="image_url" value="http://some.url/image.gif"> </applet>
5
4408
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 was quite insestual for while wasn't it? I need to write putchar so that printf can function properly. Anyway, the compiler comes with just a shell of a putchar routine. It literally returns the character you passed to it and nothing else. That is...
4
2311
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 Password on my computer so that I don't have to enter it again. How is it that this standard dialog is displayed? Does the ASP (in my case C#) do this or does the Web Server do this and the ASP never knows about it? ...
29
2148
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. My question is : Can i configure a webservice for HHTP POST ? The remote peer just performs an http web request. NO RPC style call to my system.
13
21702
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 using queues... i will try on the obsolete web pages only and so tht i can learn of how to do that.. i have taken a course called search engines and need some help in doing that... help in any knind would be appreciated.. thank u
2
1847
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
2594
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 out of that directory(where my application reside). how can i overcome to it. my code is this.. Form to select two files <form method="post" action="" enctype="multipart/form-data">
11
1494
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 apps? In other words what type of program do I create? 2. Anyone point me to a tutorial that is very basic like write one that takes two numbers and returns the sum? 3. Getting a bit deeper now, but I need to know if to use VB 2003 or
0
8968
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9334
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9259
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8208
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6750
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6053
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2193
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.