473,799 Members | 3,025 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

standard encoding for float?

Is the encoding for float platform independent? That is, if I take
the four bytes, stick them into an ethernet packet send them to
another machine, then (after endian swapping and alignment), set a
float pointer to point at those four bytes, will I get the same
value? It seems to work on the plaforms I have, but this is suppose
to work for any machine types -- so I guess my question is, is the
encoding of float standardized?

Thanks

John
Mar 18 '08 #1
5 4071
On 18 Mar., 22:11, julvr <nos...@ulvr.co mwrote:
Is the encoding for float platform independent? *That is, if I take
the four bytes, stick them into an ethernet packet send them to
another machine, then (after endian swapping and alignment), set a
float pointer to point at those four bytes, will I get the same
value? *It seems to work on the plaforms I have, but this is suppose
to work for any machine types -- so I guess my question is, is the
encoding of float standardized?
Yes. And a float might not be four bytes. Depending on how portable
you want your code to become you might stick to e.g. IEEE floats. If
not you should also consider e.g. what should happen in case you send
a float to a machine that can't represent the value.

/Peter
Mar 18 '08 #2
On 2008-03-18 22:11, julvr wrote:
Is the encoding for float platform independent? That is, if I take
the four bytes, stick them into an ethernet packet send them to
another machine, then (after endian swapping and alignment), set a
float pointer to point at those four bytes, will I get the same
value? It seems to work on the plaforms I have, but this is suppose
to work for any machine types -- so I guess my question is, is the
encoding of float standardized?
No, while IEEE 754 is the most common format there is no guarantee it is
used, and even if it is there is still the endianness issue. If you want
to send floats/doubles across the network then select a suitable
serialisation format and use it (unless you can guarantee that all
communicating hosts uses the same format). A simple solution is to use a
stringstream to convert the number to text and then back again.

--
Erik Wikström
Mar 18 '08 #3
On Mar 18, 5:29*pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2008-03-18 22:11, julvr wrote:
Is the encoding for float platform independent? *That is, if I take
the four bytes, stick them into an ethernet packet send them to
another machine, then (after endian swapping and alignment), set a
float pointer to point at those four bytes, will I get the same
value? *It seems to work on the plaforms I have, but this is suppose
to work for any machine types -- so I guess my question is, is the
encoding of float standardized?

No, while IEEE 754 is the most common format there is no guarantee it is
used, and even if it is there is still the endianness issue. If you want
to send floats/doubles across the network then select a suitable
serialisation format and use it (unless you can guarantee that all
communicating hosts uses the same format). A simple solution is to use a
stringstream to convert the number to text and then back again.

--
Erik Wikström
Thanks for the replies -- I was suspecting that this would be the
case, but I just wanted to double check.
Mar 18 '08 #4
On Mar 18, 10:11 pm, julvr <nos...@ulvr.co mwrote:
Is the encoding for float platform independent? That is, if I
take the four bytes, stick them into an ethernet packet send
them to another machine, then (after endian swapping and
alignment), set a float pointer to point at those four bytes,
will I get the same value? It seems to work on the plaforms I
have, but this is suppose to work for any machine types -- so
I guess my question is, is the encoding of float standardized?
Yes and no. There is a "standard" encoding, but it's
independant of the C++ standard, and not used everywhere.

Off hand, I know of at least four different encodings in use on
machines being sold today. Most, however, are used on
mainframes, for historical reasons: if you're only goal is to
communicate between mainstream Unix machines and Windows, then
you can probably count on IEEE format: type pun your float to a
uin32_t, and transmit that. (Note that even integers have
different formats on some machines: 36 bit 1's complement, or 48
bit signed magnitude, at least, are still in use today. Again,
however, not everyone necessarily has to worry about these.)

--
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 19 '08 #5
On Mar 25, 6:14 pm, Richard Herring <ju**@[127.0.0.1]wrote:
In message
<198ee077-6d86-480f-8ce7-f24454eb9...@s5 0g2000hsb.googl egroups.com>,
James Kanze <james.ka...@gm ail.comwrites
[...]
#if FLT_RADIX == 2 && FLT_MANT_DIG == 24 && ...
// IEEE implementation
You'd need to test a lot more than just those two macros, of
course. FLT_RADIX == 2 && FLT_MANT_DIG == 24 would also match
VAX-11 F_floating, even though its representation and
semantics are quite different from IEEE.
Agreed. That's why I left the ... At least some tests with
FLT_MIN_EXP and FLT_MAX_EXP would necessary.

--
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 26 '08 #6

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

Similar topics

5
1846
by: jalil | last post by:
1. I download a page in python using urllib and now want to convert and keep it as utf-8? I already know the original encoding of the page. What calls should I make to convert the encoding of the page to utf8? For example, let's say the page is encoded in gb2312 (simple chinese) and I want to keep it in utf-8? 2. Is this a good approach? Can I keep any pages in any languages in this way and return them when requested using utf-8...
7
4976
by: Mark | last post by:
Hi... I've been doing a lot of work both creating and consuming web services, and I notice there seems to be a discontinuity between a number of the different cogs in the wheel centering around windows-1252 and that it is not equivalent to iso-8859-1. Looking in the registry under HKEY_CLASSES_ROOT\MIME\Database\Charset and \Codepage, it seems that all variations on iso-8859-1 (latin1, etc) are mapped to code page 1252, which I'm...
9
23715
by: Mark | last post by:
I've run a few simple tests looking at how query string encoding/decoding gets handled in asp.net, and it seems like the situation is even messier than it was in asp... Can't say I think much of the "improvements", but maybe someone here can point me in the right direction... First, it looks like asp.net will automatically read and recognize query strings encoded in utf8 and 16-bit unicode, only the latter is some mutant, non-standard...
0
1900
by: Brad Wood | last post by:
I have a web service that returns an xml doc as a string. I use a StringWriter with my XmlTextWriter and the encoding attribute for the document becomes utf-16. If I save the result of the web service to a file, the document won't parse in I.E. unless I change the responseEncoding in my web.config to utf-16 and write the result to file encoded in unicode. I've read that utf-8 is a more standard encoding; is it acceptable to change the...
0
1116
by: Chakravarthy | last post by:
Hi, I'm getting response from one of our vendor service in iso-8859-1 encoding style, which is Europian Occidental. Now tell me, how to handle this stream of input in .NET? If i'm not mistaken, this MS Standard encoding technique is faaaaaaaar superior than this, how to handle the lower encoding strem in .NET? Thanks for reading this, Chakravarthy
7
9780
by: Ralf Goertz | last post by:
Hi, since my previous post <455440ad$0$30326$9b4e6d93@newsspool1.arcor-online.netis still unanswered I'd like to rephrase my question. In order to read/write a wstring in UTF-8 encoding it is *not* sufficient to imbue the stream with a locale like "de_DE.UTF-8". Doing so only takes care of facets of decimal numbers and the like. Rather, one has to call locale::global("de_DE.UTF-8"). Is this behaviour conforming to the standard? And if...
32
2049
by: r.z. | last post by:
class vector3 { public: union { float data; struct { float x, y, z; };
9
3919
by: hurcan solter | last post by:
Hi all, I am trying to convert a float value to a octet stream for transmission, I came up with solution like float deneme=3.14156789; float deneme2=0.0; vector<unsigned charvec; //this is my buffer can con contain other things besides this float int* val=reinterpret_cast<int*>(&deneme); // gives me jeebies vec.push_back((unsigned char) ((*val >24) & 0xFF ));
0
2973
by: aruna | last post by:
hey guys i earlier had very valuable responses from you all for base64 encoding algorithm.so thank for that. so now i need your assistance to do a float encoding algorithm. you may wonder why i'm so interest about this encoding algorithms. because our group is going to do a project under the ITU-T Recommendation X.891. this is named as fast infoset. this is an open source project in c language. there is an implementation in java which by...
0
9688
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
10268
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
10247
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
10031
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
9079
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
7571
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
5593
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4146
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
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.