passing floats in inet socket-based IPC

Jim Balter jim at segue.segue.com
Sun Dec 2 01:32:24 AEST 1990


In article <1990Nov29.224651.26522 at zoo.toronto.edu> henry at zoo.toronto.edu (Henry Spencer) writes:
>In article <1990Nov26.164122.6152 at noose.ecn.purdue.edu> luj at delta.ecn.purdue.edu (Jun Lu) writes:
>>Is there a quick/neat way to passing floats in inet socket-based IPC, just
>>like we "routinely" do for passing ints on "popular" architectures( with
>>just some hton or ntoh conversions) ?
>
>In a word, no.  There is too much diversity in floating-point formats to
>admit of any graceful general solution that's also quick and simple.

It wouldn't be hard to put together routines that convert to/from a canonical
bit representation from/to the native architecture.  The bits would be packed
into bytes (either hi bit first or lo bit first, pick the convention).  A
simple representation for floating point would consist of an exponent sign, a
variable length exponent, a mantissa sign, and a variable length mantissa.
If numbers are restricted to normalized form, the high-order bit of the
mantissa can be suppressed ("hidden" in the usual terminology).  A simple way
to encode variable length bit sequences is to add a 0 after any occurrence of
11 and terminate the sequence with 111.  The conversion is certainly slower
than swapping bytes, but beats the heck out of ASCII, and is much more compact
(in fact, many numbers are smaller than their fixed-width representations).
Base-10 ASCII is the worst possible representation because conversion between
base 10 and base 2 floats is either very expensive and loses precision,
or mind-bogglingly expensive and doesn't (arbitrary base 10 can't be converted
intact to base 2, but of course those that already represent base 2 values can,
but it requires multi-precision multiply, and the base 10 ASCII strings can be
rather long).



More information about the Comp.unix.questions mailing list