Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 18th, 2008, 10:15 PM
julvr
Guest
 
Posts: n/a
Default 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
  #2  
Old March 18th, 2008, 10:25 PM
peter koch
Guest
 
Posts: n/a
Default Re: standard encoding for float?

On 18 Mar., 22:11, julvr <nos...@ulvr.comwrote:
Quote:
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
  #3  
Old March 18th, 2008, 10:35 PM
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
 
Posts: n/a
Default Re: standard encoding for float?

On 2008-03-18 22:11, julvr wrote:
Quote:
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
  #4  
Old March 19th, 2008, 12:26 AM
julvr
Guest
 
Posts: n/a
Default Re: standard encoding for float?

On Mar 18, 5:29*pm, Erik Wikström <Erik-wikst...@telia.comwrote:
Quote:
On 2008-03-18 22:11, julvr wrote:
>
Quote:
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.
  #5  
Old March 19th, 2008, 11:35 AM
James Kanze
Guest
 
Posts: n/a
Default Re: standard encoding for float?

On Mar 18, 10:11 pm, julvr <nos...@ulvr.comwrote:
Quote:
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:james.kanze@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
  #6  
Old March 26th, 2008, 12:25 PM
James Kanze
Guest
 
Posts: n/a
Default Re: standard encoding for float?

On Mar 25, 6:14 pm, Richard Herring <junk@[127.0.0.1]wrote:
Quote:
In message
<198ee077-6d86-480f-8ce7-f24454eb9...@s50g2000hsb.googlegroups.com>,
James Kanze <james.ka...@gmail.comwrites
[...]
Quote:
Quote:
#if FLT_RADIX == 2 && FLT_MANT_DIG == 24 && ...
// IEEE implementation
Quote:
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:james.kanze@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
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles