Portable Doubles

This is a discussion on Portable Doubles within the C Programming forums, part of the General Programming Boards category; Actually if you convert the numbers into hexidecimal you probably won't notice the difference!! You don't have to use a ...

  1. #16
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Actually if you convert the numbers into hexidecimal you probably won't notice the difference!!
    You don't have to use a zip file, as I said if you write your own conversion programs, there should be little
    or no redundancy in the system. Some of the C wizards may be able to suggest a better way though
    Last edited by esbo; 08-30-2006 at 06:52 PM.

  2. #17
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Quote Originally Posted by esbo
    Actually if you convert the numbers into hexidecimal you probably won't notice the difference!!
    You don't have to use a zip file, as I said if you write your own conversion programs, there should be little
    or no redundancy in the system. Some of the C wizards may be able to suggest a better way though
    Convert it to hexidecimal? If the system stores things as binary, it's going to store it as binary no matter how you wish to display it. It's not as if one hex digit fits in one binary digit therefor you can just convert everything to hex and save space. I don't think they're looking to just store a string directly, I think they want to maintain the same exact bytes. I'm honestly not sure what floating point looks like in hex, but I have a feeling it's representation would be only slightly shorter than just the decimal representation of the string.

    EDIT: For strings -- here is double precision in decimal vs hex
    Code:
    3.1415926535897930
    400921FB54442D18
    So two bytes. I suppose it shrinks it a bit, but I don't think it's small enough for what the OP wants.

    EDIT 2: ...and lastly, which I probably should have thought of first, isn't converting to hex on one comp and back on another subject to the same rounding errors as just sending the double directly would have?
    Last edited by SlyMaelstrom; 08-30-2006 at 07:11 PM.
    Sent from my iPadŽ

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Maybe if you zipped the hexidecimal!

    /mock


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #19
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by EvilGuru
    I am trying to transfer double-width floating point numbers between two computers, whose floating point format is different.
    Perhaps it might be beneficial to mention the formats?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #20
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by SlyMaelstrom
    Convert it to hexidecimal? If the system stores things as binary, it's going to store it as binary no matter how you wish to display it. It's not as if one hex digit fits in one binary digit therefor you can just convert everything to hex and save space. I don't think they're looking to just store a string directly, I think they want to maintain the same exact bytes. I'm honestly not sure what floating point looks like in hex, but I have a feeling it's representation would be only slightly shorter than just the decimal representation of the string.

    EDIT: For strings -- here is double precision in decimal vs hex
    Code:
    3.1415926535897930
    400921FB54442D18
    So two bytes. I suppose it shrinks it a bit, but I don't think it's small enough for what the OP wants.

    EDIT 2: ...and lastly, which I probably should have thought of first, isn't converting to hex on one comp and back on another subject to the same rounding errors as just sending the double directly would have?
    Actually 3.1415926535897930 as a character string will compress considerably more than you indicate, if each character is stored in an eight bit byte, there are 255 possibilities that could be used, however only 10 are being used, (0-9), so there is considerable reduncancy there. If you encode each character in 4 bits rather than 8, which you could you would halve the data length at a stroke. The character string 3.1415926535897930 would use 18 bytes the hexidecimal number 400921FB54442D18 only uses 8, so you have more than halved the transmission time, (44%).
    (I was programming im hexidecimal well before your were born!!, seriously, I started programming in machine code, you lot don't know you are born!!)
    Last edited by esbo; 08-30-2006 at 07:49 PM.

  6. #21
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Ask me what C9 means in Z80 machine code!!
    (lol - don't bother the answer is ret (return))

  7. #22
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Quote Originally Posted by esbo
    Ask me what C9 means in Z80 machine code!!
    (lol - don't bother the answer is ret (return))
    Cool, I got'st the internet, too!
    Sent from my iPadŽ

  8. #23
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,070
    Quote Originally Posted by esbo
    I was programming im hexidecimal well before your were born!!, seriously, I started programming in machine code, you lot don't know you are born!!
    hahaha omg lolz!!!!!1 ur so l33t!

    Memorial University of Newfoundland
    Computer Science

    Mac and OpenGL evangelist.

  9. #24
    Registered User
    Join Date
    Apr 2005
    Posts
    42
    Quote Originally Posted by Dave_Sinkula
    Perhaps it might be beneficial to mention the formats?
    Well, to be honest I am not totally sure on the formats to tell you the truth. The system it will be running on is an embedded system (Gumstix), which uses an ARM processor. I am not sure if it has an FPU or not, it may well be using a software library of some kind.

    The other computer (well application) is written in Java (please don't hurt me!), which is friendly enough to use a standardised format for floats and doubles (DataInputStrem).

    So I guess the real question is how can I split up the parts of a systems/arch's double and re-arrange it so that it is in the same order/format as the doubles used by Java, which I am told uses the IEEE 754 floating point format in Big endian form.

    I am aware the different systems and implementations may have different sizes for various parts of a floating point number, and so a loss of precision may result.

    Of-course if anyone has a better suggestion/way to do it, I am anxious to know!

    Regards, Freddie.

  10. #25
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    If you're feeling lucky, try assuming that the other end is IEEE 754 in little endian format.
    In which case, it's a simple byte-swapping exercise.

    Try sending known floats (like 1.0) and seeing how they're represented on both sides as just arrays of bytes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #26
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Quote Originally Posted by esbo
    If you encode each character in 4 bits rather than 8, which you could you would halve the data length at a stroke.
    I said that already. It was the first reply.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #27
    Registered User
    Join Date
    Apr 2005
    Posts
    42
    Quote Originally Posted by Salem
    If you're feeling lucky, try assuming that the other end is IEEE 754 in little endian format.
    In which case, it's a simple byte-swapping exercise.

    Try sending known floats (like 1.0) and seeing how they're represented on both sides as just arrays of bytes.
    Well, I felt luck, and it worked! Question is, does anyone known of a smarter method to swap the bytes than this:
    Code:
    void my_htond (double *out, double in)
    {
        unsigned char *bin = (unsigned char *) ∈
        unsigned char *bot = (unsigned char *) out;
        int j;
        for (j = 0; j < sizeof(double); j++) bout[j] = bin[(sizeof(double) - 1) - j];
    }

  13. #28
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You could try searching the board for "reverse bytes". One such hit is changing byte order of an unsigned long?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #29
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by dwks
    I said that already. It was the first reply.
    I didn't notice that part of your reply I just noticed the bit about converting into a character string, then people saying that wouuld not be efficient ect....., it would be efficient enough!

  15. #30
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    The character string 3.1415926535897930 would use 18 bytes the hexidecimal number 400921FB54442D18 only uses 8
    I don't understand why. What is the 400921FB54442D18? Hexadecimal of 3.14...? Why its character string uses 8 bytes?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. parse doubles from socket read?
    By willy in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:32 AM
  2. C - Portable?
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 07-15-2007, 09:09 AM
  3. sscanf with doubles
    By ufsargas in forum C Programming
    Replies: 1
    Last Post: 06-28-2006, 03:05 PM
  4. Replies: 3
    Last Post: 09-01-2005, 11:47 AM
  5. which Portable mp3 player?
    By Raihana in forum A Brief History of Cprogramming.com
    Replies: 27
    Last Post: 01-09-2004, 06:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21