Using memcmp on doubles (don't laugh) 
Author Message
 Using memcmp on doubles (don't laugh)

Yep, you read that right.  Nope, I don't just expect it to work.  I
plan to exceed the boundaries of ANSI C while keeping it as portable
as possible.

I'm working on a functions to marshall C data types into forms upon
which memcmp will give the "right" answer.  This is building key
strings for a btree.

unsigned char is no problem.

unsigned short, int, and long are turned to big-endian form (htons and
htonl are working here).

signed char becomes "excess-CHAR_MAX+1"; signed short and long are
also turned to "excess-n" form, and then to big-endian.

Strings are no problem.

However, float and double have me stumped.  Anybody have any ideas?
Thanks.

--
Richard Krehbiel, Kastle Systems, Arlington, VA, USA



Tue, 22 Aug 2000 03:00:00 GMT  
 Using memcmp on doubles (don't laugh)

: Yep, you read that right.  Nope, I don't just expect it to work.  I
: plan to exceed the boundaries of ANSI C while keeping it as portable
: as possible.

: I'm working on a functions to marshall C data types into forms upon
: which memcmp will give the "right" answer.  This is building key
: strings for a btree.

: unsigned char is no problem.

: unsigned short, int, and long are turned to big-endian form (htons and
: htonl are working here).

: signed char becomes "excess-CHAR_MAX+1"; signed short and long are
: also turned to "excess-n" form, and then to big-endian.

: Strings are no problem.

: However, float and double have me stumped.  Anybody have any ideas?

It seems a very curious project; but try converting the floats/doubles
to strings of chararacters.

Will



Wed, 23 Aug 2000 03:00:00 GMT  
 Using memcmp on doubles (don't laugh)

Create your own data type, in a format useful to your needs.

Supply conversion routines with your public interface, which are heavily
#ifdef'd with machine dependent bit manipulations. Explain to your
customers that floats/doubles are stored in "machine independent"
format.

Hope this helps.
Robert Egan
--
"Did you ever wonder how much deeper the ocean would be
if it weren't full of sponges?" - Steven Wright

To reply by mail, set nospam ==> regan

Quote:

> Yep, you read that right.  Nope, I don't just expect it to work.  I
> plan to exceed the boundaries of ANSI C while keeping it as portable
> as possible.

> I'm working on a functions to marshall C data types into forms upon
> which memcmp will give the "right" answer.  This is building key
> strings for a btree.

> unsigned char is no problem.

> unsigned short, int, and long are turned to big-endian form (htons and
> htonl are working here).

> signed char becomes "excess-CHAR_MAX+1"; signed short and long are
> also turned to "excess-n" form, and then to big-endian.

> Strings are no problem.

> However, float and double have me stumped.  Anybody have any ideas?
> Thanks.



Wed, 23 Aug 2000 03:00:00 GMT  
 Using memcmp on doubles (don't laugh)

Quote:



>> but try converting the floats/doubles
>> to strings of chararacters.

Yes.  This is one of the techniques recommended by the C-faq (see
question 20.5).

  http://www.eskimo.com/~scs/C-faq/top.html

Quote:
>Okay.  What kind of strings?  One of the things I'm also trying to do
>is keep the key strings compact.  The smaller the key rep, the fewer
>levels in the B-tree and the faster the retrieval.  So expanding an 8
>byte double into a 24 byte string representation is not one of the
>things I'd choose first.

The C-faq has some suggestions for alternate formats.

Quote:
>I've been thinking about it and reading the FP spec for Intel.  I may
>disgregard portability and do bit slicing and dicing for the first cut
>at this.  Well, at least it'll be portable to systems that use the
>IEEE floating point representation.

Modulo byte ordering considerations (fool me twice, shame on me), that
is.

You might want to look at:

  http://www.cs.wustl.edu/~jxh/snips/double/double_marshall.h
  http://www.cs.wustl.edu/~jxh/snips/double/double_marshall.c

for some ideas on what it would take to do this in a (somewhat)
portable manner.  For converting binary stream into text streams, just
use any well documented method (uuencode, base64, etc.).

"The C-faq is the official FAQ list of the 2098 Winter Olympics!"

--

http://www.cs.wustl.edu/~jxh/        Washington University in Saint Louis

Quote:
>>>>>>>>>>>>> I use *SpamBeGone* <URL:http://www.internz.com/SpamBeGone/>



Wed, 23 Aug 2000 03:00:00 GMT  
 Using memcmp on doubles (don't laugh)

Quote:

> Yep, you read that right.  Nope, I don't just expect it to work.  I
> plan to exceed the boundaries of ANSI C while keeping it as portable
> as possible.

> I'm working on a functions to marshall C data types into forms upon
> which memcmp will give the "right" answer.  This is building key
> strings for a btree.

> unsigned char is no problem.

> unsigned short, int, and long are turned to big-endian form (htons and
> htonl are working here).

> signed char becomes "excess-CHAR_MAX+1"; signed short and long are
> also turned to "excess-n" form, and then to big-endian.

> Strings are no problem.

> However, float and double have me stumped.  Anybody have any ideas?
> Thanks.

There are two possibilities: You have floating point numbers in IEEE
format, or you dont. If you have IEEE format, you can do the following:

   1. Change the byte order, if necessary, so that the sign byte with the
highest byte of the exponent comes first, then the next byte of the
exponent, then the highest byte of the mantissa and so on.

   2. If the sign bit is not set, just set the sign bit.
      If the sign bit is set, negate all the bits in the number.

After step 2, you are almost there. There are two problems left: +0 and -0
will now be different, so in step 2, if the sign bit is set and the number
is equal to -0, leave it unchanged. The other problem are NaNs: All
positive NaNs will be larger than infinity, and negative NaNs will be
smaller than -infinity. You will have to decide what you want in your
application. If NaNs are supposed to behave like in the IEEE standard,
they cant be keys in an index anyway.

If you dont have IEEE format, convert to IEEE format first :-)



Fri, 25 Aug 2000 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Dumb question. Please don't laugh

2. memcmp for double comparison

3. VC7.0: class template using declarations don't work

4. Using fopen when you don't know the name of the file

5. Using VB ActiveX Dll don't get any dispatch

6. Using CSplitterWnd, the buttons of other dialog don't work

7. GNU Make (don't laugh) for MS-DOS?

8. HREF's to .exe's don't work

9. I don't what's wrong!

10. Don't understand what's Functions

11. Don't understand what's Functions

12. Don't know why this doesn't work, but

 

 
Powered by phpBB® Forum Software